Before we talk about different data types, we need to talk about variables.
I like to think of a variable as a jar with a label on it.
In your math class, when you want to use x, you just write it down, and whoever reads it, understands that x is a variable.
Most programming languages require the programmer to declare a variable and assign a type.
This isn't so in python:
>>> x = 5I made a variable called x, and I assigned the value 5 into it. The weird >>> is the Python shell ... just ignore that!
So, what is a variable? We've covered this before, but let's think about it again.
A variable is a container with a label that holds a value.
Making a variable in Python is easy:
variable_name = some_valueWhat's in my variable?
To see the value my variable is holding, use the print() function.
>>> x = 5>>> print(x)5>>>Variables can be considered either as 'constant' or as 'variable' (as in changing). Python doesn't distinguish between the two as some languages do.
When we think of the freezing point of water, we know it's 0 degrees. It's not going to change unless we add something to the water. Therefore the freezing point of water is constant.
How does this work in Python?
Build a variable called freezing_point and set its value to 0:
freezing_point = 0print(freezing_point)In case you missed that: Python doesn't care if variables are constants or variable!
Python does have constant values you can use. These 2 examples come from the math module (or library):
math.pi - The mathematical constant π = 3.141592...
math.e - The mathematical constant e, aka Euler's Number = 2.718281...
What about variables that change, like your age?
Build a variable called my_age and set its value to 62:
my_age = 62print(my_age)1. Building variables in Python is easy:
variable_name = value2. Python doesn't care if variables are constants or variable, they are both built the same way:
variable_name = valuename = 'Bob'age = 16temperature = 22.5 I just built a variable to hold a name, an age and the air temperature.
Two things happen on each line:
The '=' is called the assignment operator.
age = 12 # Variable built properly12 = age # Variable NOT built properly. This will not workLike I mentioned earlier, in most other languages, you must include the variable's type. Because Python supports dynamic typing, so this isn't a concern for you. Because Python supports dynamic typing, it can alter a variable's type depending on how it's being used. This is a a powerful feature of Python, but you should know how to check the type.
So, what about Data Types? What are the different types of data that Python can hold?
Let's build some variables to see what we can get:
snacks_packed = 2distance_to_school = 3.5water_bottle_full = Falsecell_phone_brand = 'Android'Let's use the print() function to see what's in those variables:
print(snacks_packed)print(distance_to_school)print(water_bottle_full)print(cell_phone_brand)Looks like all of our values are there. But what TYPE are they?
We can use the type() function to let us know.
Build a variable called my_name and set its value to Bobo, then check the type:
my_name = 'Bobo'print(my_name)print(type(my_name))What are some types that you will need to know?
Let's check on the type of those previously made variables:
print(type(snacks_packed)) # Should be an intprint(type(distance_to_school)) # Should be a float print(type(water_bottle_full)) # Should be a booleanprint(type(cell_phone_brand)) # Should be a stringNOTE: If you call the type() from the Python interpreter, it will output the type. If you call type() from with in source code, you must wrap it in a print() to see the output.
I've said that variables are containers with labels. We can refer to that label as a name. There are a few rules for naming variables:
Can you get Python to hold the string Hello in a variable called message? Can you ask Python what type the variable message is? You're going to need the print() and type() functions.
message = 'Hello'print(message)print(type(message))