Skip to main content

8.Python Lists

PYTHON LISTS
    A list is a collection which is ordered and changeable/mutable. Lists are written with square brackets. Lists are very similar to arrays. They can contain any type of variable, and they can contain as many variables as you wish. Lists can also be iterated over in a very simple manner. It can have any number of items and they may be of different types (integer, float, string etc.). 

Syntax:
listName=[]
Program 
Fruits=["Apple","Banana","Grapes"]
print(Fruits)
Output
["Apple","Banana","Grapes"]
    The above list consists of strings.List can contain any datatypes within it.
Program 
Fruits=["Apple",30,98.5,("Banana","Grapes")]
print(Fruits)
Output
['Apple', 30, 98.5, ('Banana', 'Grapes')]
     The above list comprises of integers<30>,strings<"Apple">,float<98.5>,tuples<('Banana','Grapes')>
Program -prints the second item in list
friends=["Harry","Ron","Hermione","Hagrid"] 
print(friends) 
print(friends[2]) #prints the second item
Output
['Harry', 'Ron', 'Hermione', 'Hagrid'] 
Hermione 
Program -prints the last item in list
friends=["Harry","Ron","Hermione","Hagrid"] 
print(friends) 
print(friends[-1]) #prints the last item
Output
['Harry', 'Ron', 'Hermione', 'Hagrid'] 
Hagrid 
Program -prints the items within range
friends=["Harry","Ron","Hermione","Hagrid"] 
print(friends) 
print(friends[1:3]) #prints from item 1 ,item2
Output
['Harry', 'Ron', 'Hermione', 'Hagrid'] 
['Ron', 'Hermione'] 
Program -prints from start to the limit
friends=["Harry","Ron","Hermione","Hagrid"] 
print(friends) 
print(friends[:3]) #prints from item 0 ,item1,item2
Output
['Harry', 'Ron', 'Hermione', 'Hagrid'] 
['Harry', 'Ron', 'Hermione'] 
Program -prints from the specified index to last
friends=["Harry","Ron","Hermione","Hagrid"] 
print(friends) 
print(friends[1:]) #prints from item1 to last item
Output
['Harry', 'Ron', 'Hermione', 'Hagrid'] 
['Ron', 'Hermione', 'Hagrid'] 
Program -to update the list with index
friends=["Harry","Ron","Hermione","Hagrid"] 
print(friends) 
friends[3]="Malfoy" #updates item 3 to malfoy
print(friends) 
Output
['Harry', 'Ron', 'Hermione', 'Hagrid'] 
['Harry', 'Ron', 'Hermione', 'Malfoy']

Printing the elements in list with for loop:
Program
friends=["Harry","Ron","Hermione","Hagrid"] 
for i in friends:
    print(i)
Output
Harry
Ron
Hermione
Hagrid

Watch the video below to know more about lists!
Do visit the link below to know about list methods!!
Happy Learning!!

Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl