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:
See example of arithmetic operators
Logical, isn't it?
Let's look at one more example then:
After running the program we will get the following error:
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:
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:
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"?
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
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.