Variables

Comments

Before we begin let us consider comments in Python. You must have noticed how I used the ‘#’ to convey certain information. When Python sees a ‘#’ it skips that line or part of code. We use comments to convey some information to those who read our code.

# This variable was obtained from database 'A'
# This value will used in the health module
user_sugar_level = 15

If we want to expand comments to multiple lines, we using triple quotes:

'''
This variable was obtained from database 'A'
This value will used in the health module
'''
user_sugar_level = 15

What are variables?

In the earlier blogs, we used variables quite often to store different values. Now let’s dive deeper into it, by exploring the inner workings of a variable, the different types of values a variable can hold, and understanding some features of a variable for Python.

Before we get started, we must bear in mind these two facts about Python:

  • The computer we use does not directly understand the code we write, but can only understand 0’s and 1’s. Thus, Python’s interpreter converts the code we write, into a language that is understandable by the computer.
  • The Python interpreter reads each line of code we write sequentially.

Let’s look at an example:

# variable_name = value_stored
integer_value = 5

print("We stored the integer: ", integer_value)

Here we assign the value ‘5’ to the variable ‘integer_value’. 

What is going to happen is that when the Python interpreter comes across this line of code, the interpreter allocates some memory where this value ‘5’ can be stored, and the same memory location will then be given a name/label (in this case ‘integer_value’). So now if we wish to access that value, all we need to do is just call the name that was assigned to it.

We can assign another value to the variable which was already assigned a value.

variable1 = 5
print("\nInitial value: ", variable1)

variable1 = 7
print("\nValue after the first modification: ", variable1)

variable1 = variable1 + 2
print("\nValue after the second modification: ", variable1)

When we execute this code, we get:

NOTE: If we wish to print a new line, we can use the escape character ‘\n’ instead of repetitiously using print(“”).

Here we see that any operation we perform on a variable name will affect the value stored in memory. The third operation adds 2 to the value stored in the variable name (which is 7) and then this computed value is saved in the memory location of ‘variable1’.

Consider another example, from the magical world of Harry Potter.

points_scored = 50

print("Hermione is excited as they got", points_scored, "points.")

print("\nDumbledore gives 5 points for Harry's phenomenal performance.")

points_scored += 5

print("\nHarry is ecstatic as Gryffindor now has:", points_scored, "points.")

Upon execution, we should get the following output:

Let’s take it apart. We first ask Python to store the value ‘50’ in a memory location with the name ‘points_scored’. We then display how excited Hermione is, and in the same line, we call ‘points_scored’ which gives us the value stored in the memory location with the same name. But Dumbledore is so impressed by Harry that he gives him 5 extra points. Now to add these points to our original score, we simply add the number of extra points to the name of the memory location, where our original number of points are stored. Finally, we then display Harry’s excitement with the value (Gryffindor’s total score) stored in the ‘points_scored’ memory location.

Tip:

  • You might be wondering about the line which has the code points_scored += 5.
  • This is a simpler expression of, points_scored = points_scored + 5

Properties of Python

Python is Dynamically Typed

Remember how in the first blog we addressed the simplicity of Python by contrasting it to C++?

                   

This is because Python is a dynamically typed language, whereas C++ is a statically typed language. When a language is dynamically typed, we do not have to explicitly state what is the data type of a value that we are going to store in a variable and we also can change the type of a variable at any given point (as shown below).

variable1 = 5
variable1 = "Verification Master"
print("variable1 now holds the value: ", variable1)

Python is Case Sensitive

Python is a case-sensitive programming language, so variables that differ by case are considered different.

a = 5
A = 15

print("a is: ", a)
print("A is: ", A)

Here ‘a’ and ‘A’ are considered different by Python and upon execution we see:

Let us explore an important aspect of programming and that is how to name a variable?

  • Remember to use descriptive names that are meaningful and convey exactly what value the variable holds.
    • To create variables names with multiple words, it is best practice to make them separated by an ‘_’.
  • Avoid using short names, or single-letter variables as these can confuse whoever reads your code.

Consider the following:

'''Let us assume we want to create a variable to store out blood group'''


bg = "O+" 
# this can confuse readers


bloodgroup = "O+" 
# this is hard to read (as it is not separated by an '_')


blood_group = "O+" 
# this is most preferred way to name a variable

What have we learned?

  • How is a value stored in a variable?
  • What is the role of a Python interpreter?
  • Which escape character provides a new line?
  • What makes a language a dynamically typed language?
  • What is the proper way to name your variable?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments