0% found this document useful (0 votes)
6 views

pyhon 2pog3

The document contains multiple Python programs demonstrating various list operations, including swapping elements, summing items, finding the largest number, removing duplicates, counting specific strings, and calculating differences between lists. Each program is accompanied by example input and output to illustrate its functionality. The programs collectively showcase fundamental list manipulation techniques in Python.

Uploaded by

mamatha.pragada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

pyhon 2pog3

The document contains multiple Python programs demonstrating various list operations, including swapping elements, summing items, finding the largest number, removing duplicates, counting specific strings, and calculating differences between lists. Each program is accompanied by example input and output to illustrate its functionality. The programs collectively showcase fundamental list manipulation techniques in Python.

Uploaded by

mamatha.pragada
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

# Python3 program to swap first and last element of a list

# 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

newList = [12, 35, 9, 56, 24]

print(swapList(newList))

Output
[24, 35, 9, 56, 12]

# Swap function
def swapList(list):

start, *middle, end = list


list = [end, *middle, start]

return list

# Driver code
newList = [12, 35, 9, 56, 24]

print(swapList(newList))

2. Write a Python program to sum all the items in a list.


def sum_list(items):

sum_numbers = 0

for x in items:

sum_numbers += x

return sum_numbers

print(sum_list([1,2,-8]))

Output
-5

3. Write a Python program to get the largest number from a list.


def max_num_in_list( list ):

max = list[ 0 ]

for a in list:

if a > max:

max = a

return max

print(max_num_in_list([1, 2, -8, 0]))

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

for word in words:


if len(word) > 1 and word[0] == word[-1]:

ctr += 1

return ctr

print(match_words(['abea', 'xyz', 'aba', '1220']))

Output
2
6. Write a Python program to print a specified list after removing the 0th, 4th
and 5th elements.

color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']


color = [x for (i,x) in enumerate(color) if i not in (0,4,5)]
print(color)

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]

8. Write a Python program to convert a list of characters into a string.

s = ['p', 'y', 't', 'h' , 'o', 'n']


str1 = ''.join(s)
print(str1)

Output
Python

9. Write a Python program to create a list by concatenating a given list with a


range from 1 to n.

my_list = ['p', 'q']

n=4

new_list = ['{}{}'.format(x, y) for y in range(1, n+1) for x in my_list]

print(new_list)

Output
['p1', 'q1', 'p2', 'q2', 'p3', 'q3', 'p4', 'q4']

Write a Python program to find common items in two lists.


color1 = "Red", "Green", "Orange", "White"

color2 = "Black", "Green", "White", "Pink"

print(set(color1) & set(color2))

Output
{'Green', 'White'}

Write a Python program to split a list into different variables.

color = [("Black", "#000000", "rgb(0, 0, 0)"), ("Red", "#FF0000", "rgb(255, 0, 0)"),

("Yellow", "#FFFF00", "rgb(255, 255, 0)")]

var1, var2, var3 = color

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)')

You might also like