Hi guys, welcome back to emmason247.
This is day 2 of our python tutorial and as i mentioned in the introductory and setup part, we will be talking about variables and working with them in python. If you have not read the first part of this tutorial, i recommend you do so by
Clicking Here.
I want you to follow this tutorial with keen interest and practice everything carefully if you are really interested in learning python.
Look at a variable as a container with a label that is used to hold information. A variable is a symbolic name associated with a certain value.
During your communication with the computer as a programmer,there are times when you are going to have to give it information and you need a container to hold it. Look at the example below:
MyName = "Isaiah Ozadhe"
The above statement simply means that you have succeeded in creating a variable (container) named(labeled) MyName and you have assigned a value - Isaiah Ozadhe - to it.
Creating a variable in python and assigning it a value is very easy. Now we will go practical. Remember the folder we created in part 1? (yea i told you to keep it). Lauch Visual studio code and open the folder by clicking on File>Open Folder.
Now click on the new file icon with the plus(+) sign and name the file variables.py.
Enter the following code in the file:
MyName = "Isaiah Ozadhe"
print(MyName)
You can decide to sustitute your name into the code. Now right click anywhere on your work area and click on 'Run Python File In Terminal'.
If Everything went fine, you will see the name in out code printed on the terminal. Now Let me attempt to explain the 2 lines of code.
Line 1:
First we declare a variable with the name - "MyName".
Then We Assign a value - Isaiah Ozadhe to it using the "=" operator (more on operators as we go on).
Line2:
We print the variable out in the terminal using the inbuilt print function(more on functions later).
I assume you now understand how to declare and assign a value to a variable.
Look at this. if i have water inside a container, i can decide at any time to replace it with oil.
This is also easily acheivable in python. the content of a certain variable can be changed at any time.
Add some new code to our variable file so it looks like this:
MyName = "Isaiah Ozadhe"
print(MyName)
MyName = "John"
print(MyName)
MyName = "mark"
print(MyName)
MyName = "james"
print(MyName)
Now run the code by following the process we used earlier. What do you see in the terminal?. A new name is printed on each line. That is because we replaced the name after each printing.
It's like throwing your water away and replacing it with oil in the same container. it all depends on you - its your container so you decide how you want to use it.
Note: The last value you assign to the variable will be the active one.
Remember that the content of your container has a type. is it water?, is it oil? or is it sand?.
Same way we have what is known as data types in python varables. Below are the most common data types in python:
String.
Srings are used to hold texts in python. the variable we declared above is a string variable. Any character, word, number etc enclosed in single or double quotes ('',"") is a stringed variable. Examples are:
MyName = 'Isaiah Ozadhe'
Surname = "Ozadhe"
Height = "6.2ft"
Numbers
This type of variable is used to hold a numeric value. It Could be a whole number(int), a decimal number(float) or a complex number.
Below are examples of Numeric variables:
age= 24
Height = 6.2
a = 3.2j
Line 1: an integer number.
Line 2: a float number.
Line 3. a complex number.
List
As the name implies we can declare a variable that can hold more than one value at the same time.
Replace the code in variables.py file with the code below:
even_numbers = [2, 4, 6, 8, 10]
print(even_numbers)
print(even_numbers[0])
print(even_numbers[3])
even_numbers.append(12)
print(even_numbers)
print(even_numbers[1:3])
new_numbers =[12, 14, 16]
even_numbers.extend(new_numbers)
print(even_numbers)
even_numbers.insert(0, "start")
print(even_numbers)
run it. what do you see in the terminal?. Let me explain.
Note: examine each code and it's output line by line as i explain.
Lines 1 and 2: You declare a list variable and print it out.
Lines 3 and 4: You are printing individual values from the list using their index postion. the first value in the list is always index 0.
Lines 5 and 6: We are using the append method to add a value to our list and then we print out the whole new list.
Line 7: We are choosing a range from our list to print out. in this case index 1:3.
Lines 8-10: Here we declare a new list. then we use the extend method of a python list to join it with our old list and print out the whole new list.
Lines 11 and 12: We use the insert method to specify an index postion in the list and insert a value to it. then we print the new list.
If you don't understand it, go back again and use the line numbers to write the code, run them and observe carefully line by line.
Tupple
A tupple is just like a list. In python a tupple is called an immutable list. that symply means that you cannot change anything about it once it is declared.
Adding a Comment.
Before we go on, i want to let you know how to add comments to our code. recall that we had to replace our code in the variables.py file a while ago and that is not commendable because we might need it later.
Once a line of code is commented, it will no longer be interpreted - it will be seen as plain text.
To comment a single line of code, just add the hastag sign(#) to the begining.
Eg:
#MyName = "Isaiah Ozadhe"
The above code will not be executed because it is seen as a comment given that it is preceded by the # sign.
To comment multiple lines, add a tripple quote(''') to the position you want to start and the postion you want to end.
E.g
'''
Myname = "Isaiah Ozadhe"
print(My name)
Age = 5
'''
All the above codes will not be executed if you run because they are in between triple quotes.
Another important use of comments is in the description of your code and adding notes between codes. Look at this:
#The aim of this part of the tutorial is to learn about variables.
#The line below declares a variable
MyName = "Isaiah Ozadhe"
#This line prints the value of the variable
print(MyName)
#this is the end of my program.
You see, all the comments will not be seen by the interpreter but they explain our code to anybody looking at it. I hope that was clear enough.
Now you can comment all our previous codes in the variables.py file by adding (''') to the start and end of the whole code.
To Illustrate tupple, Enter this code:
myTupple = (2,3,4,4,5)
print(myTupple)
run it and observe that the tupple just like a list has all it's items printed out.
Now add this line to the code:
myTupple[0]=5
Run it and you see we have an error. what i was trying to do is replace the item on index postion 0 with 5 but that is not possible because it's immutable. you can't make changes to it.
Dictionary
A dictionary is also like a list but we do not use index to acess its values. we make use of assigned keys.
Again comment all our code and add this:
My_data = {"surname":"Ozadhe", "FirstNAme": "Isaiah", "age": 24}
print(My_data)
print(My_data["age"])
print(My_data["surname"])
Run the code.
In Lines 1 and 2: we declare our dictionary and print it out in entirety.
In Lines 3 and 4: we print out specific values from the dictionary using their keys unlike in list and tupple where we use index.
All right guys that is it for variables.
What Now?
Practice!!!!!! Practice!!!!!! Practice!!!!!! Practice!!!!!! Practice!!!!!! Practice!!!!!! Practice!!!!!!
Yes that is what you need to be good in any programming language at all. go back and continue practicing everything we have learnt today until you master them.
I remain Isaiah Ozadhe and i write regularly on programming in very SIMPLE TERMS here on emmason247. stay tuned for part 3 "Collecting user inputs and reading from external files".
Don't forget to check regularly for new articles here on emmason247.
Until next time, it is goodbye!!!.