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

Input output and processing device's

Input devices - 1) Mouse 2) Keyboard 3) Game controller 4) Scanner 5) Microphone Input device means a device that provide data to computer for processing. * Keyboard sends command to  computer * Scanner scans the document or image and sends it to the computer. Output devices - 1) Monitor 2) Printer 3) CD/DVD 4) Projector 5) Speaker Output devices displays the information to the user or they collect the information from the processing device. * Monitor displays the information on the screen. *Printer prints the information on paper. Input/Output devices (I/O) Input/Output devices works as both input and output devices. Input/output devices- 1) USB flash 2) Hard disk * USBflash stores information and also sends information to the computer. So *USB flash work as both input and output device. Processing devices - Processing device means a device which process the data        Or The devices which convert raw data into a useful information. Processing

How computers works(how software works on computer)

1) Introduction to computers.    A computer is nothing but a device that compute numbers. So now let's talk about how this cool thing works. 2) parts of a computer and their main functions. a] CPU (Central Processing Unit) -     Do all the processing (Arithmetical and          logical operations). b] RAM (Random Access Memory) -     Stores important data that needed for     execution of any program. c] Storage devices (Hard Disk,SSD) -     Stores date like (Operating System (O.S), Software's,  images, songs, videos, etc.) d] Motherboard -     You can think it as a PCB that helps to communicate with other devices like CPU,RAM, storage devices, graphics card etc to each other. e] PSU (Power Supply Unit) -     Provides power to all components.

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