2. Lists in Python
• List is a collection which is ordered and
changeable. Allows duplicate members.
• # Creation of List
•
• # Creating a List
• List1 = []
• print(List1)
3. List of Numbers
• # Creating a List of numbers
• List2 = [10, 20, 14]
• print(List2)
4. List of Strings
• # Creating a List of strings
• List3 = ["apple", "banana", "cherry"]
• print(List3)
7. List with Duplicate Numbers
• # Creating a List with the use of
Numbers
• # (Having duplicate values)
• List4 = [1, 2, 4, 4, 3, 3, 3, 6, 5]
• print(List4)
8. List with Mixed Type
• # Creating a List with
• # mixed type of values
• # (Having numbers and strings)
• List = [1, 2, 'apple', 4, 'banana', 6,
'cherry']
• print(List)
13. Length
• Knowing the size of List
• List1 = []
• print(len(List1))
• List2 = [10, 20, 14]
• print(len(List2))
14. List Methods
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count()
Returns the number of elements with the specified
value
extend()
Add the elements of a list (or any iterable), to the end
of the current list
index()
Returns the index of the first element with the
specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
15. Add Items
• To add an item to the end of the list,
use the append() method:
• thislist = ["apple", "banana", "cherry"]
• thislist.append("orange")
• print(thislist)
• ['apple', 'banana', 'cherry', 'orange']
16. Add Items
• To add an item at the specified index,
use the insert() method:
• thislist = ["apple", "banana", "cherry"]
• thislist.insert(1, "orange")
• print(thislist)
• ['apple', 'orange', 'banana', 'cherry']
18. Remove Item
• The pop() method removes the
specified index, (or the last item if
index is not specified):
• thislist = ["apple", "banana", "cherry"]
• thislist.pop()
• print(thislist)
• ["apple", "banana"]
19. • The del keyword removes the specified
index:
• thislist = ["apple", "banana", "cherry"]
• del thislist[0]
• print(thislist)
• The del keyword can also delete the list
completely:
• thislist = ["apple", "banana", "cherry"]
• del thislist
20. • The clear() method empties the list:
• thislist = ["apple", "banana", "cherry"]
• thislist.clear()
• print(thislist)
21. Copy a List
• Make a copy of a list with the copy()
method:
• thislist = ["apple", "banana", "cherry"]
• mylist = thislist.copy()
• print(mylist)
• ["apple", "banana", "cherry"]
22. • Make a copy of a list with the list()
method:
• thislist = ["apple", "banana",
"cherry"]
• mylist = list(thislist)
• print(mylist)
23. Index of item in list
• What is the position of the value
"cherry":
• thislist = ['apple', 'banana', 'cherry']
• x = thislist.index("cherry")
• print(x)
24. Reverse
• Reverse the order of the fruit list:
• thislist = ['apple', 'banana', 'cherry']
• thislist.reverse()
• print(thislist)
• ['cherry', 'banana', 'apple']
27. Extend list
• Use the extend() method to add list2 at
the end of list1:
• list1 = ["a", "b" , "c"]
• list2 = [1, 2, 3]
• list1.extend(list2)
• print(list1)
• ['a', 'b', 'c', 1, 2, 3]
28. Exercise:
• Let the list is given
• fruits = ["apple", "banana", "cherry"]
• 1. Use negative indexing to print the
last item in the list.
• 2. Print the second item in the fruits
list.
• 3. Change the value from "apple" to
"kiwi", in the fruits list.
29. • 4.Use the append method to add
"orange" to the fruits list.
• 5. Use the insert method to add
"lemon" as the second item in the
fruits list.
• 6. Use the correct syntax to print the
number of items in the list.
30. Input Statement
Program that takes an amount in Indian Rupees as
input. You need to find its equivalent in Euro and
display it. Assume 1 Euro equals Rs. 80.
a=input('Enter number=')
a=int(a)
print('Equivalent Euro=',a*80,end="")
34. x=input("Enter test +ve or –ve: ")
if x== "+ve":
print("Quarantine")
else:
print("stay at home")
x==“+ve”
True
False
Quarantine
Enter Test
Stay at home
35.
36. a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
40. Try It
# lets start the game
x=input("Enter your number choice")
X=int(x)
if(x%3==0 and x%5==0):
print("FIZZBUZZ!")
elif(x%3==0):
print("FIZZ!")
elif(x%5==0):
print("BUZZ!")
else:
print(x)
41. Quiz
• What does the following code print to the
console?
•
if 5 > 10:
print("fan")
elif 8 != 9:
print("glass")
else:
print("cream")
42. • What does the following code print to the
console?
hair_color = "blue"
if 3 > 2:
if hair_color == "black":
print("You rock!")
else:
print("Boring")
43. • What does the following code print to
the console?
if True:
print(101)
else:
print(202)
44. If else if Ladder
x = 0
a = 5
b = 5
if a > 0:
if b < 0:
x = x + 5
elif a > 5:
x = x + 4
else:
x = x + 3
else: x = x + 2
print(x)
Given the
nested if-else
below, what will
be the value x
when the code
executed
successfully