Arithmetic operators
Every programming language allows you to do arithmetic operations, and Python is no exception. Every basic calculation is written intuitively:
- the + sign denotes addition,
- the - sign denotes subtraction,
- the * sign denotes multiplication,
- the / sign denotes division.
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.
Increasing the value of a variable
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.
Text operators
What does your intuition tell you? Is it possible to add strings to one another? What happens when you add "Wars" to "Star"?
See example of text operators
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
Logical operators
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
|
Task 1
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
Task 2
Look at the code. You will find the following variables there:
- in_left_hand_i_have_a_pen = False
- in_left_hand_i_have_a_phone = True
- in_right_hand_i_have_a_mug = True
- in_right_hand_i_have_a_spoon = False
Then make appropriate logical operations and save their results to variables, then display the results on the screen:
- I have a pen in my left hand and a cup in my right hand.
- I have a cup in my right hand or a spoon in my right hand.
- I don't have a phone in my left hand.
Do the task
See the solution
Task 3
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>"
Do the task
See the solution
Task 4
Declare a variable named number, give it any numeric value. Try dividing the variable by zero. Read the error message.
Do the task
See the solution
Next chapter: Lists »