The document discusses lists in Python, including how to create, access, modify, loop through, slice, sort, and perform other operations on list elements. Lists can contain elements of different data types, are indexed starting at 0, and support methods like append(), insert(), pop(), and more to manipulate the list. Examples are provided to demonstrate common list operations and functions.
Corporate Management | Session 3 of 3 | Tendenci AMS
Python list
1.
2. Creating List
Access, Modify and Delete list Elements
Merge Lists
Use the slicing syntax to operate on
sublists
Loop over lists
3. A list is a compound data type, you can group values
together
A list contains items separated by commas and enclosed
within square brackets ([]).
Lists are similar to arrays in C.
One difference between them is that the items belonging
to a list can be of different data type.
The values stored in a list can be accessed using the [ ]
operator
Index starts at 0.
4.
5.
6. What is the result of this code?
nums = [5, 4, 3, 2, 1]
print(nums[-2])
8. Create a list, areas, that contains the area of the hallway (hall), kitchen
(kit), living room (liv), bedroom (bed) and bathroom (bath), in this order.
Use the predefined variables.
Print areas with the print() function.
# area variables (in square meters)
hall = 11.25
kit = 18.0
liv = 20.0
bed = 10.75
bath = 9.50
# Create list areas
_____________________________
# Print areas
_____________________________
9. SN Function with Description
1 len(list)
Gives the total length of the list.
2 max(list)
Returns item from the list with max value.
3 min(list)
Returns item from the list with min value.
4. sum(list)
Returns the sum of all elements of list
10.
11.
12. Two List can be combined using +
operator.
We cannot add a integer with list using +
13. Forming new lists with a repeating sequence using the
multiplication operator:
Create a list of 5 elements with initial value as 0
14. Insertion
Append(ele) Add element at end of List
Insert(index , ele) Add element at given index
Extend( seq ) Appends all the elements of seq to list
Deletion Pop(index) Delete element based on Index
Remove(key) Delete element based on key
Count(key) Returns the number of occurrences of key
Index(key) Returns the index of key element
Sort Sorts elements of List
Reverse Reverses elements of list
15. We can add elements to list.
To add one element, use append method.
This adds an item to the end of an existing list.
mylist = []
mylist.append(5)
mylist.append(8)
mylist.append(12)
#prints [5,8,12]
print(mylist)
18. Insert the elements in sorted order.
Algorithm
If list is empty, append element to list.
If element is less than first element, insert front.
Traverse till current element is less than
element to insert or till end of list
Insert the element.
19. We can add a series of elements using extend
method or + operator.
20. a = [1,2,3]
b = [4,5,6]
Understand the difference between
a.append(b) and
a.extend(b)
23. Remove method takes the key element to
delete and removes it if present.
24.
25.
26.
27.
28.
29. In all the previous methods, we were not able
to determine the index where the key
element is present.
30. index() method finds the given element in
a list and returns its position.
If the same element is present more than
once, index() method returns its
smallest/first position.
If not found, it raises
a ValueError exception indicating the
element is not in the list.
31. The index method finds the first occurrence of a list item and
returns its index.
If the item isn't in the list, it raises aValueError.
letters = ['p', 'q', 'r', 's', 'p', 'u']
print(letters.index('r'))
print(letters.index('p'))
print(letters.index('z'))
2
0
ValueError: 'z' is not in list
32.
33.
34. The sort method can be used to sort the
elements of list.
48. Python List
Convert a list of temperature values from
celsius to fahrenheit
49. Given a list of elements, create a new list which
has elements with one added to every element
of old list.
nums = [12, 8, 21, 3, 16]
new_nums = [13, 9, 22, 4, 17]
50. Collapse for loops for building lists into a
single line
Components
Iterable
Iterator variable
Output expression
51.
52.
53. Given a list of elements extract all the odd
elements and place in new list.
54.
55. Conditionals on the iterable
[num ** 2 for num in range(10) if num % 2 == 0]
Conditionals on the output expression
[num ** 2 if num % 2 == 0 else 0 for num in
range(10)]
56. The all() function returns True if all elements of the
supplied iterable are true or if there are no elements.
So if all elements in a list, tuple, or set match Python's
definition of being true, then all() returns True.
57. The any() function is the converse of
the all() function. any() returns True if any element of
the iterable evaluates true.
If the iterable is empty, the function returns False
58. Read the marks of subject and find the result.
Print Pass if marks scored in all the subjects is
>= 35 otherwise Fail.
59. The zip() function take iterables, makes iterator that aggregates elements
based on the iterables passed, and returns an iterator of tuples.