Variables

When you are writing programs using any programming language, you will come across a term called variable very frequently. Think of a variable as a named container for any data you might have. Notice the named container part. Let’s look at few examples below,

1
2
3
4
5
6
7
8
9
10
11
12
product_name = "Samsung Monitor"
unit_price = 555.99
quantity = 2

# print the value of the "product_name" variable
print(product_name)
"Samsung Monitor"

# print total cost by multiplying unit_price and quantity
total_cost = unit_price * quantity
print(total_cost)
1111.98

In the above example, we defined or created 4 variables. When you create a variable, you need to give it a name as well as some value it will hold. Name of the variable is in the left hand side of the “equation” and the value of that variable is in the right hand side. The values of the variable can be assigned or set in multiple ways. In the above examples, we assigned the values of first five variables ourselves. This is also called hard-coding the values which basically means that in your program the unit_price is always 555.99.

But if you look at the total_cost, its value is calculated by multiplying values of unit_price and quantity. We do not care what the values of unit_price and quantity are but we know that to calculate total cost we need to multiply them together. This is fundamental to programming where we do computation on data that we gather. Here we have “gathered” the data needed to calculate the total cost by hard-coding the values of the variables but in real world application, you would have to get these values from a database or via user input in a web page or in an application.

Let’s break down our example one by one. In the line where we created a variable called product_name as product_name= "Samsung Monitor", we told Python that we want to store the text (also called string in programming lingo) into a variable called product_name. Later on we “use” this variable by telling Python to display its value using the print function as: print(product_name). Python will see that we want to print the value of product_name variable and will display “Samsung Monitor”.

Since the value of this variable is a string, we also call this variable a string variable because it stores some string. This “naming convention” used for communicating with other developers or even yourself when there are problems or questions like “Hey I have a string variable to store user’s comment, how do I remove foul words from its content? :thinking:”. It helps others to know what kind of data you are working with and what you need to do in order to solve the problem.

Similarly, we also created two other variables, unit_price and quantity with their values 555.99 and 2 respectively. In unit_price, it contains a value 555.99 which is a real number i.e. it contains digits with a decimal separator. In Python, real numbers are stored as float so we can also call this variable a float variable. With quantity, the value it hold is a number 2 which is a whole number and is stored as int (short for integer). So you’ve probably guessed it, quantity is called integer variable.

We will discuss more about data types like string, float, integer, list etc. in later posts.

So far, all our variables have same values but their value can change over time. This can happen due to some event like user inputs. Try this example

1
2
3
4
name = input("Enter name ")
print("value of name is ", name)
name = input("Enter name again ")
print("but now the value of name is", name)

You will see something like this

1
2
3
4
Enter name python
value of name is  python
Enter name again java
but now the value of name is java

As you can see from the output, Python asked us to enter a name which is then stored in the variable called name and then we tell Python to print the value of name. But Python again asks us to enter a name which is then again assigned to name. This means, the previous value “python” is replaced with the new value “java” and when we ask Python to print the value, it prints the latest value.

So, a variable’s value can be changed multiple times and it will only keep the latest value. Any values that were previously assigned to it will be completely lost.

Naming conventions

There are some rules that you have to follow when naming variables in Python. I’ll list some of them here

  • :heavy_check_mark: You can use any alpha-numeric characters i.e. any letters and digits, and underscore (_) character. But only letters or underscore is allowed as the first character of the name. e.g. name, Name, nAme, name_of_person, nameOfPerson, names_of_5_people, __________weird_variable_name are all valid names of a variable
  • :x: It cannot start with numbers or special characters e.g. 2_prices = [33.99, 1.50], $name="John"
  • :x: It cannot contain space e.g. name with space = "Kelly"

Apart from these rules, there are conventions that you should follow when naming your variables. In Python community, snake_case is used to write names of functions and variables and PascalCase to write names of classes. Different programming languages have different conventions of naming things and are quite different from one another but I will show you some commonly used patterns in Python community.

  • variables names should be in lower case
    • :+1: name, price, quantity
    • :-1: Name, nAme, Price
  • when you need to use multiple words to describe the purpose of a variable, they should ideally be separated by underscore “_” for easily finding out word boundaries but if there are more than two words it is highly recommended to use it otherwise it might be difficult to read
    • :+1: shipping_date, shippingdate, on_button_click
    • :-1: shippingDate, Shippingdate, ShippingDate, onbuttonclick

Although I haven’t introduced you to functions and classes in Python, I will list the conventions used here for them as well. For naming functions, the same conventions and rules of naming variables apply. However for classes it is a bit different.

  • name of classes should be written in PascalCase i.e. the first letter of each word should be in upper case
    • :+1: Customer, Product,WebClient
    • :-1: customer, product, web_client

There are other conventions as well but this should get you started. Each development team will have their own conventions and is better to follow their guidelines. For more details check this style guide from the creator of Python himself. It contains not only the naming conventions but also other coding guidelines when using Python. If you are only interested in the naming guidelines then click here.

Exercise

Take the following example. It will print the total cost to be 2999.9 but we would also like to add shipping cost of 15 dollars to the total cost. Can you do that? Refer to the formula below to calculate the total cost. \(TotalCost = UnitPrice * Quantity + ShippingCost\)

1
2
3
4
5
unit_price = 299.99
quantity = 10

total_cost = unit_price * quantity
print("Total cost is ", total_cost)

After you add the shipping cost, the total price should be 3014.9.

Updated:

Comments