Python Note 4
Python Note 4
30/12/2023
How to store the results of these operations, in order to use them in other
operations, and so on.
Python will help you with that. It offers special "boxes" (containers) for that
purpose, and these boxes are called variables - the name itself suggests that
the content of these containers can be varied in (almost) any way.
a name;
a value (the content of the container)
If you want to give a name to a variable, you must follow some strict rules:
Python does not impose restrictions on the length of variable names, but that
doesn't mean that a long variable name is always better than a short one.
Here are some correct, but not always convenient variable names:
Moreover, Python lets you use not only Latin letters but also characters
specific to languages that use other alphabets.
10t (does not begin with a letter), Exchange Rate (contains a space)
NOTE
The PEP 8 -- Style Guide for Python Code recommends the following naming
convention for variables and functions in Python:
Keywords
Take a look at the list of words that play a very special role in every Python
program.
They are called keywords or (more precisely) reserved keywords. They are
reserved because you mustn't use them as names: neither for your
variables, nor functions, nor any other named entities you want to create.
Fortunately, due to the fact that Python is case-sensitive, you can modify any
of these words by changing the case of any letter, thus creating a new word,
which is not reserved anymore.
import
You mustn't have a variable named in such a way - it is prohibited. But you
can do this instead:
Import
These words might be a mystery to you now, but you'll soon learn the
meaning of them.
Creating variables
You can use a variable to store any value of any of the already presented
kinds, and many more of the ones we haven't shown you yet.
The value of a variable is what you have put into it. It can vary as often as you
need or want. It can be an integer one moment, and a float a moment later,
eventually becoming a string.
Let's talk now about two important things - how variables are created,
and how to put values inside them (or rather - how to give or pass
values to them).
REMEMBER
The creation (or otherwise - its syntax) is extremely simple: just use the
name of the desired variable, then the equal sign (=) and the value
you want to put into the variable.
var = 1
print(var)
The first of them creates a variable named var , and assigns a literal
with an integer value equal to 1 .
The second prints the value of the newly created variable to the
console.
Note: print() has yet another side to it - it can handle variables too. Do you
know what the output of the snippet will be?
variable
How do you assign a new value to an already created variable? In the same
way. You just need to use the equal sign.
The equal sign is in fact an assignment operator. Although this may sound
strange, the operator has a simple syntax and unambiguous interpretation.
It assigns the value of its right argument to the left, while the right argument
may be an arbitrarily complex expression involving literals, operators and
already defined variables.
var = 1
print(var)
var = var + 1
print(var)
1
2
The first line of the snippet creates a new variable named var and
assigns 1 to it.
The third line assigns the same variable with the new value taken from
the variable itself, summed with 1 . Seeing a record like that, a mathematician
would probably protest - no value may be equal to itself plus one. This is a
contradiction. But Python treats the sign = not as equal to, but as assign a
value.
Take the current value of the variable var , add 1 to it and store the result in
the variable var .
In effect, the value of variable var has been incremented by one, which has
nothing to do with comparing the variable with any value.
Do you know what the output of the following snippet will be?
var = 100
var = 200 + 300
print(var)
500 - why? Well, first, the var variable is created and assigned a value of 100.
Then, the same variable is assigned a new value: the result of adding 200 to
300, which is 500.
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.
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:
sheep = sheep + 1
Python offers you a shortened way of writing operations like these, which can
be coded as follows:
x *= 2
sheep += 1
Take a look at the examples below. Make sure you understand them all.
i = i + 2 * j ⇒ i += 2 * j
x = x ** 2 ⇒ x **= 2
Key takeaways
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)
5. You can assign new values to already existing variables using the
assignment operator or one of the compound operators, e.g.: (2.1.4.5)
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.: (2.1.4.4)
var = "007"
print("Agent " + var)