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

Apple is moving from Intel to ARM

       According to some rumours Apple is planning to use ARM chips instead of Intel in their Mac's. Apple plan to use their custom build ARM processors will be gadhe changer if everything goes well. Intel is facing difficulties in achieving their perfomance goal. If Apple's ARM processors outperforms Intel processors then it will great for Apple and their customers. By rumours it seems Apple 🍎 will launch their Mac's with ARM processors in fourth quarter of next year.  ARM processors generates less heat so it's easy to overclock, that will significantly boost their perfomance. Also ARM processors are very power efficient. Because of power efficiency of ARM processors we will notice increase in battery life of upcoming ARM based Mac's and MacBooks. If you don't know the difference between ARM and x86 then visit Difference between ARM and x86

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

The future of gaming phone's (AMD graphics on Samsung phones)

The future of gaming smartphones seems to be bright. Lots of large gaming companies are now focusing on developing mobile games. Pc games are being ported for mobiles like fortnite, call of duty,PUBG. Mobile eSports gaming is going to peak at this time. So companies are trying to develop gaming phone's as high end as possible and trying to lower the price. E.g Asus ROG and ROG 2, Xiaomi  blackshark. Samsung and AMD is doing partnership. The AMDs Radeon graphics are based on RDNA architecture will be going to combine with Samsung's Exynos cpu. RDNA means Radeon DNA. RDNA gives more perfomance compare to its power usage. RDNA architecture will be used in play station 5 and Xbox series X. Samsung's Phone with Exynos processor integrated with Radeon graphics could go on the sale in 2021. The smartphone companies are trying very hard to defeat each other. In  future we expect that we're able to play games like GTA 5 on our smartphones. If you want to read more arti...