0% found this document useful (0 votes)
19 views28 pages

Lecture Unit 5

Python lecture notes

Uploaded by

hashimabdulatwif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views28 pages

Lecture Unit 5

Python lecture notes

Uploaded by

hashimabdulatwif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

UNIVERSITY OF LUSAKA

SCHOOL OF SOCIAL SCIENCES AND TECHNOLOGY

LECTURE~UNIT 5: Python – Lists And Tuples 1


NAME: Longwani C Perry (Mr.)
CONTACT: +260 966589151
§ Lists:
§ - Ordered, mutable collections of items
§ - Can contain elements of different data types
§ - Defined using square brackets []
§ - Example: my_list = [1, 'hello', 3.5]

§ Tuples:
§ - Ordered, immutable collections of items
§ - Can contain elements of different data types
§ - Defined using parentheses ()
§ - Example: my_tuple = (1, 'hello', 3.5)
§ Use lists for collections of items that can change.
§ Examples: To-do lists, collections of user inputs.
§ Use tuples for collections of items that should not change.
§ Examples: Coordinate pairs, configuration settings.
§ Accessing Elements:
§ - Using indices: my_list[0] returns the first element
§ - Negative indices: my_list[-1] returns the last element

§ Modifying Elements:

§ - Assigning a new value: my_list[1] = 'world'


§ - Appending elements: my_list.append('new_item')
§ - Removing elements: my_list.remove('hello')
§ Methods:
§ - append(item): Adds an item to the end of the list
§ - extend(iterable): Adds elements from an iterable to the end of the list
§ - insert(index, item): Inserts an item at a specified index
§ - remove(item): Removes the first occurrence of an item
§ - pop(index): Removes and returns the item at the specified index
§ - sort(): Sorts the list in ascending order
§ - reverse(): Reverses the order of the list

§ Operations:
§ - Concatenation: list1 + list2
§ - Repetition: list1 * 3
§ - Membership test: 'item' in list1
§ Creating an empty list: my_list = []
§ Creating a list with elements: my_list = [1, 2, 3]
§ Using list comprehensions: squares = [x**2 for x in range(10)]
§ Creating an empty tuple: my_tuple = ()
§ Creating a tuple with elements: my_tuple = (1, 2, 3)
§ Single element tuple: single_element = (1,)
§ Lists within lists: nested_list = [[1, 2, 3], [4, 5, 6]]
§ Tuples within tuples: nested_tuple = ((1, 2), (3, 4))
§ Mixed nesting: mixed = [1, (2, 3), [4, 5]]
§ Slicing syntax: list[start:end:step]
§ Examples:
§ my_list[1:3]

§ my_list[:2]
§ my_list[::2]
§ Slicing syntax: tuple[start:end:step]
§ Examples:
§ my_tuple[1:3]

§ my_tuple[:2]
§ my_tuple[::2]
§ Positive indexing: my_list[0]
§ Negative indexing: my_list[-1]
§ Positive indexing: my_tuple[0]
§ Negative indexing: my_tuple[-1]
§ Basic syntax: [expression for item in iterable]
§ Example: squares = [x**2 for x in range(10)]
§ Conditional comprehensions: [x for x in range(10) if x % 2 == 0]
§ Finding length: len(my_list)
§ Summing elements: sum(my_list)
§ Checking membership: 'a' in my_list
§ Finding length: len(my_tuple)
§ Summing elements: sum(my_tuple)
§ Checking membership: 'a' in my_tuple
§ Using a for loop: for item in my_list:
§ Using enumerate: for index, item in enumerate(my_list):
§ Using a for loop: for item in my_tuple:
§ Using enumerate: for index, item in enumerate(my_tuple):
§ append(item): Adds a single item to the end.
§ extend(iterable): Adds elements from an iterable to the end.
§ insert(index, item): Inserts an item at a specified index.
§ remove(item): Removes the first occurrence of an item.
§ pop(index): Removes and returns the item at the specified index.
§ clear(): Removes all items from the list.
§ sort(): Sorts the list in ascending order.
§ reverse(): Reverses the order of the list.
§ Once created, tuple elements cannot be changed.
§ To change elements, create a new tuple.
§ Converting list to tuple: tuple(my_list)
§ Converting tuple to list: list(my_tuple)
§ - Lists and tuples are fundamental data structures in Python.
§ - Lists are mutable and versatile, while tuples are immutable and suitable for fixed
collections.
§ - Understanding how to access, modify, and utilize list methods and operations is
crucial for efficient programming.
Questions????

Subscribe to my YouTube Channel !!!


For more ICT lessons

Perlongs Computing

28

You might also like