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:
# 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.")
# 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.
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:
# 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: 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)
# 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.
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.
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.
Go to task 2: Task 2
See solution: solution to task 2
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>"
Go to task three: Task 3
See solution: Solution to Task 3