Every programming language allows you to do arithmetic operations, and Python is no exception. Every basic calculation is written intuitively:
The above signs are called operators. Their use is very intuitive:
# We define a variable in which we store the current year
actual_year = 2018
# We define a variable in which we store the year of release
# of the first version of Python
python_year_of_birth = 1991
# And here we calculate how old Python already is
# To do that, we'll use the operator - (minus)
python_age = actual_year - python_year_of_birth
# What's the use of data if we can't read them?
print("Python is", python_age, "years old.")
See example of arithmetic operators
Logical, isn't it?
Let's look at one more example then:
# What will happen?
my_variable = 100 / "dear!"
After running the program we will get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'int' and 'str
What happened here?
Python tried to divide the number 100 by the string "dear!". Is it possible to calculate it? Of course not! So an error was reported. Read the error message carefully, namely its last line. It means that Python cannot use the division operator for int and str data.
A very interesting operator, available to the programmer is the operator %, or modulo. This is the remainder of a division. So if we divide 16 by 5, according to the rules of arithmetic, we get the result of 4 and remainder 1. In Python we can write this operation as follows:
# Calculating the modulo, which means dividing with remainder
my_val = 16 % 5
print(my_val)
The program will display the number 1 on the screen.
Imagine you're writing a computer game. When you shoot down an enemy ship in it, you get 100 points. This means that the number 100 should be added to your current score. How do I do this? It's very simple:
# Define a score
score = 0
# Add 100 points to your current score.
score = score + 100 # score is the name of the variable,
# In which we store the score of the game
print(score)
See example of increasing the value of a variable
Such notation means that the score variable is based on the current value of the score variable and 100 is added to it.
The same result can be achieved by using a shortened notation:
score += 100
It works the same way as the previous example. The += operator is the so-called increment operator. In addition to the increment operator, there is also the decrement operator -=, which subtracts a value from the current value of the variable.
What does your intuition tell you? Is it possible to add strings to one another? What happens when you add "Wars" to "Star"?
title = "Star" + "Wars"
print(title)
The result will be "StarWars". So you can!
And can you multiply a string by a number?
If you have the courage, try to execute the following code and see what happens:
See example: multiplying string by number
# at your own risk! ;-)
spell = "Bloody Mary " * 3
print(spell)
The computer supports logical operations, i.e. actions that result in a True or False value.
Sig | Description | Example | wynik działania |
---|---|---|---|
== | sign of equality | 2 == 5 | False |
!= | sign of inequality | 2 != 5 | True |
> | greater than | 1 > 6 | False |
< | less than | 1 < 6 | True |
>= | greater than or equal to | 2 >= 3 | False |
<= | less than or equal to | 2 <= 2 | True |
and | logical "and" | (2 == 2) and (3 > 2) | False |
or | logical "or" | (2 == 2) or (3 > 2) | True |
not | negation | !(2 == 3) | True |
Look at the previous chapter: we defined the product and its net price, saving them in the variables product and net_price respectively. Now calculate the gross price of the product at a VAT rate of 23% (or 0.23). Save the result in the gross_price variable. Then, using the print instruction, display the full product information: name, net and gross price.
Do the task Check the solution
Look at the code. You will find the following variables there:
Declare a numeric variable, give it any value that is a natural number. Then check if the number is even: if the result of dividing it modulo 2 is 0 - it is even, if 1 - it is not. Display a message that will contain a description of the operation: "<number> % 2 = <result>"
Declare a variable named number, give it any numeric value. Try dividing the variable by zero. Read the error message.