Data Type
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
Integers
a = 10
print(a)10
Just like the print function, there exists a type() function in python which tells us the type of an object inside python.
print(type(a)) # Yes we can chain functions together<class 'int'>
Float
Decimal numbers are called float inside python
b = 10.0
print(b)10.0
print(type(b)) <class 'float'>
Boolean
Boolean is a data type which contains True and False.
True and False values helps us to evaluate certain condition checks as we will discuss later.
d = True
print(d)True
print(type(d))<class 'bool'>
e = False
print(e)False
print(type(e))<class 'bool'>
None
None data type means a null value. A value that does not exist inside python.
our_variable = None
print(our_variable)None
print(type(our_variable))<class 'NoneType'>
Strings
String is a class of sequence data type. It could contain sequence of characters. We have been given a lot of functionalities for strings which we will discuss later.
A string could be created by using either doube quotes or single quotes.
"This is one string"
'This is one string'
Both of these strings have same value.
f = "Some random character sequence"
print(f)Some random character sequence
print(type(f))<class 'str'>
We are not allowed to use different opening and closing quotes to create a string
"This is a string'
In the above string, we started with double quotes, but ended it with a single quote. That statement will throw an error.
# writing as comment because otherwise this will throw an error
# g = "this is a string' Let's say we want to create a string, (Alien's planet)
h = "Alien's planet"h # Colab autoprints the value of a variable at the last code line "Alien's planet"print(h) # output of print() functionAlien's planet
This is the output of print() function: Alien's planet
This is the output of colab's autoprint: "Alien's planet"
Only the output formatting is different but the two means the same thing
print(type(h))<class 'str'>
# not allowed to use same type of quotes like this
# i = 'Alien's planet' We can not create a string like this as the second single quote will be considered as the closing quote of a string
Escape Sequence
"If I wink, know that I want you to lie". Python understood the words of his creator.
\ backslash character changes the behaviour of whatever character follows the backslash character.
In the following code, 'Alien\'s planet' backslash will tell python to treat the second single quote as a literal quote character rather than using it as a closing quote for the string.
j = 'Alien\'s planet'
print(j)Alien's planet
So, we need to be mindful about using these quotes interchangeably.
We can create strings using either of the two ways, but we need to be careful about how we create these strings. Further discussion on it is kind of, out of the scope for this beginner level tutorial.
#"Alien's planet" easy and preffered way
#'Alien\'s planet' backslash escape sequence way
#Both works"I just taught Python how to lie" Tickles took a sigh.
Multi-Line Strings
There are times where we need to split our strings over multiple lines.
You may find that a lot of people incuding myself use multi-line strings as some sort of comment, as they look more readable than a standard comment.
Much like the one line strings, they also use the quotes, double or single quotes. But we use three start and end quotes for multi-line strings.
"""This is a multi line string we can
write whatever we want in here
"""'This is a multi line string we can\n write whatever we want in here \n'Depending on your system you may notice a \n character, which may dictate the newline character. Python stores the information about new lines by using the \n.
\n is just one character which means introduce a new line.
multi_line_string = """This is a multi line string we can
write whatever we want in here
"""
multi_line_string'This is a multi line string we can\n write whatever we want in here \n'print(multi_line_string)This is a multi line string we can
write whatever we want in here
multi2 = '''This is also a sample
multi line string but this time starting and ending
with three triple quote characters
'''
multi2'This is also a sample\n multi line string but this time starting and ending \n with three triple quote characters\n'print(multi2)This is also a sample
multi line string but this time starting and ending
with three triple quote characters
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →