Search Header Logo
Python list

Python list

Assessment

Presentation

Arts

1st Grade

Practice Problem

Hard

Created by

s tobgyal

Used 28+ 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)

media

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

media

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)

media

​clear():
~removes all items from the list

Example:
number=[1,2,3,4,1,2,3,4]

number.clear()

print(number)

media

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)

media

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)

media

​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?????


media

15

reverse():
​reverses the elements of the list


Example:
my_num=[3,1,7,2,4,6,5]

my_num.reverse()

print(my_num)



media

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)

media

17

​max()
returns the largest item in the list

media

​min()
returns the smallest item in the list

media

18

sum():
Return the sum of all elements in the list

media

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 :

media

Example2 :

media

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