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
Post a Comment