Variables can hold all different types of data. Here are some of the main primitive data types in Python:
int is an integer (number). float is a decimal number. str is a string (characters/words). bool is a binary value: true or false. None is a special type for no value!
Combining values
5 + 105.0 + 10'foo' + 'bar'
Often we want to combine two values into a single value.
We can add two numbers (ints or floats) with the + operator.
We can also concatenate two strings with the + operator.
Note: in each case, the operator returns the same data type as the two values it was given (more or less).
Boolean logic
TrueandTrueTrueandFalseTrueorFalseFalseorFalse
We can also combine two booleans into a single boolean!
and returns true if both values are True (or truthy).
or returns true if either value is True (or truthy).
Math
5 - 105.0 / 105 * 105 % 2
Numbers have many more arithmetic operators.
What do all the following do?
Once again, each of these operators takes two numbers and returns a single number.
Math
5 < 1010.0 > 55 >= 52 <= 55 == 510 != 5
We also might want to compare numbers to each other. We can do that with these comparrison operators.
These operators take two numbers and return what data type?
Comparing values
a = 'foo'
b = 'bar'print(a == b)
print(a != b)
The equality operators (== and !=) are also used for strings!
The equality operators return a boolean too. This is very useful for checking if a variable holds a certain value.
Comparing values
a = 'foo'
b = 'bar'print(a == b)
The equality operator (==) is also used for strings!
The equality operator returns a boolean. This is very useful for checking if a variable holds a certain value.
None
a = None
a isNone
None is a very special value. It represents a lack of value!
It's very important to keep track of missing data. It's also very import to check if a variable is None or not.
We can check if a variable is None with the is operator.
Not
a = None
a isnotNone
b = False
b isFalse
b isnotTrue
The not operator returns the opposite of what it is given.
is and not are also used for bool types (True and False).
Differences between == and is
The difference between == and is is subtle. We will return to this in coming lectures.
For now, remember to use is for bool and None types and == for str, int, and float types.
Review
What is a variable
Assigning and overwriting variables
Primitive data types (string, int, float, bool, None)
Operators (+, -, /, * , %, >, <, ==, !=, is, is not)