Python Variables

Python Variables

Before we delve into what Python variables are we would take a general life approach to understand what ordinary variables are; because by this method you will be able to effectively understand every concept that would be outlined in this article. This article will cover all you need to know about Python variables, and how to assign and call out variables.

What are Python Variables?

Variables are storage locations for our piece of data. Let’s use a simple scenario to better understand this.

In a kitchen, there are jars and containers that hold food and ingredients right? The pepper has a container which we would call a pepper variable, the salt also has a container which we would tag as the salt variable. When cooking we retrieve these containers and in this case “variables” to spice up our meals right?

This is exactly how a variable in Python works; we store up ingredients but in this case “values” to be used up in our calculations or code later on.

Let’s take our first example:

Johnny buys four drinks and 2 packs of food. Representing this as a variable in Python we write:

drinks = 4

food = 2

Can you tell me the variables in this illustration…inserts brief pause\**

You guessed it right food and drinks.

Modifying Python Variables

In the previous section, we cleared out the puzzling concept of python variables; In this section, we would venture deeper into performing more operations and tweaking our variables.

Building on our previous case study of a pepper and salt container— in our words “variables”—let’s assume we suddenly ran out of salt and decided to use the salt container to store let’s say sugar (don’t beat me up it's just a case study).

Now that variable is still called a salt container but now it holds sugar, so when directing someone in our kitchen we say look in the salt container you will see the sugar.

Coming back to our grocery-themed python code, let’s say Chizoba got one more drink on her way from the store; the total number of drinks will be five.

drinks = 4

food = 2

drinks = 5 (the new value of drinks)

Now, did you notice the change in the variable's value after an additional drink? Meaning we now have a total number of 5 drinks. This signifies a variable data can be later modified and that new data will now be used in place of the former data.

· The Equal to Sign ‘=’

Let’s discuss something we probably could have missed in our variable definition, the equal to sign “=”. In math, we always say this means equal to but in python, it is used to assign value to an item.

Let’s retrace back to the salt and pepper container illustration, the salt container was assigned to contain salt and the pepper container to contain pepper. But if you remember along the line we ran out of salt so we assigned that salt container to now contain sugar. This in all ramifications shows that the sign “=” in python is used to assign values.

However the equal to sign in python is represented this way”==” and this differentiates it from the other “=”, don’t bother about this now as we would cover this in later articles.

Basic Variable Operations

Let’s take a little example

x = 2.5

y = 4.7

Here we just defined these variables, now we can employ these variables in a math summation.

x+y = 2.5 + 4.7 = 7.2

Now a little brain work for you

What if later on I declared x = 3, what would x +y be…

Remember what we discussed in modifying variables, x will now take on the new value assigned to it, completely erasing the previous figure

x = 2.5

y = 4.7

x = 3

x+y = 3 + 4.7 = 7.7

Now, do you see how this works?

Naming Python Variables

When naming your variables it is very paramount to be cautious and use variable names that appropriately represent an item you are naming.

Look at this funny case, let’s say I have 5 cars and I want to assign a variable to hold this information.

Now I represent this as

richest = 5

At this point, I am well aware that this richest means car, but what happens when I come back to this code after a month? I would have no idea what richest stands for, so do well to name your variables well.

This would now lead us to a very important section

Rules In writing Variable Names

No No No!!!!! I shall not stand down and be disrespected because of my easy syntax and user-friendliness.

Believe me this isn’t me talking, this is just Python throwing that error message in your face because you flouted some of its rules. So let’s go over some of the actions Python will not tolerate.

1. Using Special/Reserved functions as Variable Names:

There are some functions used to “perform wonders” when coding in python. We call these functions special or reserved functions. Let’s take a look at the print function. This function shows you the “fruit of your labor” I like to say. The print function displays the result of your calculations and inputs on the terminal of your IDE (Integrated Development Environment).

However, when you use this as a variable name, you are disrupting python’s stipulated flow because the print function is configured to print your result.

Now you understand why it is a grave sin to use special/reserved functions when writing your code. Some of these functions with their color theme are:

False, class, finally, lambda, None, continue, return, if, True, is, for, def, from, not, while, with, as, and, elif, del, try, global, or, yield, pass, assert, break, else, except, import, in, raise.

2. Starting a Variable Name with a Number or Dot

Python frowns at any programmer who starts a variable name with a number, dot or a “#” symbol. Though Python is praised for its easy syntax some inputs are not acceptable.

E.g.

23bread = 5

 #123 = 10

.fred = 50

DO NOT THIS!

3. Using Hyphens in your Variable Name

This is unacceptable, refrain from using hyphens, if you have a long compound name use the underscore:

lamborghini-race-cars = 67 (WRONG!)

lamborghini_race_cars = 67 (RIGHT!)

Some built-in python attributes and methods use the hyphen to separate their names so you wouldn’t want to disrupt python's flow by using hyphens in your variable names.

4. Use Underscore, Letters and Alphanumeric Values

Use variables names like

chizoba = 45

alpha_123 = 45

alexander = “girl”

got_a_car = True

I am sure you saw the last one and you were about to draw your sword on me ha-ha! The “False and True” keyword fall under Boolean values and were in the value spot not in the variable spot. Your values can be words, numbers, Boolean values and even None. Don’t worry, with time you will understand this better.

Calling out Python Variables

I know I have touched on this a bit in the previous sections, but to make it official let’s give it a proper section. Never forget we store variables so we can use them in later cases.

Using a familiar topic we all like: Money. In this example we will take some names and weigh the amount of money they have:

Gerald =1000

Matthew = 2000

Tunde = 5000

Total_money = Gerald + Matthew + Tunde #This line of code calls out the variable names and performs a summation operation on their values)

print(total_money)

Output = 8000

Now let us modify this piece of information by inputting Gerald with 4000 below

Gerald =1000

Matthew = 2000

Tunde = 5000

Gerald = 4000

#Now the Gerald variable will automatically erase the previous value and assume a new value, so when called out

total_money = Gerald + Matthew + Tunde

= 4000 + 2000 + 5000

print(total_money)

Output = 11000

Now, this should explain what this section is, when variable names are used to store data you can call it out anywhere in your code by simply typing the exact variable name. You can also modify your variable's value just like what we have been doing in the previous sections.

Let’s have a little brain teaser

Gerald = 1000

Matthew = 9000

Tunde = 3000

Gerald = Gerald +1000

Matthew = Matthew + Matthew

Tunde = Tunde/2

print( Gerald + Matthew + Tunde)

Write your answer in the comment section.


Like, comment, share and follow if you enjoyed this article.

Don't forget to follow me on all my socials.

Twitter: Chizobaonorh_

LinkedIn: Chizoba Ononlememen

Email: Rewardonorh@gmail.com