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.

Estimated Time

9 mins

Skill Level

Intermediate

Functions

n/a

Course Provider

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.

  1. Data usually tends to be bigger than a few characters
  2. Usually you’ll want to refer to your data multiple times in a program.

Used Where?

  • 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.

Syntax do(s)

1) While assigning a value, variable is always located on the left of the equation and data on the right of it.

2) You can start a variable name with a letter or underscore (_)

3) But if you want to print a variable this will have to be without quotes: print(mytext), more on this later.

Syntax don't(s)

1) You can't start a variable name with a number.

2) You can't use symbols (except underscore) in a variable name.

3)You can't include spaces in your variable name.

Example 1

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 digits and we could refer to it in 100 different codes. You can see that giving it a name by assigning it to a variable makes everything much more practical.

** Let’s try to understand the code above step by step.

  1. First, sun_to_earth variable gets created and then 149 is assigned to it as a value.
  2. A new value is assigned to sun_to_earth variable and now it is 150.
  3. print function is executed for our variable and as its value is currently 150 that get’s displayed on the screen.

Tips

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.
3- Practical naming conventions:
  • 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.

Example 2

>>> 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.

This is a perfect example to first lesson’s print function as both text and variable is used inside the print function. And variable names are probably nicely chosen. Keep in mind though variable naming is a subjective topic and you will have to be mindful about your own practices that work the best for you as you go along in your coding journey.

Advanced Concepts (Optional)

1- You can also assign multiple variables in one line. Let’s see some examples:

Example 3

>>> i, j, k = “Hello”, 55, 21.0765
>>> 

>>> print(i, j, k)

Hello 55 21.0765

This is a perfect example to first lesson’s print function as both text and variable is used inside the print function. And variable names are probably nicely chosen. Keep in mind though variable naming is a subjective topic and you will have to be mindful about your own practices that work the best for you as you go along in your coding journey.

Lesson 3

Data Types

Wheatstacks_(End_of_Summer),_1890-91_(190_Kb);_Oil_on_canvas,_60_x_100_cm_(23_5-8_x_39_3-8_in),_The_Art_Institute_of_Chicago


Claude Monet

Haystacks

Haystacks by Claude Monet

“Have you installed Python yet?”

Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.
Sandra Files