Python

Lists in Python

You can find the python codes used in tutorial https://rajith681.wordpress.com/2016/06/18/python-3-lists below.

List Creating

List1=[]
List2=[1,2,3,4,5]
List3=['a','b','c']
List4=[1,'abc']

Accessing List Items

List1=[]
List2=[1,2,3,4,5]
List3=['a','b','c']
List4=[1,'abc']

print(List2[4])
print(List3[1])

Updating List

List1=[]
List2=[1,2,3,4,5]
List3=['a','b','c']
List4=[1,'abc']

List2[2]=10
List3[0]="z"

print(List2)
print(List3)

Basic List Operatioins

List1=["a","b","c"]     
List2=["d","e","f"]

print (len(List1))      #1

print (List1 + List2)   #2

print (List1*2)         #3

print ("a" in List1)    #4
print (1 in List1)

List Methods

append() 

List1=[1,2,3,4]

print(List1)

List1.append(5)

print(List1)

count() 

List1=[1,2,3,1,1,2,1,4,7,1]

print(List1.count(1))

index() 

List1=[1,2,3,1,1,2,1,4,7,1]

print(List1.index(4))

insert() 

List1=[0,1,2,3,4,5,6,7,8,9,10]

print(List1)

List1.insert(1,"A")

print(List1)

remove() 

List1=[0,1,2,3,4,5,6,7,8,9,10]

print(List1)

List1.remove(5)

print(List1)

sort() 

List1=[1,3,7,6,4,2,5]

print(List1)

List1.sort()

print(List1)

min() and max() 

List1=[1,3,7,6,4,2,5]

print("Max=",max(List1))

print("Min=",min(List1))

Kavinda

Proud UCSCian | Proud Devan | Computer Geek | Superman | Love Chess & Programming

One thought on “Lists in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.