Variables
Python infers the type of your variables by looking at the right hand side of the equals sign.
a = "Python"
b = 10
c = 10.25
d = [10, 20, 30, 40]
In the example above, a is a string (a sequence of characters), b is an integer, c is a floating point, and d is a list of integers.
Notice the syntax. A string is a sequence of characters in between double quotes " " or single quotes ' '. An integer does not contain a decimal point. A floating point number contains a decimal point 10.0, 10.2, 10.355, and a list contains a primitive data type separated by commas in between square brackets [ ].
NOTE: it is not very good practice to name your variables a, b, c, d, etc. Instead, you should give them good names, such as name_of_human = "Hello". It makes reading your code much easier.
Variables must be declared before they can be used. For example,
# The following will cause an error since a is not declared
print(a)
When you run the code above, you'll get the following error message, which says that the variable a above has not been defined before you tried to use it.
Traceback (most recent call last):
File "example.py", line 1, in <module>
print(a)
NameError: name 'a' is not defined
To fix the problem with the code above, we declare a variable called a and set it to the value "Hello".
a = "Hello"
print(a)
When you run the code above, you will see the following:
Hello
Data Types
Variables in Python has a certain type. This makes sure that the given variable is calculated in the proper CPU unit. For example, the CPU has an arithmetic unit that only calculates integers. If we tried to put a real number in this unit, it couldn't handle the decimal point.
| Type | Name | Stores | Example |
|---|---|---|---|
| int | integer | whole numbers only | 10, 20, 30 |
| float | floating-point | real numbers | 10.5, 20.2, 3.1615 |
| str | string | any sequence of alpha-numeric characters | "a", "python", 'hello', 'this string contains 34 characters ' |
| list | list | any grouping of homogeneous or heterogenous data types | ['a', 'group', 'of', 'strings', 10, 25], [0, 1, 2, 3] |
| set | unordered set | unique values unordered | {10, 20, 30, 40} |
| dict | dictionary | a key/value pair | {"animal": "tiger", "age": 34} |
| tuple | tuple | an immutable list | (1, "animal", 100, 77.2) |
| bool | Boolean | A value of True or False | True, False |
# Dictionaries store a key/value pair, but the syntax looks similar to a set.
# If you see a : inside of the curly braces { }, then it's a dictionary, otherwise
# it's a set.
# Dictionaries are created using curly braces { }
animal = {"type": "tiger", "age": 34}
# You can get the value by referring to the key in square brackets [ ]
print(animal["type"], animal["age"])
The animal map looks like the following:
| key | value |
|---|---|
| "type" | "animal" |
| "age" | 34 |
Notice that the key can be any data type, and the value can be any data type. You can even mix them, as I did above. Both keys are string data types. For the values, "animal" is a string, but 34 is an integer. So, this shows that you can mix data types.
The code should print the following:
tiger 34
Literals
Literals are like constants in mathematics. They are literally the value we specify. Examples of a literal may be something like:
# Integer literals have no decimal point
100
# Floating point literals contain a decimal
10.2
# String literals have single (') or double quotes (")
"Hello World"
# Boolean literals are either True or False (notice capital T or F)
True
False
It is important to know what data type literals are because it sets the data type of a variable. For example, the following code creates a variable called my_var. Since we don't explicitly set a data type, Python infers what it is. In this case, my_var is the data type bool.
my_var = True
Stephen Marz (2019)