1. A variable is a named location reserved to store values in the memory. A variable is created or initialized automatically when you assign a value to it for the first time. (2.1.4.1)
2. Each variable must have a unique name – an identifier. A legal identifier name must be a non-empty sequence of characters, must begin with the underscore(_
), or a letter, and it cannot be a Python keyword. The first character may be followed by underscores, letters, and digits. Identifiers in Python are case-sensitive. (2.1.4.1)
3. Python is a dynamically-typed language, which means you don’t need to declare variables in it. To assign values to variables, you can use a simple assignment operator in the form of the equal (=
) sign, i.e.,
1 |
var = 1 |
4. You can also use compound assignment operators (shortcut operators) to modify values assigned to variables, e.g.,
1 2 |
var += 1 var /= 5 * 2 |
5. You can assign new values to already existing variables using the assignment operator or one of the compound operators, e.g.:
1 2 3 4 5 6 7 8 |
var = 2 print(var) var = 3 print(var) var += 1 print(var) |
6. You can combine text and variables using the +
operator, and use the print()
function to output strings and variables, e.g.:
1 2 |
var = "007" print("Agent " + var) |
Shortcut operators
It’s time for the next set of operators that make a developer’s life easier. Very often, we want to use one and the same variable both to the right and left sides of the =
operator. For example, if we need to calculate a series of successive values of powers of 2, we may use a piece like this:
1 |
x = x * 2 |
You may use an expression like this if you can’t fall asleep and you’re trying to deal with it using some good, old-fashioned methods:
1 |
sheep = sheep + 1 |
Python offers you a shortened way of writing operations like these, which can be coded as follows:
1 2 |
x *= 2 sheep += 1 |
Let’s try to present a general description for these operations.
If op
is a two-argument operator (this is a very important condition) and the operator is used in the following context:
1 |
variable = variable op expression |
It can be simplified and shown as follows:
1 |
variable op= expression |
Take a look at the examples below. Make sure you understand them all.
1 2 3 4 5 6 7 8 9 |
i = i + 2 * j ⇒ i += 2 * j var = var / 2 ⇒ var /= 2 rem = rem % 10 ⇒ rem %= 10 j = j - (i + var + rem) ⇒ j -= (i + var + rem) x = x ** 2 ⇒ x **= 2 |
Swaping the values of two variables
Take a look at the snippet:
1 2 3 4 5 |
variable_1 = 1 variable_2 = 2 variable_2 = variable_1 variable_1 = variable_2 |
If you do something like this, you would lose the value previously stored in variable_2
. Changing the order of the assignments will not help. You need a third variable that serves as an auxiliary storage.
This is how you can do it:
1 2 3 4 5 |
variable_1 = 1 variable_2 = 2 auxiliary = variable_1 variable_1 = variable_2 variable_2 = auxiliary |
Python offers a more convenient way of doing the swap – take a look:
1 2 3 |
variable_1 = 1 variable_2 = 2 variable_1, variable_2 = variable_2, variable_1 |
Exercise 1
What is the output of the following snippet?
1 2 3 |
var = 2 var = 3 print(var) |
3
Exercise 2
Which of the following variable names are illegal in Python?
1 2 3 4 5 6 7 8 |
my_var m 101 averylongvariablename m101 m 101 Del del |
my_var
m
101 # incorrect (starts with a digit)
averylongvariablename
m101
m 101 # incorrect (contains a space)
Del
del # incorrect (is a keyword)
Exercise 3
What is the output of the following snippet?
1 2 3 |
a = '1' b = "1" print(a + b) |
11
Exercise 4
What is the output of the following snippet?
1 2 3 4 |
a = 6 b = 3 a /= 2 * b print(a) |
1.0
2 * b = 6
a = 6 → 6 / 6 = 1.0
Ex. 5
Which of the following variable names are illegal in Python?
(Select two answers)
False
print
_a&b
Print
Scenario
Miles and kilometers are units of length or distance.
Bearing in mind that 1
mile is equal to approximately 1.61
kilometers, complete the program in the editor so that it converts:
- miles to kilometers;
- kilometers to miles.
Do not change anything in the existing code. Write your code in the places indicated by ###
. Test your program with the data we’ve provided in the source code.
Pay particular attention to what is going on inside the print()
function. Analyze how we provide multiple arguments to the function, and how we output the expected data.
Note that some of the arguments inside the print()
function are strings (e.g., "miles is"
, whereas some other are variables (e.g., miles
).
1 2 3 4 5 6 7 8 |
kilometers = 12.25 miles = 7.38 miles_to_kilometers = 1.61 * miles kilometers_to_miles = kilometers / 1.61 print(miles, "miles is", round(miles_to_kilometers, 2), "kilometers") print(kilometers, "kilometers is", round(kilometers_to_miles, 2), "miles") |
Scenario
Take a look at the code in the editor: it reads a float
value, puts it into a variable named x
, and prints the value of a variable named y
. Your task is to complete the code in order to evaluate the following expression:
3x3 – 2x2 + 3x – 1
The result should be assigned to y
.
Remember that classical algebraic notation likes to omit the multiplication operator – you need to use it explicitly. Note how we change data type to make sure that x
is of type float
.
Keep your code clean and readable, and test it using the data we’ve provided, each time assigning it to the x
variable (by hardcoding it). Don’t be discouraged by any initial failures. Be persistent and inquisitive.
Test Data
Sample input
x = 0
x = 1
x = -1
Expected Output
y = -1.0
y = 3.0
y = -9.0
Solution:
1 2 3 4 5 6 |
x = -1 x = float(x) y = 3*x**3 - 2*x**2 + 3*x - 1 print("y =", y) |