Monday, January 4, 2016

Python Basics 02 - Data Structures


How are you today?

Today we are going to see how to define and initialize python variables and other data structures like arrays, etc.

Before we talk about variables, I want to tell you some additional but vary basic things in python.

print("This is the First Line",end=' ')
print("This is the Second Line")

In the above code end='' will tell the compiler to print the line break.

\t is used to print the tab space and \n is used to print the line break.

To generate random numbers in Python you have to import an external library.

import random
random.random() - This function will return a random  number between 0 and 1
random.randint(1,30) -  This function will return a random values between two specified integer values

Ok now, Let's start with Python Variables.
It is not very hard to create a variable in Python. You just have to give the name of the variable and assign the value to the variable as you do in other programming languages. Python compiler will identify the variable type using the value which is stored.

str = "This is a String Variable"
number = 23 //This is a Integer Variable
floating = 34.78233 // This is a floating point number

Well the variable initialization and deceleration in Python is simple as that.

But you have to keep in mind that if you save an number as a String variable type you will not be able to do any arithmetic calculations without converting that variable value to Integer type.

num1 = "23" //This variable holds 23 as a String Type values
num2 = num1*2 //If you try to do arithmetic operation like this you will get an error
If you want to do this calculation you have to convert the string value to Integer value first as follows.
num2 = int(num1)*2  //This will print the correct value

Now let's look in to LIST data structure in Python.

List is similar to array in PHP or Java but can grow in the size.

[] - This will create an empty list
numbers = [1,2,3,4,5,6] - This is an Integer List

Python allows nested lists too.
nestedList = [1,2,3,4,["One",''Two","Three''],5,6]

len(nestedList) - To get the length of the List
nestedList[4] - Returns the value of the 4th index of the List
nestedList[1:4] - Returns the values between 1 and 4 indexes of the List
nestedList[:4] - Returns the values of the indexes which are less than 4
nestedList[4:] - Returns the values of the indexes which are greater than 4

Now let's look in to DICTIONARY data structure in Python.

Dictionary data structure in Python allows you to save a values with a reference key just as a phone book saves the numbers with the name which refers to the number.

empty = {}  // This is an empty dictionary
phoneList = {"Dulanjana":"12345","Bhagya":"67890","Fernando":"23986"}

phoneList.keys() - This will return all the keys in phoneList dictionary
phoneList.values() - This will return all the values in phoneList dictionary
phoneList["Dulanjana"] - This will return the value which refer by the given key // 12345

Well, Hope you got something from this.
See you from another tutorial.

Past Topics :-



No comments:

Post a Comment