Basic types in python#
Variables in python can hold different types of numbers and also other things such as text, images and more complex objects.
We can also print out the type of a variable.
See also
This is an integer number:
a = 5
type(a)
int
And this is a floating point variable:
b = 3.5
type(b)
float
When combining variables of different types, Python makes a decision which type the new variable should have
c = a + b
type(c)
float
Strings#
Variables can also hold text. We call them a “string” in this case, and define them by surrounding the value with either single quotes ' '
or double quotes " "
:
first_name = "Robert"
last_name = 'Haase'
Strings can be concatenated using the +
operator:
first_name + last_name
'RobertHaase'
first_name + " " + last_name
'Robert Haase'
If we want to have single and double quotation marks within our text, we can put them in like this:
text = "She said 'Hi'."
print(text)
She said 'Hi'.
text = 'He said "How are you?".'
print(text)
He said "How are you?".
Combining strings and numbers#
When combining variables of numeric types and string types, errors may appear:
first_name + a
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [12], in <cell line: 1>()
----> 1 first_name + a
TypeError: can only concatenate str (not "int") to str
Those can be prevented by converting the numeric variable to a string type using the str()
function:
first_name + str(a)
'Robert5'
You can also convert strings to numbers in case they contain numbers:
d = "5"
int(d)
5
a + int(d)
10
If the string does not contain a number, an error message may appear:
int("hello")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [17], in <cell line: 1>()
----> 1 int("hello")
ValueError: invalid literal for int() with base 10: 'hello'
int("")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [18], in <cell line: 1>()
----> 1 int("")
ValueError: invalid literal for int() with base 10: ''
int("5")
5
f-strings#
Instead of having to manually convert numbers to strings in order to assemble them with other strings, we can resort to f-strings which are defined by simply adding an f
before the opening quote of a regular string:
f"This is an f-string"
'This is an f-string'
We can now add variables directly in this text by surrounding them with curly brackets:
f"This is an f-string. a's value is {a}. Doubling the value of a gives {2*a}."
"This is an f-string. a's value is 5. Doubling the value of a gives 10."
As you can see above, f-strings can contain as many variables as needed and curly brackets can contain more than just a variable. We can even execute functions inside them:
f"The first_name variable contains {first_name.lower().count('r')} r letters."
'The first_name variable contains 2 r letters.'
Exercise#
Marie Curie’s name and birthdate are stored in variables. Concatenate them in one string variable and print it out. The output should be “Marie Curie, * 7 November 1867”
first_name = "Marie"
last_name = "Curie"
birthday_day = 7
birthday_month = "November"
birthday_year = 1867