0% found this document useful (0 votes)
6 views10 pages

Python Sequences Programs

The document provides a comprehensive guide on various list operations in Python, including methods for adding, removing, and manipulating list elements. It includes examples of using functions like append, insert, extend, remove, pop, and more, along with practical programs demonstrating list transformations and calculations. Additionally, it covers advanced operations such as zipping multiple lists together and filtering elements based on conditions.
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)
6 views10 pages

Python Sequences Programs

The document provides a comprehensive guide on various list operations in Python, including methods for adding, removing, and manipulating list elements. It includes examples of using functions like append, insert, extend, remove, pop, and more, along with practical programs demonstrating list transformations and calculations. Additionally, it covers advanced operations such as zipping multiple lists together and filtering elements based on conditions.
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

# Creating a list

fruits = ['apple', 'banana', 'cherry']

# 1. append() - Adds an element to the end of the list


[Link]('date')
print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']

# 2. insert() - Inserts an element at a specific index


[Link](1, 'grape')
print(fruits) # Output: ['apple', 'grape', 'banana', 'cherry', 'date']

# 3. extend() - Appends multiple elements at the end of the list


[Link](['elderberry', 'fig'])
print(fruits) # Output: ['apple', 'grape', 'banana', 'cherry', 'date', 'elderberry', 'fig']

# 4. remove() - Removes the first occurrence of an element


[Link]('banana')
print(fruits) # Output: ['apple', 'grape', 'cherry', 'date', 'elderberry', 'fig']

# 5. pop() - Removes and returns an element at a specific index (or the last element if no index
is specified)
removed_element = [Link](2)
print(removed_element) # Output: cherry
print(fruits) # Output: ['apple', 'grape', 'date', 'elderberry', 'fig']

# 6. index() - Returns the index of the first occurrence of an element


index = [Link]('date')
print(index) # Output: 2

# 7. count() - Returns the number of occurrences of an element


count = [Link]('apple')
print(count) # Output: 1

# 8. sort() - Sorts the list in ascending order


[Link]()
print(fruits) # Output: ['apple', 'date', 'elderberry', 'fig', 'grape']
# 9. reverse() - Reverses the order of the list
[Link]()
print(fruits) # Output: ['grape', 'fig', 'elderberry', 'date', 'apple']

# 10. clear() - Removes all elements from the list


[Link]()
print(fruits) # Output: []

# Creating a list
numbers = [5, 2, 7, 1, 8, 4]

# 11. len() - Returns the number of elements in the list


length = len(numbers)
print(length) # Output: 6

# 12. copy() - Creates a shallow copy of the list


numbers_copy = [Link]()
print(numbers_copy) # Output: [5, 2, 7, 1, 8, 4]

# 13. max() - Returns the maximum element in the list


maximum = max(numbers)
print(maximum) # Output: 8

# 14. min() - Returns the minimum element in the list


minimum = min(numbers)
print(minimum) # Output: 1

# 15. sum() - Returns the sum of all elements in the list


total = sum(numbers)
print(total) # Output: 27

# 16. pop() without index removes and returns the last element
last_element = [Link]()
print(last_element) # Output: 4
print(numbers) # Output: [5, 2, 7, 1, 8]

# 17. sort() with reverse=True sorts the list in descending order


[Link](reverse=True)
print(numbers) # Output: [8, 7, 5, 2, 1]

# 18. count() can be used to count the number of occurrences of an element


count_of_2 = [Link](2)
print(count_of_2) # Output: 1

# 19. index() can find the index of an element within a range


index_of_5 = [Link](5, 1, 4)
print(index_of_5) # Output: 2

# 20. del statement can be used to remove an element by its index


del numbers[3]
print(numbers) # Output: [8, 7, 5, 1]

Python program that adds 10 to each element in a list


# Original list
numbers = [5, 10, 15, 20, 25]

# Adding 10 to each element using a for loop


for i in range(len(numbers)):
numbers[i] += 10

# Displaying the updated list


print(numbers)
output:
[15, 20, 25, 30, 35]

Python program that multiplies each element in a list by 10


# Original list
numbers = [5, 10, 15, 20, 25]

# Multiplying each element by 10 using a list comprehension


numbers = [num * 10 for num in numbers]

# Displaying the updated list


print(numbers)

output:
[50, 100, 150, 200, 250]

Python program that adds the first and last element, second and second-to-last element, and
so on, in a list

# Original list
numbers = [2, 4, 6, 8, 10, 12]

# Initializing an empty result list


result = []

# Adding corresponding elements from the start and end of the list
for i in range(len(numbers)//2):
[Link](numbers[i] + numbers[-i-1])

# Displaying the resulting list


print(result)

output:
[14, 14, 14]

Python program that adds the elements at odd and even positions separately from a list:
# Original list
numbers = [2, 5, 7, 10, 11, 13, 16]

# Initializing variables for odd and even position sums


odd_position_sum = 0
even_position_sum = 0

# Adding elements at odd and even positions separately


for i in range(len(numbers)):
if i % 2 == 0:
even_position_sum += numbers[i]
else:
odd_position_sum += numbers[i]

# Displaying the sums


print("Sum of elements at odd positions:", odd_position_sum)
print("Sum of elements at even positions:", even_position_sum)

output:
Sum of elements at odd positions: 25
Sum of elements at even positions: 41

Python program that adds the first two elements, second two elements, and so on, in a list
# Original list
numbers = [2, 4, 6, 8, 10, 12]

# Initializing an empty result list


result = []

# Adding pairs of elements


for i in range(0, len(numbers), 2):
if i + 1 < len(numbers):
[Link](numbers[i] + numbers[i + 1])

# Displaying the resulting list


print(result)

output:
[6, 14, 22]

Python program that adds the numbers greater than 10 from a list
# Original list
numbers = [5, 10, 15, 20, 25]

# Initializing the sum variable


sum_greater_than_10 = 0

# Adding numbers greater than 10


for num in numbers:
if num > 10:
sum_greater_than_10 += num

# Displaying the sum


print("Sum of numbers greater than 10:", sum_greater_than_10)

output:
Sum of numbers greater than 10: 60

Python program that prints the square and cube of each element in a list:
# Original list
numbers = [2, 4, 6, 8, 10]

# Printing the square and cube of each element


for num in numbers:
square = num ** 2
cube = num ** 3
print(f"Number: {num}, Square: {square}, Cube: {cube}")
output:
Number: 2, Square: 4, Cube: 8
Number: 4, Square: 16, Cube: 64
Number: 6, Square: 36, Cube: 216
Number: 8, Square: 64, Cube: 512
Number: 10, Square: 100, Cube: 1000

Python program that adds 0 next to each element in a list


# Original list
numbers = [2, 4, 6, 8, 10]

# Adding 0 next to each element


result = []

for num in numbers:


[Link](num)
[Link](0)

# Displaying the resulting list


print(result)
output:
# Original list
numbers = [2, 4, 6, 8, 10]
# Adding 0 next to each element
result = []

for num in numbers:


[Link](num)
[Link](0)

# Displaying the resulting list


print(result)

output:
[2, 0, 4, 0, 6, 0, 8, 0, 10, 0]

Python program that reverses the first half and second half of a list separately:
# Original list
numbers = [1, 2, 3, 4, 5, 6, 7, 8]

# Determine the midpoint index


midpoint = len(numbers) // 2

# Reverse the first half


first_half = numbers[:midpoint]
first_half.reverse()

# Reverse the second half


second_half = numbers[midpoint:]
second_half.reverse()

# Concatenate the reversed halves


result = first_half + second_half

# Display the resulting list


print(result)

output:
[4, 3, 2, 1, 8, 7, 6, 5]
Python program that removes even elements from a list
# Original list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Removing even elements


numbers = [num for num in numbers if num % 2 != 0]

# Displaying the updated list


print(numbers)

output:
[1, 3, 5, 7, 9]

Python program that removes elements at even positions from a list


# Original list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Removing elements at even positions


numbers = [numbers[i] for i in range(len(numbers)) if i % 2 != 0]

# Displaying the updated list


print(numbers)

output:
[2, 4, 6, 8, 10]

Python program that removes elements greater than 5 from a list


# Original list
numbers = [1, 6, 2, 8, 4, 10, 3, 7, 9, 5]

# Removing elements greater than 5


numbers = [num for num in numbers if num <= 5]

# Displaying the updated list


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

Python program that removes perfect square elements from a list


import math

# Original list
numbers = [1, 4, 7, 9, 16, 25, 36]

# Removing perfect square elements


numbers = [num for num in numbers if [Link](num)**2 != num]

# Displaying the updated list


print(numbers)

output:
[1, 7, 25]

python program to add, subtract, multiply of two lists


# Original lists
list1 = [2, 4, 6, 8, 10]
list2 = [1, 3, 5, 7, 9]

# Element-wise addition
addition_result = [x + y for x, y in zip(list1, list2)]

# Element-wise subtraction
subtraction_result = [x - y for x, y in zip(list1, list2)]

# Element-wise multiplication
multiplication_result = [x * y for x, y in zip(list1, list2)]

# Displaying the results


print("Addition:", addition_result)
print("Subtraction:", subtraction_result)
print("Multiplication:", multiplication_result)

output:
Addition: [3, 7, 11, 15, 19]
Subtraction: [1, 1, 1, 1, 1]
Multiplication: [2, 12, 30, 56, 90]

usage of zip()
numbers = [1, 2, 3]
letters = ['A', 'B', 'C']
symbols = ['!', '@', '#']

# Zip the three lists together


zipped = zip(numbers, letters, symbols)

# Iterate over the zipped tuples


for item in zipped:
print(item)

output:
(1, 'A', '!')
(2, 'B', '@')
(3, 'C', '#')

You might also like