10.1 Printing Text
Let's print something else. For example, your name and year of birth. To do this, just write two more calls to the print() function. It'll look something like this:
print("Alexander")
print(1985)
Numbers can be printed without quotation marks. The quotes are a part of the text, not the print() function itself.
Here's what I got:
10.2 Creating Variables
As you probably know, many programming languages have variables. Python is no exception.
Let's define it: a variable is a special spot in memory that can store some data. In Python, variables have a name and a value.
You can think of a variable as a box with a label — its name. And the contents of the box — that's the value of the variable.
In Python, you don’t need to declare variables in any special way. Just write something like:
name = value
The equal sign here is not the same as equality in math. It's an assignment operator.
In other words, the "equals" symbol is a command telling us to set (assign) the variable name to the value value.
Here's a few examples:
| name = "Alexander" age = 35 city = "London" pi = 3.14 |
The variable name contains the value — a string "Alexander". The variable age contains the value — the number 35. The variable city contains the value — a string "London". The variable pi contains the value — the float number 3.14. |
In Python, any variable can hold any type of value. A variable doesn't have a predefined type — only the type of the object it currently holds.
10.3 Expressions and Operators
The left side of the assignment operator must always be a variable name. But on the right side, you can have an expression of any complexity.
| name = "Alex" + "Alex" age = 5 * 7 age = age * 2 + 3 age = age + 1 |
The variable name contains the value — the string "AlexAlex". The variable age contains the value — the number 35. The variable age contains the value — the number 73. The variable age contains the value — the number 74. |
In the first example, we combined two strings. Yep, in Python, you can join two strings together using the "+" operator. This operation is called concatenation. You can only concatenate a string with another string. In languages like Java or JavaScript, you can "add" a string and a number (and get a new string as a result). In Python, you can't do that. You'll need to explicitly convert the number to a string before "adding" it to another string.
Now, check out the third and fourth examples: here, the variable age appears on both sides of the assignment operator. That's because this isn't equality, like in math.
Let's break down this command:
age = age + 1
It has two parts:
- Calculate the value of the expression age + 1, using the current value of age.
- Store the result in the variable age.
This command increases the value of the variable age by 1.
The order of operations is the same as in math:
- First, operations in parentheses are executed.
- Then comes multiplication and division.
- And finally — addition and subtraction.
GO TO FULL VERSION