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...

How to install and use scrcpy on Linux?

In this article, you will learn how to install and use scrcpy on Linux. What is scrcpy? scrcpy is an application used to display and control the Android phone without installing any application on your android device and importantly it does not require any  root  access. Things you need to start using this application are listed below. 1) USB cable 2) Android phone with USB debugging on(Android 5.0 or above) 3) PC(Debian based os required) Installation sudo apt install scrcpy I prefer to use snap for installing scrcpy because snap store seems to have the latest package To install using snap use the following command snap install scrcpy Running scrcpy step 1) Plug an Android device(turn on USB debugging) using USB to your pc. step 2) open terminal and type scrcpy (or type scrcpy --help to see list of additional commands). If you want to know more about scrcpy click on the link below https://github.com/Genymobile/scrcpy Bonus:  You can also play games like among us using s...