1. Variables and Boxes
Variables are these special things for storing data. Like, any kind of data. All data in Python is stored using variables. A variable is kinda like a box. A regular, everyday box.
So, let's say you wrote down the number 13 on a piece of paper and put that piece of paper in a box. Now we can say “the box stores the value 13”.
Every variable in Python has two important properties: a name and a value.
The name is used to tell one variable apart from another. Kinda like a label on the box.
The value is some kind of object, data, or info stored inside the variable.
Every object in Python has its own type. For example, there might be types like “integer,” “float,” “text,” “cat,” “house,” and so on. However, a variable (box) itself doesn’t have a type. You can put an object of any type into the box. Same as in real life.
2. Creating Variables
In Python, there's no need to explicitly declare variables. You just write something like this:
name = value
The equals sign here isn't math's “equals.” It’s actually an assignment operator.
In other words, the equal symbol is like a command that says you gotta set (or assign) the value value to the variable named name.
Let's check out some examples:
| name = "Alexander" | Variable name contains a value — a string with the text “Alexander” |
|---|---|
| age = 35 | Variable age contains a value — an integer 35 |
| city = "London" | Variable city contains a value — a string with the text “London” |
| pi = 3.14 | Variable pi contains a value — a floating number 3.14 |
In Python you can assign any value to any variable. A variable doesn’t have a predefined type, only the type of the object it currently holds.
3. Expressions and Operators
On the left-hand side of the assignment sign, there gotta be a variable name. But the right-hand side can be an expression of any complexity.
| name = "Alex" + "Alex" | Variable name contains a value — a string with the text "AlexAlex" |
|---|---|
| age = 5 * 7 | Variable age contains a value — the number 35 |
| age = age * 2 + 3 | Variable age contains a value — the number 73 |
| age = age + 1 | Variable age contains a value — the number 74 |
You can join two strings using the "+" sign. This operation is called concatenation. But you can only join string with string. In languages like Java or JavaScript, you could "sum" a string and a number. Not in Python: you gotta explicitly convert the number to a string first and then "add" it to the string.
Also, notice that the variable age appears on both sides of the assignment operator. That’s 'cause assignment isn’t equality, like in math.
Let’s look at this command:
age = age + 1
It’s got two parts:
- Calculate the value of expression
age + 1, using the current value ofage; - Save the result of that calculation into the variable
age.
That command increases the value of the age variable by 1.
The order of operations is pretty much like math:
- First, do whatever's in the parentheses;
- Then go for multiplication and division;
- And at the end — addition and subtraction.
GO TO FULL VERSION