Ciam Py 3
Ciam Py 3
Data
Structures
Part 1
... }
Table of contents
01 What is Collection ? ---
02 Lists : Indexing
03 Lists : Slicing
05 Practice
01 { ..
What is Collection ?
} ..
Collection
· A collection allows us to put many values in a single "variable“
There are four collection data types in the Python programming language:
• Dictionary
..
is a collection which is unordered, changeable and indexed.
No duplicate members.
01 { ..
Lists
} ..
List
Create a List:
List = ["apple", "banana", "cherry"]
print(List)
Empty_list = []
} ..
Data Types
} ..
List : indexing
Access Items :
You access the list items by referring to
the index number:
} ..
List : indexing
Access Items :
} ..
List : indexing
Access Items through a Loop :
Print all items in the list, one by one:
} ..
02 { ..
List : Slicing
} ..
Liste : slicing
*
Liste : slicing
*
04 { ..
Lists Methods
} ..
Adding Lists Together
Adding 2 lists :
L1 = [1, 2, 3]
L2 = L1 + [4, 5]
print(L2)
} ..
List Method .append()
you can add values to the end
of a list using the .append()
method
alpha = [‘A’, ‘B’]
orders.append(‘C’)
print(alpha)
# Result: [A’, B’, C']
..
Min, max et somme d'une liste
Copy a List
You cannot copy a list simply by typing list2 =
list1, because: list2 will only be a reference
to list1, and changes made in list1 will
automatically also be made in list2. Make a copy
of a list with the copy() method
List Methods
.Len()
.list()
.enumerate()
2D list
l = [[“Ahmed", 61], ["Ali", 70], ["Sam", 67]]
# Access :
x = l[0][1]
print(x)
# Output
# 61
{ ..
Let’s
Practice
} ..
● What is the output of the
following list assignment
l = [1, 2, 3]
print(l.index(4))
● What is the output of the
following list assignment
l = [1, 2, 3]
l.append()
print(l)
What is the output of the following
list assignment
List_A = [1,2,3,4,5]
sum(List_A[2:])
What is the output of the following
list assignment
list = ['python', 'learning',
'@', ‘F', 'for', ‘Y']
print(list[0:6:2])
What is the output of the following list
assignment
List = [12,,12,14,16,18,20]
X = List.count(12) +List.count(14)
What is the output of the following list
assignment