Data types operators and expressions
Estimated Time: 20 minutes
What you need to know about data types
- Python can store different types of data
- The basic types of data you’ll learn about first are:
- Strings
- Integers
- Floats
- Booleans
- Python can do different operations based on the data type
- like
+
to add numbers together
- like
Basic Data Types
Your name, age, date of birth, and photo are different pieces of data. Data, and the variables that refer to them, are represented by different types.
Every programming language has data types, but the built-in types are different from one programming language to another. In Python, we have more than 10 built-in data types. Here are the 3 types we need in this lesson:
- int (for integer)
- Represents integer numbers, like
1
,4
, or10
- Represents integer numbers, like
- float (a floating-point number)
- Represents floating-point numbers, like
1.0
,4.32
, or10.189
- You might know these as ‘decimal’ numbers or fractions - numbers that have a dot separating the integer part from the fractional part.
- Represents floating-point numbers, like
- str (for string)
- Stores text as character-based data like words, e.g.,
Hello World
- Stores text as character-based data like words, e.g.,
These are called primitive types. Python can represent more complicated data using compound types like List and Dictionary, which are made out of the primitive types. We’ll learn about some of those in later lessons.
Python figures out the type automatically based on some rules:
- numbers without a decimal point are treated as ints (like
10
) - numbers with a decimal point are treated as floats (like
1.0
) - text between quote marks is treated as strings (like
"Hello"
or'100.5'
)
There are more rules for other types, but we're skipping them for now.
Video: Data Types
Operators
Operators are symbols that represent computations like addition and multiplication.
+
performs addition-
performs subtraction*
performs multiplication/
performs division**
performs exponentiation%
performs remainder (or modulus) operation
What would happen?
Try to guess what each snippet would print out. Then, click the arrow to see an explanation.
print(5 + 10)
print(5 + 10)
It would print 15
.
First, python adds 5
and 10
, then it does the print
.
print(4 + 3 * 2)
print(4 + 3 * 2)
It would print 10
.
Python uses the same order of operations as school
arithmetic, so the *
comes before the +
.
print(10 ** 5)
print(10 ** 5)
It would print 100000
.
**
is the exponentiation operator, so it's as if Python did 10 * 10 * 10 * 10 * 10
.
print(12 % 10)
print(12 % 10)
It would print 2
.
The %
operation calculates the remainder. 12
divided by 10
has remainder 2
.
Expressions
An expression is a sequence of operands and operators, like 15+5
, that gets evaluated to a certain result.
Python doesn’t do everything all at once. It runs a program line by line, in order. Within a line, Python has an evaluation order that decides what comes first.
In the below code, the right hand side is evaluated before assignment.
age = 15 + 5
print(age) # 20
The 15 + 5
part of the line is called an “expression”. It’s a part of a line in Python.
Expressions get evaluated before assignment. So in the above code, 15 + 5
is evaluated to 20
, and then the value 20
is assigned to the variable age
.
Expressions also get evaluated before they get printed out in print()
:
print(10 / 2) # 5
Python does the 10 / 2
first, then the print()
Math operators and evaluation order
Python uses the same order of operations that arithmetic traditionally uses.
5 + 4 * 3 # 17 (not 27)
6 * 2 ** 2 # 24 (not 144)
This is the order for the math operators:
- Parenthesis
- Exponentiation (raising to the power)
- Multiplication/division/remainder
- Addition/subtraction
- Left to right to break any ties
You can use parentheses for grouping. They can change the order, or make it more obvious:
5 + (4 * 3) # 17
(5 + 4) * 3 # 27
6 * (2 ** 2) # 24
(6 * 2) ** 2 # 144
Parentheses are also used for calling functions like str()
or print()
, but it’s usually not that confusing to tell which is which.
Every opening paren needs a closing paren. You can’t do just (5 + 7
, it needs to be (5 + 7)
.
Practice: Working with operators
Solution Code (try for 5 minutes before peeking)
a = 15
b = 7
# Print the sum (+) of a and b (this one is done for you)
print(a + b)
# Print the difference (-) of a and b
print(a - b)
# Print the product (*) of a and b
print(a * b)
# Print the quotient (/) of a and b
print(a / b)
# Print the remainder (%) of a and b
print(a % b)
TypeError and Type Conversion
If you try to add a number to a string, Python will raise a TypeError
. It doesn’t know what you want to do. Do you want to concatenate the two, as if they are both strings? Or, add them together mathematically, as if they are numbers? Python refuses to guess.
age = "15" # a string
age + 5 # TypeError, tried to add a string and an int
To solve this error, you can convert between data types.
- Turn a string into an int with the function
int()
. - Turn an integer into a str with the function
str()
age = "15" # a string
int(age) + 5 # 20
age + str(5) # "155"