pyhon 2pog3
pyhon 2pog3
# Swap function
def swapList(newList):
size = len(newList)
# Swapping
temp = newList[0]
newList[0] = newList[size - 1]
newList[size - 1] = temp
return newList
# Driver code
print(swapList(newList))
Output
[24, 35, 9, 56, 12]
# Swap function
def swapList(list):
return list
# Driver code
newList = [12, 35, 9, 56, 24]
print(swapList(newList))
sum_numbers = 0
for x in items:
sum_numbers += x
return sum_numbers
print(sum_list([1,2,-8]))
Output
-5
max = list[ 0 ]
for a in list:
if a > max:
max = a
return max
Output
2
4. Write a Python program to remove duplicates from a list.
a = [10,20,30,20,10,50,60,40,80,50,40]
dup_items = set()
uniq_items = []
for x in a:
if x not in dup_items:
uniq_items.append(x)
dup_items.add(x)
print(dup_items)
Output
{40, 10, 80, 50, 20, 60, 30}
5. Write a Python program to count the number of strings from a given list of
strings. The string length is 2 or more and the first and last characters are the
same.
def match_words(words):
ctr = 0
ctr += 1
return ctr
Output
2
6. Write a Python program to print a specified list after removing the 0th, 4th
and 5th elements.
Output
['Green', 'White', 'Black']
7. Write a Python program to calculate the difference between the two lists.
list1 = [1, 3, 5, 7, 9]
list2=[1, 2, 4, 6, 7, 8]
diff_list1_list2 = list(set(list1) - set(list2))
diff_list2_list1 = list(set(list2) - set(list1))
total_diff = diff_list1_list2 + diff_list2_list1
print(total_diff)
Output
[9, 3, 5, 8, 2, 4, 6]
Output
Python
n=4
print(new_list)
Output
['p1', 'q1', 'p2', 'q2', 'p3', 'q3', 'p4', 'q4']
Output
{'Green', 'White'}
print(var1)
print(var2)
print(var3)
Output
('Black', '#000000', 'rgb(0, 0, 0)')
('Red', '#FF0000', 'rgb(255, 0, 0)')
('Yellow', '#FFFF00', 'rgb(255, 255, 0)')