Python Variables (A fundamental concept of coding)
Variables contain data. So, you can call them later. This makes data reusable, recallable, reassignable etc.
Before we advance with additional functions and lessons, understanding data types will help you grasp the programming logic.
But even before that, it’s crucial to understand variables. Programmers coming from other languages would already know, but if you’re a newbie programmer you may not yet have had the chance to truly understand variables. In that case this lesson will help you.
Provided by HolyPython.com
Simplest definition of a variable is a “named container” of data you’d like to refer to in your program. There are 2 main reasons you may want to do this.
- Data usually tends to be bigger than a few characters. Imagine calling books by their whole content (possibly 100s of pages) each time you refer to them instead of their names. Amazon would never take off. 🙂
- Usually you’ll want to refer to your data multiple times in a program. So it helps to have it in a convenient container and call it whenever we need to such as when we want to print it.
- It’s hard to imagine programming without variables. It would be like referring to humans with their features and not using first or last names. As you can imagine it’d get confusing pretty quickly.
- Similarly in programming, we use variables to identify and refer to data of different sizes, types and shapes.
Let’s say you have a big number you’re working with at hand: 149,597,870. This number denotes the average distance between sun and our planet earth in kilometers.
Let’s say you’re planning to refer to this number in quite a few calculations. Instead of typing it every time you can instead assign it to a variable and just refer to the variable itself. Take a look:
line 1 >>> sun_to_earth = 149
line 2 >>> sun_to_earth = sun_to_earth + 1
line 3 >>> print (sun_to_earth)
output >>> 150
Our data could be way longer than this example. It could have 100 more lines. You can see that assigning it to a variable and giving it a name makes everything much more practical. This is a creation process. You can congratulate yourself.
* Let’s try to understand the code above step by step.
- First, sun_to_earth variable gets created and then 149 is assigned to it as a value. Realize assignee is always on the left side of the equal* sign.
- A new value is assigned to sun_to_earth variable and now it is 150.
- print function is executed for our variable and as its value is currently 150 that get’s displayed on the screen.
1- As in the example above, variable name is always located to the left of the equation during the assignment.
So, the variable goes on the left side and value being assigned goes on the right side.
2- Choosing a variable name is a somewhat strategic decision in Python (and programming in general) and it’s wise to give it some thought since you might refer to this name a ton of times in the future.
- First off, some names are reserved in Python so you can not use these such as: print, true, false, count, sum etc. In short, if it seems like a keyword in Python you probably shouldn’t be using it as your variable name.
- variable names have to start with a letter or underscore(_) .
- You can include numbers, if you started your variable name correctly with a letter or underscore.
- Variable name is case sensitive. If you used a capital letter, that’s exactly how you should refer to it later on.
- You can include a little suffix of your choice so you don’t coincide with a Python keyword accidentally. i.e.: var_number , var_true , v_start , v_close, the_class, the_one, the_name. etc. You can be creative, just try to have a meaningful system.
- Try not to use one letter variable names unless you’re messing around or doing a quick and dirty job that doesn’t matter too much. Names such as a, x, y, z can get confusing for you only after 2-3 mins from naming your variable. While thinking about how to code or what to calculate you’ll forget what you named x and what you named y and so on.
- Another alternative to including underscore in your variable name is including capital letters. i.e.: carsCounted , doorsClosed , dogsSaved , shipsFixed etc. It can also be useful to include meaningful expressions rather than some confusing words as above examples.
>>> dogsName = “d’Artagnan”
>>> dogsKind = “Saint Bernard”
>>> print(“His name is ” , dogsName , “.” , ” He is a ” , dogsKind, “.”)
His name is d’Artagnan. He is a Saint Bernard.
Could you catch what’s happening in this example? We are printing some values directly inside the print function while we are also printing other values through variables which were assigned priorly.
Although we will see more about different data types in Python in next lesson you might want to make sure variable usage in this example is clear. You can take your time and also check out the Python variable exercises we have prepared for you.
Also variable names are case sensitive as you’ve probably noticed.
Advanced Concepts (Optional)
1- You can also assign multiple variables in one line in Python.
Here is a variable example with print function:
>>> i, j, k = “Hello”, 55, 21.0765
>>> print(i, j, k)
Hello 55 21.0765
This is a perfect Python example as it bridges first three Python lessons. In first lesson we have seen the print function and here we are printing three Python variables at the same time.
Here data seems like text, number and number with decimals. In next lesson we will see how Python handles these different data types specifically. We will name them string, integer and float. But it’s more than just naming. They have their own Python methods and own way of existing.