Coding / Programming Videos

Post your favorite coding videos and share them with others!

Python 3 — The Beginner – Nidhin kumar – Medium

Source link

#EP: 2 — DataTypes Integers, Floats, Strings

Photo by Mika Baumeister on Unsplash

Integers and Floats

There are two Python data types that could be used for numeric values:

  • int — for integer values
  • float — for decimal or floating point values

We can create a value that follows the data type by using the following syntax:

a = int(4.7)   # a is now an integer 4
b = float(4) # b is now a float of 4.0

We can check the type by using the type function:

print(type(a))
int
print(type(b))
float

Python Best Practices

For all the best practices, see the PEP8 Guidelines.

You can use the atom package linter-python-pep8 to use pep8 within your own programming environment in the Atom text editor.

Good

print(10 + 5)

Bad

print(                      10 + 5)

Strings

Strings in Python are shown as the variable type. We can define a string with either double quotes " or single quotes '. If the string you are creating actually has one of these two values in it, then you need to be careful to assure your code doesn’t give an error.

>>> my_string = 'this is a string!'
>>> my_string2 = "this is also a string!!!"

You can also include an in your string to be able to include one of these quotes:

>>> this_string = 'Simon's skateboard is in the garage.'
>>> print(this_string)
Output:
Simon's skateboard is in the garage.

If we don’t use this, notice we get the following error:

>>> this_string = 'Simon's skateboard is in the garage.'

Error:
File "<ipython-input-20-e80562c2a290>", line 1
this_string = 'Simon's skateboard is in the garage.'
^
SyntaxError: invalid syntax

The color highlighting is also an indication of the error you have in your string in this second case. There are a number of other operations you can use with strings as well. In this video you saw a few:

>>> first_word = 'Hello'
>>> second_word = 'There'
>>> print(first_word + second_word)

HelloThere

>>> print(first_word + ' ' + second_word)

Hello There

>>> print(first_word * 5)

HelloHelloHelloHelloHello

>>> print(len(first_word))

5

Unlike the other data types, you can also index into strings, but you will see more on this soon! For now, here is a small example. Notice Python uses 0 indexing — we will discuss this later in this lesson in detail.

>>> first_word[0]

H

>>> first_word[1]

e

The len() function

len() is a built-in Python function that returns the length of an object, like a string. The length of a string is the number of characters in the string. This will always be an integer.

There is an example above, but here’s another one:

print(len("ababa") / len("ab"))
2.5

Exercise

Try the below exercise to be familiar with the topics

  1. What happens if you divide by zero in Python? (print(30/0))
  2. The line of code in the below code will cause a SyntaxError, Run the below code first and solve it
# TODO: Fix this string!
ford_quote = 'Whether you think you can, or you think you can't--you're right.'
print(ford_quote)

3. We’ve already seen that the type of objects. What will be the output of the below code? Will it be 3415 or 49

apple_count = "34"
orange_count = "15"
tropical_fruit_count = apple_count + orange_count
print(tropical_fruit_count)

4. Write a Server Log Message

username = "Nidhin"
timestamp = "04:50"
url = "http://medium.com/python/nid/ep1"
# TODO: print a log message using the variables above.
# The message should have the same format as this one:
# "Nidhin accessed the site http://medium.com/python/nid/ep1 at 04:50."

5. Use string concatenation and the len() function to find the length of a certain movie star’s actual full name. Store that length in the name_length variable. Don’t forget that there are spaces between the different parts of a name!

given_name = "William"
middle_names = "Bradley"
family_name = "Pitt"
name_length = # Add your logic here to find the length
# Now we check to make sure that the name fits within the driving license character limit
# Nothing you need to do here
driving_license_character_limit = 28
print(name_length <= driving_license_character_limit)

Got Stuck check this link

Source link

Bookmark(0)
 

Leave a Reply

Please Login to comment
  Subscribe  
Notify of
Translate »