python_rev
python_rev
1/20/2025‒1/21/2025
Subhodeep Chanda
Roll No: 122
UG 1 SEM 2 (NEP)
Paper Code: C1PH230212P
Syntax:
NOTE: the return value is a new list, leaving the old list
unchanged.
Demonstrations:
Example:
Example:
append()
clear()
count()
index()
insert()
1/21
pop()
____________________________________________________________
Demonstration:
Demonstration:
Here,
’Car’ is the class
my_car is an object of the class ’Car’
The ’start’ method is a behavior that can be called using the
object
Demonstration:
numbers = [1, 2, 3, 4, 5]
sum_of_elements = sum([x for x in numbers])
print("Sum of the elements:", sum_of_elements)
# DNA string
dna = "gcatgactattccttgac"
# Method 1: Using negative indexing
sixth_start = dna[5]
sixth_end = dna[-6]
# Method 2: Without using negative indexing
length_dna = len(dna)
sixth_end_non_negative = dna[length_dna - 6]
def find_median(data):
sorted_data = sorted(data)
n = len(sorted_data)
if n % 2 == 1:
return sorted_data[n // 2]
else:
mid1, mid2 = sorted_data[n // 2 - 1], sorted_data[n // 2]
return (mid1 + mid2) / 2
def print_wing_pattern():
n = 10
for i in range(n, 0, -1):
left = '*' * i
spaces = ' ' * (2 * (n - i))
right = '*' * i
print(left + spaces + right)
print_wing_pattern()
def calculate_means(numbers):
n = len(numbers)
if n == 0:
return None, None, None
arithmetic_mean = reduce(lambda x, y: x + y, numbers) / n
geometric_mean = prod(numbers) ** (1 / n)
harmonic_mean = n / reduce(lambda x, y: x + (1 / y), numbers,
0)
≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡