In Python, information is stored in variables. We have already interacted with variables before when getting inputs from the user.
name = input() surname = input() print(name, '--', surname)
This program reads two inputs. It stores the first input in a variable called
nameand the second input in a variable called
surname. In the case the person using the program inputs
Nikolafor the first input and then hit enter and put
Teslafor the second input and then hit enter the
namevariable will store
Nikolavalue in it and whenever printed will show
surnamevariable will store
Teslavalue in it and whenever printed will show
Variables can be used to store any type of information. They can store textual information as shown in the example above, they can store numbers, they can store collections, and other structures.
To store numeric values in variables we can do the following:
a = 10 b = 400 print(a, b, a * b)
The program will print
10 400 4000.
This program stores
10in a variable called
400in a variable called
b, and then prints their values along with the multiple of those two.
Write a program that stores
-20in a variable called
30in a variable called
100in a variable called
z. Print the result of the
x * y + zexpression.
So, we can use Python to do basic arithmetic operations and use it as a simple calculator.