Lecture # 12 - Lists and For Loop

Lecture # 12 - Lists and For Loop

List in Python. For Loop in Python

List:

In Python, a list is a collection of items that can hold different data types, such as integers, strings, or even other lists. Lists are ordered, mutable (modifiable), and allow duplicate elements.

Syntax:

my_list = [element1, element2, element3, ...]

Creating a List:

# Creating a list of integers
nums = [1, 2, 3, 4, 5]
# Creating a list of strings
names = ['Abdullah', 'Ali', 'Bilal']
# Creating a mixed-type list
info = [20, 'Abdulah', 2.57, True]

Accessing Elements:

nums = [1, 2, 3, 4, 5]
names = ['Abdullah', 'Ali', 'Bilal']
info = [20, 'Abdulah', 2.57, True]
print(nums[0])
print(names[2])
print(info[3])

We can access the elements in the list by their index number. The first element in the list has the index 0.

Output:

Modifying Elements:

info = ['Abdullah', 20, 3.14]
print(info)
info[1] = 'Student'
print(info)

We can modify an element at our specified index number. info[1] = 'Student' means that modify the element present at index 1 with new element Student.

Output:

List Slicing:

numbers = [1, 2, 3, 4, 5]
print(numbers[1:4])

You can create a sublist (slice) of a list by specifying a range of indices.

Output:

Concatenating Lists:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
conc_list = list1 + list2
print(conc_list)

Output:

Adding Element to End of a List:

names = ['Abdullah', 'Ali', 'Bilal']
print('Before Append: ', names)
names.append('Fasih')
print('After Append: ', names)

Output:

Removing Element from a List:

names = ['Abdullah', 'Ali', 'Bilal']
print('Before Remove: ', names)
names.remove('Bilal')
print('After Remove: ', names)

Output:

Print Length of a List:

nums = [1, 2, 3, 2, 1, 2, 2, 44, 4, 5]
print('Length of list: ', len(nums))

Output:

Sort the List:

nums = [1, 2, 3, 2, 1, 2, 2, 44, 4, 5]
print('List: ', nums)
nums.sort()
print('Sorted List:', nums)

Output:

Reverse the List:

nums = [1, 2, 3, 2, 1, 2, 2, 44, 4, 5]
print('List: ', nums)
nums.reverse()
print('Reversed List: ', nums)

Output:

Remove Last Element from List:

nums = [1, 2, 3, 2, 1, 2, 2, 44, 4, 5]
print('List: ', nums)
nums.pop()
print('After pop: ', nums)

Output:

Adding Element in a List:

nums = [1, 2, 3, 4, 5]
print('List: ', nums)
nums.insert(0, 66)
print('After Insertion:', nums)

Syntax of insert is: insert(index, element) . insert is used to add an element to a specified index.

Output:

Extend a List:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
print('List 1: ', list1)
list1.extend(list2)
print('Extended List: ', list1)

The extend() function adds all the elements of an iterable (such as a list, tuple, or string) to the end of the list

Output:

For Loop:

In Python, a for loop is used to iterate over a sequence (such as a list, tuple, string, or range) or any other iterable object.

Syntax:

for element in iterable:
    # Code block to be executed for each element in the iterable

Example 1:

num = int(input("Enter a Number: "))
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in numbers:
    print(f"{num} X {i} = {num*i}")

This iteration is over a list.

Output:

Example 2:

for i in range(5):
    print(i)

This iteration is over a range. The first index of range will be 0.

Output: