

Python list
Presentation
•
Arts
•
1st Grade
•
Practice Problem
•
Hard
s tobgyal
Used 31+ times
FREE Resource
21 Slides • 0 Questions
1
list in python
2
Lists in python:
~are used to store multiple items in a single variable.
~It can have any number of items and they may be of different types(integer,float,strings)
~A list contains items/elements separated by commas and enclosed within square brackets []
List items:
~are ordered, mutable, and allow duplicate values
~list items are indexed, the first item has index [0], the second has [1] etc
3
Example:
my_friends=["Nima","Dawa","Karma"]
print(my_friends)
Q. What is the index for the third element in the list?
4
List index:
~we can use the index operator[] to access an item in a list.
~Indexes starts from 0.
Negative indexing:
beginning from the end..
Example:
fruits=["orange","apple","banana"]
print(fruits[-2])
output??
5
Access items:
~by referring to its index
~ typing to access indexes beyond the number of elements or items will raise IndexError
Example:
animals=["Cat","Dog","cow"]
print(animals[1])
Example:
animals=["Cat","Dog","cow"]
print(animals[3])
Output?????
6
list length function():
~to find the number of items in a list
~count will start from one
Example:
subjects=["English","Dzongkha","Maths","ICT"]
print(len(subjects))
output??????
7
nested list:
~a list in another list or list within list
Example:
my_list=["Cat",[1,2,3],['a','b','c']]
print(my_list)
8
list methods:
can be used to manipulate items in the list
append():
~adds an item to the end of the list.
Example:
friends=["Sonam","Pema","Dorji"]
friends.append("Wangmo")
print(friends)
Output
9
remove():
removes the first matching element (which is passed as an argument) from the list.
Example:
number=[1,2,3,4,1,2,3,4]
number.remove(4)
print(number)
clear():
~removes all items from the list
Example:
number=[1,2,3,4,1,2,3,4]
number.clear()
print(number)
10
index()
returns the index of the specified element in the list.
Example:
fruits=["Apple","Orange","Banana","Grapes"]
x=fruits.index("Banana")
print(x)
Output????
11
extend():
adds the items of two or more lists (list, tuple, string etc.) to the end of the list.
Example:
vege_1=["Cabbage","Chilli","Potato"]
vege_2=["Tomato","Raddish"]
vege_1.extend(vege_2)
print(vege_1)
12
count():
~returns the number of times the specified element appears in the list.
vege_1=["Cabbage","Chilli","Potato","Chilli"]
my_count=vege_1.count("Chilli")
print(my_count)
Output?????
13
insert():
inserts an element to the list at the specified index
Example:
my_vowels=['a','e','i','u']
my_vowels.insert(3,'o')
print(my_vowels)
pop():
removes the item at the specified index.
Example:
my_vowels=['a','e','i','u']
my_vowels.pop(2)
print(my_vowels)
Output?????
14
sort():
sorts the items of a list in ascending or descending order
Example:
my_num=[3,1,7,2,4,6,5]
#sort in ascending order
my_num.sort()
print(my_num)
Example:
my_num=[3,1,7,2,4,6,5]
#sort in descending order
my_num.sort(reverse=True)
print(my_num)
Output?????
15
reverse():
reverses the elements of the list
Example:
my_num=[3,1,7,2,4,6,5]
my_num.reverse()
print(my_num)
16
copy():
returns a shallow copy of the list
Example:
my_num=[3,1,7,2,4,6,5]
my_num.copy()
print(my_num)
17
max()
returns the largest item in the list
min()
returns the smallest item in the list
18
sum():
Return the sum of all elements in the list
19
slicing:
access specific portions of sequences like lists, strings, and tuples using a special syntax: [start:stop:step]. Here’s what each part means:
start: The index to begin the slice (inclusive). If omitted, it defaults to the start of the sequence.
stop: The index to end the slice (exclusive). If omitted, it defaults to the end of the sequence.
step: The amount by which the index increases. If omitted, it defaults to 1.
20
Example1 :
Example2 :
21
Activity question:
1. The temperature for last 7 days in Thimphu has been recorded in the list below.
temperatures = [22, 23, 24, 22, 20, 18, 19]
Write a Python program to find the average temperature in Thimphu.
2. Write a Python program that display a menu of Bhutanese traditional dishes. Make the menu display the list of Bhutanese dishes as given below.
3. Sonam has scored following marks in different subjects in class X. Write a Python program to calculate the average mark scored by Sonam.
marks = [70, 64, 63, 49, 64, 68, 57, 54, 60, 78, 99]
4. Write a Python program to calculate the sum of all even numbers in a given list.
numbers = [2, 5, 8, 3, 10, 7, 6, 12]
list in python
Show answer
Auto Play
Slide 1 / 21
SLIDE
Similar Resources on Wayground
18 questions
Used to, be/get used to
Presentation
•
KG
16 questions
Shapes and Colors
Presentation
•
1st - 2nd Grade
15 questions
math missing addends
Presentation
•
1st Grade
18 questions
Plants
Presentation
•
6th - 8th Grade
19 questions
Possessive Adjectives
Presentation
•
1st Grade
15 questions
Even and Odd
Presentation
•
1st - 2nd Grade
17 questions
Money - Coins
Presentation
•
1st - 2nd Grade
15 questions
La Ropa
Presentation
•
KG
Popular Resources on Wayground
24 questions
PBIS-HGMS Day 10
Quiz
•
6th - 8th Grade
10 questions
HCS SCI 03 Summer School Review 3
Quiz
•
3rd Grade
11 questions
Home Scope
Quiz
•
7th - 8th Grade
15 questions
HCS SCI 05 Summer School Assessment 3 Review
Quiz
•
5th Grade
35 questions
Lufkin Road Middle School Student Handbook & Policies Assessment
Quiz
•
7th Grade
18 questions
Geo 11.3 Area of Circles and Sectors
Quiz
•
9th - 11th Grade