Variables, expressions and operators
Using Python, we meet the basic building blocks of programs — variables, data types, expressions and operators — and find out why some common calculation errors happen.
All topics (2 of 3)
Chapter 1. Introduction to information technology
Chapter 2. Programming fundamentals
A variable is a name given to a value in the program’s memory. In Python it is created with an assignment statement using =: x = 5. Here = does not mean equality; it is a command: “bind the value on the right to the name on the left”. Later the variable can be given another value: the statement x = x + 1 increases its value by one.
Every value has a type: integer (int), floating-point number (float), string (str), Boolean (bool); the type can be found with type(x). In Python the type is determined by the value (dynamic typing), so after x = 5 you may write x = "abc". A name starts with a letter or an underscore and consists only of letters, digits and _; case matters (count and Count are different names). The input() function always returns a string, so a number must be converted with int() or float().
An expression is a combination of values, variables and operators that evaluates to a single value: 2 * (x + 3). The arithmetic operators are +, -, *, / (division; the result is always a float), // (the integer part) and % (the remainder). For example, 7 / 2 equals 3.5, 7 // 2 equals 3 and 7 % 2 equals 1. The remainder operator is used very often: the condition n % 2 == 0 checks whether a number is even.
Comparison operators (==, !=, <, >, <=, >=) return a bool value, and the logical operators and, or, not combine them. Operations are performed by precedence: first parentheses, then *, /, //, %, then +, -, so 2 + 3 * 4 equals 14 and (2 + 3) * 4 equals 20. Floating-point numbers are stored approximately in binary, so the result of 0.1 + 0.2 is not exactly 0.3 but 0.30000000000000004; avoid comparing floating-point numbers with ==.
Key takeaways
- A variable is a name given to a value; in Python it is created with
=and can later be bound to another value. - The main types are
int,float,strandbool;input()returns a string, so convert it withint()orfloat(). - An expression evaluates to one value using operators;
/always gives afloat,//the integer part and%the remainder. - Comparison operators return
bool, andand,or,notcombine them. - Precedence affects the result, so use parentheses for clarity; floating-point numbers are stored approximately.
Practice tasks
- Swap the values of the variables
aandbin two ways: with the statementa, b = b, aand with a third variable; compare the results. - Ask the user for a number of seconds with
input()and split it into hours, minutes and seconds using the//and%operators; for example, 3725 seconds = 1 hour 2 minutes 5 seconds. - Predict the values of these expressions before running the program, then check them in Python:
2 + 3 * 4 % 5,17 // 5 + 17 % 5,not (5 > 3) or 2 == 2,0.1 + 0.2 == 0.3.
Review questions
Test yourself: think first, then reveal the answer.
1What do the expressions `7 // 2` and `7 % 2` equal?Show answerHide answer
`3` and `1`: the integer part and the remainder.2What is the difference between the `=` and `==` operators?Show answerHide answer
`=` assigns a value to a name, while `==` checks whether two values are equal and returns a `bool`.3Why does the expression `0.1 + 0.2 == 0.3` give `False`?Show answerHide answer
The numbers 0.1 and 0.2 cannot be represented exactly in binary, so the sum is approximate (`0.30000000000000004`).
0 of 3 topics completed
Sign in to mark