Wednesday, November 8, 2017

VARIABLE

In today episode on our python tutorial, we will be talking about the different kind of data we can work with in our python program. We will learn how to store data in variables and how to use variables in our program.

The word variable is the same across all programming language. A variable is a named memory location. All computer programs interact with our computer memory. Think of memory as an empty room where you would like to keep different boxes of the same shape, size and color. If we store different things in the boxes, it will be hard for us to know which box contains which element. Therefore, we need to name our boxes. Giving names to the boxes is called variable naming.
Let take a closer look at the first program we wrote, the hello_world.py program. We can declare a variable to store the message “Hello World!” instead of printing the message directly. Open the program hello_world.py in our text editor and do the following:

message = “Hello World!”

print (message)

NOTE: unlike most other programming language, we don’t use semicolon (;) to end our line of code.

In our new program, we have created the variable “message” which store the “Hello World!” and on the second line, we printed the message. One thing about a variable is that, once created, it can be used throughout the program without the need to declare them again.

We can create as much variable as we want in our program, but we can use one variable multiple times. Let add another line to print another message. Add the following to the next line after the closing parenthesis such that your code looks like:

message = “Hello World!”

print (message)

message = “I am new to the programming world!”

print (message)

save the program and run the code. The output of the code should look like:

Hello World!

I am new to the programming world!

We can always change the value stored in a variable anytime in our code and python will always keep track of our variable present value.

If we had not printed the first message, then python will only print the second message. Try removing the first print(message) and run the code. The output will just be the second message.

Naming and using variables

When using variables in python, there are certain rules and regulations for naming variables. This helps not to cause error in our code and also to help us to write code that is easier to read and understand. Always keep the following rules in mind when working or declaring variables for use:

·         Variable name can only contain letters, numbers or underscore characters. You can start a variable name with a letter or underscore but not with a number. E.g animal_1 but not 1_animal

·         Variable name must not contain space, but we can use an underscore character to separate words e.g. first name is not allowed but first_name is allowed.

·         Avoid the use of python reserved keywords and functions names as variable names. There are some words which python reserved for itself which should not be use as a variable name e.g. if, print, while, continue etc. to see the full list of python reserved words, visit http://docs.python.org/2.5/ref/keywords.html.

·         Keep variable names short and descriptive. E.g. first_name which is better than f_n, name_lenght which is better than length_of_persons_name.

·         Be cautious when using lowercase l and uppercase O as they may be mistaken for number 1 and 0

One of the greatest problem encounter by a programmer is variable naming. But it takes continuous practice and try reading other peoples code and you will start coming up with a meaningful variable name.

How to avoid name errors when using variable

In everyday, we make mistake and since from the famous quote which says that “no one is above mistakes”, there is possibility of making mistakes in our code and our compiler would raise a traceback that there is an error in our code. It is a normal thing to make mistake but we need to learn how to fix these mistakes.

Let write some code with a mistake of misspelt variable in it. Let open our new version of hello_world.py program and type the following:

message = “Hello World!”

print (mesage)

Anytime our program encounter an error, python interpreter does it best to help us figure out where the error is in our program. The interpreter will raise a Traceback when a program encounters an error during the execution.

A traceback message shows a record of where the interpreter encounter an error when trying to execute a code. Because of the misspelt variable that is called to the print() method, a traceback will thus be raised by our interpreter which will look like this:

Traceback (most recent call last):

File "hello_world.py", line 2, in <module>

print(mesage)

NameError: name 'mesage' is not defined

Looking at the output above, the first line indicated a traceback, the second line tells us that the file hello_world.py on line 2 is having a problem. The third line shows the code that generated the error. The fourth lines tells us that the error eoncountered is of type NameError which means a name that has not been defined has been used in our program or we have wrongly spelt our variable name.

If we look at our code, we can observe that we omitted the letter S in our “message” which generated the error. To correct this error, we either correct the variable name or we remove one S from our variable name. i.e.

message = “Hello World!”

print (message)

Or

mesage = “Hello World!”

print (mesage)

if any of the above is used, the program will run without encountering an error.

using variables in our code is very good but it may get complicated if we use bad spelling and make mistakes in our spelling. Many of the programming error encountered are typographical error which we need to look out for and try as much as possible to watch out for typo errors.

NOTE: the best way to understand programming concept is to try using them to create something. Henceforth, any topic done will be accompanied by an exercise, if you encounter any problem, comment it and we will respond to you as fast as possible.

Try to practice the following and try to give us your source code in the comment box.

1.      Write a simple program which prints a greeting message. Store your greeting message in a variable and then print the message

2.      Edit your program so that you have a traceback when your code tries to run. Correct the traceback and make sure your code eventually works.

Hint: make sure to save your program with descriptive names using lowercase, underscore and number if needed. Use the .py extension in saving your works.



Thank you for reading!

Watch out for the next session which will be on data types used in python.

Always remember to subscribe to our newsletter as that give you a high chance of being notified when a new lecture has been posted.
Share:

0 comments:

Post a Comment