0% found this document useful (0 votes)
2 views24 pages

Python Programming-Week 7

This document covers the topic of Lists and Tuples in Python programming, including their creation, methods, and key concepts such as indexing, slicing, and immutability. It outlines learning objectives for students, provides examples of looping through lists, and explains the use of the 'in' keyword. Additionally, it details list methods and the differences between lists and tuples, emphasizing when to use each data structure.

Uploaded by

hammadnaeem437
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)
2 views24 pages

Python Programming-Week 7

This document covers the topic of Lists and Tuples in Python programming, including their creation, methods, and key concepts such as indexing, slicing, and immutability. It outlines learning objectives for students, provides examples of looping through lists, and explains the use of the 'in' keyword. Additionally, it details list methods and the differences between lists and tuples, emphasizing when to use each data structure.

Uploaded by

hammadnaeem437
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

Python Programming

Topic of the Week: Lists & Tuples


Instructor: [Link] Zulfiqar
Contents
● Lists & Tuples
○ List creation
○ Indexing and slicing
○ List methods (append, insert, remove, pop, sort)
○ Tuples and immutability
Learning Objectives
By the end of this lecture, students should:

● loop through lists using for


● use range() properly
● understand index vs value
● use in keyword for iteration and checking
Looping Through Lists
Basic Concept
A list contains multiple values.

Example:

fruits = ["apple", "banana", "mango"]


Loop Through List (Value-Based)
for item in fruits:

print(item)
Output:
apple
banana
mango

Explanation:

● item stores each value one by one


● no need to use index

Important Point
This is the simplest and most recommended way
Index vs Value
Value-Based Loop Index-Based Loop
for item in fruits: for i in range(len(fruits)):
print(item) print(i, fruits[i])

● gives actual values Output:


● simple and clean
0 apple
1 banana
2 mango
Difference

Index Value

position actual data

0,1,2 apple, banana

used for access used for display

When to use index?


● when you need position
● when modifying list
● when comparing elements
range() Function
Example 2:
for i in range(1, 6):
What is range()? print(i)

Used to generate numbers. Output:

12345
Syntax
range(start, stop, step)
Example 3:
for i in range(0, 10, 2):
Examples print(i)

Output:
Example 1:
for i in range(5): 02468
print(i)

Output:
Key Points
01234
● start is included
● stop is NOT included
● step is optional
Using range() with Lists
fruits = ["apple", "banana", "mango"]

for i in range(len(fruits)):
print(fruits[i])
in Keyword
A. Used in loops
for item in fruits:
print(item)

B. Used for checking


if "apple" in fruits:
print("Found")

C. Not in list
if "orange" not in fruits:
print("Not Found")

Important Idea
in checks existence
Practice Examples

Example 1: Sum of list


numbers = [1, 2, 3, 4]
total = 0

for num in numbers:


total += num

print(total)

Example 2: Count elements


count = 0

for item in fruits:


count += 1

print(count)

Example 3: Print even numbers


for i in range(2, 11, 2):
print(i)
Lab Practice

Task 1: List Traversal


Task 4: Pattern Printing
● create a list of 5 names Pattern 1:
● print all names using loop
*
**
***
Task 2: Index + Value ****

● print index and value together Code:

for i in range(1, 5):


print("*" * i)
Task 3: Sum of numbers
● take list
● calculate sum Pattern 2:
1
12
123
1234
Lists & Tuples
List Creation, Indexing and Slicing
List Creation 2. Omitting start index 4. Reverse slicing
numbers = [10, 20, 30] nums = [10, 20, 30, 40, 50]
names = ["Ali", "Sara"] nums = [10, 20, 30, 40, 50]

print(nums[:3]) print(nums[::-1])
Indexing
print(numbers[0]) # 10 Output: Output:
print(numbers[-1]) # 30
[10, 20, 30] [50, 40, 30, 20, 10]
Basic slicing (start to end) Starts from beginning up to index 2 Reverses the list using negative step
print(numbers[0:2]) # [10, 20]
3. Using step (skip values)
nums = [10, 20, 30, 40, 50] nums = [10, 20, 30, 40, 50, 60]
print(nums[1:4])
print(nums[0:6:2])
Output:
Output:
[20, 30, 40]
[10, 30, 50]
From index 1 to 3 (4 is excluded)
Takes every 2nd element
List Methods
1. append() 2. insert() 3. remove()
numbers = [10, 20, 30] numbers = [10, 20, 30] numbers = [10, 20, 30]
[Link](40) [Link](1, 15) [Link](20)
print(numbers)
print(numbers) print(numbers)
Output:
Output: Output:
[10, 20, 30, 40]
[10, 15, 20, 30] [10, 30]
Adds element at the end
Removes the value 20
Adds 15 at index 1

Important Concept : Lists are mutable (changeable)


List Methods
4. pop() 5. sort() 6. reverse()
numbers = [10, 20, 30] numbers = [30, 10, 20] numbers = [10, 20, 30]
[Link]() [Link]() [Link]()
print(numbers)
print(numbers) print(numbers)
Output:
Output: Output:
[10, 20]
[10, 20, 30] [30, 20, 10]
Removes last element

Sorts in ascending order Reverses the list

Important Concept : Lists are mutable (changeable)


Tuples
Definition
A tuple is like a list but cannot be changed

Example
t = (1, 2, 3)

Access
print(t[0])

Immutability
t[0] = 5 # ERROR
Tuple Methods
count()
t = (10, 20, 10, 30)
print([Link](10))

Output:

index()
t = (10, 20, 30)
print([Link](20))

Output:

1
Can we add tuples? (t1 + t2)
Yes — this is called concatenation

t1 = (1, 2)
t2 = (3, 4)

t3 = t1 + t2
print(t3)

Output:

(1, 2, 3, 4)

It creates a new tuple

Original tuples remain unchanged


List → Tuple conversion
lst = [10, 20, 30]

t = tuple(lst)
print(t)

Output:

(10, 20, 30)


Tuple → List conversion
t = (10, 20, 30)

lst = list(t)
print(lst)

Output:

[10, 20, 30]


Tuple packing & unpacking
Tuple packing Single element tuple Immutability confusion
t = 1, 2, 3 t = (5,) # correct t = (1, 2, 3)
print(t)
# t[0] = 5 Error
Without comma:
(1, 2, 3)
But:
t = (5) # NOT a tuple
t = (1, [2, 3])
Tuple unpacking t[1][0] = 99 # allowed
t = (10, 20, 30) Nested tuple
a, b, c = t Tuple is fixed, but inside mutable objects
t = (1, (2, 3), 4)
can change
print(a, b, c) print(t[1][0])

10 20 30 2
Difference

List Tuple

[] ()

mutable immutable

changeable fixed
When to use Tuple?
● fixed data
● security needed
● faster execution

You might also like