In the previous lesson, we used Python like a calculator and explored various arithmetic operators.
Wouldn’t it be great if we could save and re-use our calculations results? For this purpose we will use Python variables. In this lesson you will learn what a variable is and how to declare it.
A variable is used to store information that can be referenced later.
We use variables to name the result of a calculation we made. In other words, we can assign the result of that calculation to a variable. We can create an unlimited number of variables; we just have to make sure we give them unique names.
Creating a python variable is super easy. let's check if your REPL has a variable called result:
>>> result
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'result' is not defined
This is the way Python lets you know about errors. Ignore the first two lines and focus on the actual error instead. Python reports: name 'result' is not defined.
Python errors tend to be very helpful if you know where to look. You’ll eventually need to write code on your own and you are going to run into errors. Being able to decipher error messages is a useful skill!
Now let’s declare the variable name result and try this again:
Did you run these 2 lines of code?
What happened?
Python sees an assignment: we assign the result of 3 * 5 to the variable called result. Assignments are done with the = character. So we declared that result is the result of the expression 3 * 5.
In the previous example, we picked the general name result as our variable name, but you can choose any name you deem appropriate. Generally you should always pick a variable name that best describes its contents. This practice makes your code more readable and easy to understand.
If we were counting this sentence's total number of characters for example, a good name would be sentence_length.
Remember how we did math with number? You can do just as well with Python variables too, it's a crucial part of programing to use variables with expressions:
>>> time_passed_in_seconds = 20
>>> 3 * time_passed_in_seconds
60
>>> time_passed_in_seconds - time_passed_in_seconds
0
>>> _
An expression is basically anything that Python can evaluate to a value
These are all valid expressions because Python can evaluate them to a value:
What you see above the expressions are called comments. Anything that follows a hash symbol (the #) is seen as a Python comment and is ignored by the Python interpreter.
A value assigned to a variable does not have to be a number,
It can also be a string for example.
Python has several more data types, like booleans, tuples, lists and dictionaries; which can all be stored in variables.
Python has a built-in function called type(), which we can use to determine a variable or expression type.
Here are some examples:
>>> number = 4
>>> type(number)
<class 'int'>
>>> string = 'super man'
>>> type(string)
<class 'str'>
Some characters are not allowed in a variable name; there are a few rules we need to abide by.
valid characters
naming rules
valid examples
invalid examples
If your coming from a programing language like C# or Java, your probably used to name variables in the camel-case format. With camel-case, we use uppercase letters to separate words clearly.
While we can use camel-case in Python, we usually prefer 'snake-case' where we use underscores to separate the words of the variable name. Camel-case is only used to name classes in python. So instead of fullName, we pythonists use full_name.
I have gathered some basic challenges for you to go over.
These scripting tasks are perfect for beginners,
to practice variable naming and basic operations in Python.
Try to solve these and sweet sale in confidence onto the next lesson.
We learned how to deal with numbers and how to store numbers in variables. But what about text? In the next lesson we will learn all about Python strings.
Next: Strings