Skip to main content

how to use list data type in Python?

Where we can use Python
1) In web development using frameworks like Django and flask.
(Django has more features than flask but flask is good for lightweight web applications)
2) In data science and artificial intelligence.
  (python has wide range of libraries and frameworks for data science and for artificial intelligence)
   
3)In Hacking

  and in many other things

Lists in python
List is data type 

For complex programs we need to store and arrange large amount of data. For storing large amount of data we use lists, tuples and dictionary. 
In this data type the values(data) are stored in the ordered sequence unlike dictionary. 

Example of a list 

Animal =  ['Tiger', 'Cat', 'Dog']
As you can able to see in above example, the list start and end with square brackets, [ ]

We can store strings in a single or double quotes('string' or "string").
To separate the values in the list we can use comma ( , ) .

List value - 
                       The list value is referred to list itself.
E.g
   

Let's take another example -
 Vehicles = ['Car', 'Bike', 'Truck']
In above example Vehicles  is a variable.
The Vehicle variable store one value I.e list value. But list value contains more value. That values are Car, Bike, Truck.

Next point -  
How do we access the data from the list.
To print the all the values from the list.
We simply write the following line of code.

 1. Vehicles = ['Car', 'Bike', 'Truck']
 2. print(Vehicles) 
After running this program you will get following output.

Output
            ['Car', 'Bike', 'Truck']

But what if we want to get a certain value from list value. For that we use following statement

  print(list _ name[index no])

In above line of code we write.  listname and index no of a value.
Note: Index no are always start from zero
E.g
    Vehicle = ['Car' ,'Bike' ,'Truck'] 
Data has index as follows
Car = 0
Bike = 1
Truck = 2

If we want to print the first value from the list then you just need to do this
         print(Vehicle[0])
Example program

Vehicles = ['Car', 'Bike', 'Truck']
#To print first value
print(Vehicles[0])

#To print second value
print(Vehicles[1])

#To print third value
print(Vehicles[2])

 Output -
'Car' 'Bike' 'Truck'

A single list can also be contain more than one list value
E.g
     list = [['Car', 'Bike' 'Truck']                                  [10,20,30]]
The normal indexing doesn't work here. If you want to print 10 then you think just write

       print(list[3])
This won't work here

To index this type of lists, we just index the first list to zero and second list value to one
E.g

list = [['Car', 'Bike' 'Truck']  [10,20,30]]

List value has index a follows
['Car', 'Bike' 'Truck'] = 0
   [10,20,30] = 1

To print the first list value. We just use following statement.

     print(list[0])

Output 
['Car', 'Bike' 'Truck'] 

To print second list value
    print(list[1])
Output
            [10,20,30]

To print the specific value from the specific list value. We just need to do a slightly different thing, look at following skeleton of code.

print(list _ name[index of list value][index of value from that list value])

E.g

list = [['Car', 'Bike' 'Truck']  [10,20,30]]
#To print Car
print(list[0][0])

#To print Truck
print(list[0][2])

#To print 10
print(list[1][0])

#To print 20
print(list[1][1])




Comments

Popular posts from this blog

Difference between ARM and x86 processors

Difference between ARM and x86 processors. Introduction What is CPU? CPU stand for central processing unit. The fundamental function of CPU is to process information. Introduction to ARM processors ARM stands for ADVANCED RISC MACHINES  . The ARM processors based on RISC instruction set. RISC stands for REDUCED INSTRUCTION SET COMPUTING . By seeing the full form of RISC you probably get the idea of that the ARM processors has less and simple instruction set than the processors of x86 architecture. Introduction to x86 The x86 processors based on CISC instruction set. CISC stand for COMPLEX INSTRUCTION SET COMPUTING . This type of processors has complex instruction sets means more instruction than the ARM processors The first difference point is Applications - 1. ARM processors - Usually ARM processors are used in portable, small and that devices which has unreliable source of power like batteries. E.g some laptops, smartphones...

Best AdSense alternative for your blog and website.

If you want a better alternative of AdSense for your blog or website because of Adsense is not approving the site or any other reason. If your website has adult content or pirated content Adsense won't approve your website. The name of the alternative is  PopAds . PopAds  allow you to monetize any type of website.  PopAds also allows you to monetize a website that contains adult or copyrighted content(i am not supporting piracy). If you want to create an account of popads visit this link  popads.net . Pros of using popads 1. You can monetize any type of website. 2. Instant approval. 3. You can control the rates of advertisements. 4. You have control of which type of ads do you want to show on your website like  a)popup  b)popunder  c)tabunder  d)tabup  e)long loading  f)Adult  e)Audio/Video Con 1)you can't use Adsense with popads.