Theory
What is the variable?
- Objects (text, numbers, complex data structures) with data are stored inside computer memory.
- In the real world we use a post address when we need to visit some place or a building.
- Computers also use addresses to get access to a particular object in memory and it looks like this
0x00000284DE5A3E20
- A variable is simply a name you give to a memory address where an object is stored.
- It’s much simpler to remember that your data is located in a variable with name
data
rather than0x00000284DE5A3E20
We use the equals sign “=” between variable name and an object we want to give this name to.
Example #1
Let’s now use a variable in our first ‘hello world’ program, for now we simply give it the name “my_variable”.
1
2
my_variable = "hello world"
print(my_variable)
Here is what happens:
- We create a new variable and assign a string data to it.
- The print statement looks what memory address variable represents.
- The print statement then fetches the text data in that memory location and outputs it into the terminal.
Python Print function with variable example visualized
Example #2
Let’s also modify ‘enter your name’ program and use a variable to store the data a user enters:
1
2
your_name = input("enter your name: ")
print(your_name)
Here is what happens:
- We create a new variable
your_name
input("enter your name: ")
asks user to enter name in the Terminal.- As soon as user enters it and hits Enter key, input() sends this text to the
your_name
variable. - The user’s name is now stored inside this variable.
- The
print
statement then fetches the text data stored in an address represented byyour_name
and outputs it into the terminal.
Python Print and Input functions function with variable example visualized
When a user enters their name it’s going to be stored as string.
Practice.
Let’s now start writing our calculator.
- Re-use the code we wrote in the previous Step
- We want a user to enter two numbers and then print them in the terminal.
- Create first variable which will represent first number and read this data from the Terminal using input() function.
- Similarly, create second variable which will represent second number and read this data from the Terminal.
- Print both numbers using one print statement.
- The result should look like this:
1 2 3
enter first number: 70 enter second number: 7 70 7
Solution
1
2
3
number_1 = input('enter first number: ')
number_2 = input('enter second number: ')
print(number_1, number_2)