Print Function and Variables in Python
Let's make our first steps in Python
The very first thing we will learn, is how to create a variable in Python.
Variables are like containers that store information for later use.
To create a variable in Python, we use the =
symbol.
Let's create our first variable:
num=10
This code will create a variable that's called num with a value of 10
You can name a variable as you wish. But, there are certain naming conventions to improve readability.
It is common practice to use lowercase letters. Also, if your variable name has more than one word, separate them with an underscore:
num=10
another_num=2
Also be sure to name your variables in a way that best describes the value they store. Doing so makes your code more understandable for others.
For example, a variable named price is more intuitive than a variable named x.
As mentioned before, variables store information for later use. Now, we will learn how to display the value of an already existing variable. We can do this with the print()
function.
Let's try it out:
num=10
print(num)
Great! When we run this code, the number 10 is displayed in the output window.
We will use the print()
function very often to test our code.
Once you have created a variable, you can always reassign a new value to it:
price = 10
print(price)
price = 20
print(price)
Great. Let's wrap this up with some questions.
What will be the output?
num = 10
print(num)
What will be the output?
num = 0
print(num)
print(10)
What will be the output?
number_one = 1
print(number_one)
What will be the output?
price = 1
price = 2
print(price)