SlideShare a Scribd company logo
1 of 45
Yeshwantrao Chavan College of
Engineering, Nagpur
List and control statements
Lists in Python
• List is a collection which is ordered and
changeable. Allows duplicate members.
• # Creation of List
•
• # Creating a List
• List1 = []
• print(List1)
List of Numbers
• # Creating a List of numbers
• List2 = [10, 20, 14]
• print(List2)
List of Strings
• # Creating a List of strings
• List3 = ["apple", "banana", "cherry"]
• print(List3)
Indexing
apple banana cherry
0 1 2
-3 -2 -1
• print(List3[0])
• print(List3[2])
• print(List3[-1])
Replace
• Change the second item:
• List3 = ["apple", "banana", "cherry"]
• List3[1] = "blackcurrant"
• print(List3)
• ['apple', 'blackcurrant', 'cherry']
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)
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)
More on Indexing
• List=[2, 33, 222, 14, 25]
2 33 222 14 25
0 1 2 3 4
-5 -4 -3 -2 -1
• print(List[-1]) # 25
• print(List[0]) # 2
• print(List[:-1]) # 2 33 222 14
• print(List[: : -1]) # 25 14 222 33 2
• print(List[:]) # 2 33 222 14 25
2 33 222 14 25
0 1 2 3 4
-5 -4 -3 -2 -1
List
+ve Index
-ve Index
• thislist = ["apple", "banana", "cherry",
"orange", "kiwi", "melon", "mango"]
• print(thislist[2:5])
• ['cherry', 'orange', 'kiwi']
• print(thislist[:4])
• ['apple', 'banana', 'cherry', 'orange’]
• print(thislist[2:])
• ['cherry', 'orange', 'kiwi', 'melon', 'mango']
apple banana cherry orange kiwi melon mango
0 1 2 3 4 5 6
• print(thislist[-4:-1])
• ['orange', 'kiwi', 'melon’]
• print(thislist[:-1])
• ['apple', 'banana', 'cherry', 'orange',
'kiwi', 'melon']
apple banana cherry orange kiwi melon mango
-7 -6 -5 -4 -3 -2 -1
Length
• Knowing the size of List
• List1 = []
• print(len(List1))
• List2 = [10, 20, 14]
• print(len(List2))
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
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']
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']
Remove Item
• The remove() method removes the
specified item:
• thislist = ["apple", "banana", "cherry"]
• thislist.remove("banana")
• print(thislist)
• ["apple", "cherry"]
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"]
• 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
• The clear() method empties the list:
• thislist = ["apple", "banana", "cherry"]
• thislist.clear()
• print(thislist)
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"]
• Make a copy of a list with the list()
method:
• thislist = ["apple", "banana",
"cherry"]
• mylist = list(thislist)
• print(mylist)
Index of item in list
• What is the position of the value
"cherry":
• thislist = ['apple', 'banana', 'cherry']
• x = thislist.index("cherry")
• print(x)
Reverse
• Reverse the order of the fruit list:
• thislist = ['apple', 'banana', 'cherry']
• thislist.reverse()
• print(thislist)
• ['cherry', 'banana', 'apple']
Sort
• Sort the list alphabetically:
• thislist = ['apple', 'banana', 'cherry’]
• thislist.sort()
• print(thislist)
• cars = ['Ford', 'BMW', 'Volvo']
• cars.sort()
• print(cars)
Join Two Lists
• list1 = ["a", "b" , "c"]
• list2 = [1, 2, 3]
• list3 = list1 + list2
• print(list3)
• ['a', 'b', 'c', 1, 2, 3]
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]
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.
• 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.
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="")
Conditional Statement
Comparison Operators
• equal: ==
• not equal: !=
• greater than: >
• less than: <
• greater than or equal to: >=
• less than or equal to: <=
If -else
if (condition):
Statement 1
else:
Statement 2
Condition
True
False
Statement 1
Statement 2
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
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")
Game Time
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)
Quiz
• What does the following code print to the
console?
•
if 5 > 10:
print("fan")
elif 8 != 9:
print("glass")
else:
print("cream")
• 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")
• What does the following code print to
the console?
if True:
print(101)
else:
print(202)
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
list and control statement.pptx

More Related Content

Similar to list and control statement.pptx

Similar to list and control statement.pptx (20)

LIST_tuple.pptx
LIST_tuple.pptxLIST_tuple.pptx
LIST_tuple.pptx
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Python Lists.pptx
Python Lists.pptxPython Lists.pptx
Python Lists.pptx
 
Lists
ListsLists
Lists
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Python_Sets(1).pptx
Python_Sets(1).pptxPython_Sets(1).pptx
Python_Sets(1).pptx
 
powerpoint 2-13.pptx
powerpoint 2-13.pptxpowerpoint 2-13.pptx
powerpoint 2-13.pptx
 
R Data Structure.pptx
R Data Structure.pptxR Data Structure.pptx
R Data Structure.pptx
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdf
 
File handling in pythan.pptx
File handling in pythan.pptxFile handling in pythan.pptx
File handling in pythan.pptx
 
Data type list_methods_in_python
Data type list_methods_in_pythonData type list_methods_in_python
Data type list_methods_in_python
 
updated_list.pptx
updated_list.pptxupdated_list.pptx
updated_list.pptx
 
Pa1 session 3_slides
Pa1 session 3_slidesPa1 session 3_slides
Pa1 session 3_slides
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Python List
Python ListPython List
Python List
 
6. list
6. list6. list
6. list
 
Python Dictionary
Python DictionaryPython Dictionary
Python Dictionary
 
Module 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptxModule 2 - Lists, Tuples, Files.pptx
Module 2 - Lists, Tuples, Files.pptx
 
Tuple in python
Tuple in pythonTuple in python
Tuple in python
 

Recently uploaded

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 

Recently uploaded (20)

Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 

list and control statement.pptx

  • 1. Yeshwantrao Chavan College of Engineering, Nagpur List and control statements
  • 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)
  • 5. Indexing apple banana cherry 0 1 2 -3 -2 -1 • print(List3[0]) • print(List3[2]) • print(List3[-1])
  • 6. Replace • Change the second item: • List3 = ["apple", "banana", "cherry"] • List3[1] = "blackcurrant" • print(List3) • ['apple', 'blackcurrant', 'cherry']
  • 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)
  • 9. More on Indexing • List=[2, 33, 222, 14, 25] 2 33 222 14 25 0 1 2 3 4 -5 -4 -3 -2 -1
  • 10. • print(List[-1]) # 25 • print(List[0]) # 2 • print(List[:-1]) # 2 33 222 14 • print(List[: : -1]) # 25 14 222 33 2 • print(List[:]) # 2 33 222 14 25 2 33 222 14 25 0 1 2 3 4 -5 -4 -3 -2 -1 List +ve Index -ve Index
  • 11. • thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon", "mango"] • print(thislist[2:5]) • ['cherry', 'orange', 'kiwi'] • print(thislist[:4]) • ['apple', 'banana', 'cherry', 'orange’] • print(thislist[2:]) • ['cherry', 'orange', 'kiwi', 'melon', 'mango'] apple banana cherry orange kiwi melon mango 0 1 2 3 4 5 6
  • 12. • print(thislist[-4:-1]) • ['orange', 'kiwi', 'melon’] • print(thislist[:-1]) • ['apple', 'banana', 'cherry', 'orange', 'kiwi', 'melon'] apple banana cherry orange kiwi melon mango -7 -6 -5 -4 -3 -2 -1
  • 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']
  • 17. Remove Item • The remove() method removes the specified item: • thislist = ["apple", "banana", "cherry"] • thislist.remove("banana") • print(thislist) • ["apple", "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']
  • 25. Sort • Sort the list alphabetically: • thislist = ['apple', 'banana', 'cherry’] • thislist.sort() • print(thislist) • cars = ['Ford', 'BMW', 'Volvo'] • cars.sort() • print(cars)
  • 26. Join Two Lists • list1 = ["a", "b" , "c"] • list2 = [1, 2, 3] • list3 = list1 + list2 • print(list3) • ['a', 'b', 'c', 1, 2, 3]
  • 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="")
  • 31.
  • 32. Conditional Statement Comparison Operators • equal: == • not equal: != • greater than: > • less than: < • greater than or equal to: >= • less than or equal to: <=
  • 33. If -else if (condition): Statement 1 else: Statement 2 Condition True False Statement 1 Statement 2
  • 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")
  • 37.
  • 38.
  • 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