Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Python Programs

Gshd
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)
9 views

Python Programs

Gshd
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
You are on page 1/ 5

Python Programs

11. Python program to append elements in the list

Input:

my_list = ['geeks', 'for', 'geeks']

another_list = [6, 0, 4, 1]

my_list.append(another_list)

print(my_list)

Output:

['geeks', 'for', 'geeks', [6, 0, 4, 1]]

12. Python program to create a tuple of elements

Input:

Tuple1 = ()

print("Initial empty Tuple: ")

print(Tuple1)

Tuple1 = ('Geeks', 'For')

print("\nTuple with the use of String: ")

print(Tuple1)

list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")

print(tuple(list1))

Tuple1 = tuple('Geeks')

print("\nTuple with the use of function: ")

print(Tuple1)

Output:

Initial empty Tuple:


()

Tuple with the use of String:


('Geeks', 'For')

Tuple using List:


(1, 2, 4, 5, 6)

Tuple with the use of function:


('G', 'e', 'e', 'k', 's')

13. Python program to sort the tuple

Input:

def last(n):

return n[m]

def sort(tuples):

return sorted(tuples, key = last)

a = [(23, 45, 20), (25, 44, 39), (89, 40, 23)]


m=2

print("Sorted:"),

print(sort(a))

Output:

Sorted: [(23, 45, 20), (89, 40, 23), (25, 44, 39)]

14. Tuple program to print half in one line and another half in another line

Input:

def print_half_tuple(tup):

midpoint = len(tup) // 2

print(*tup[:midpoint])

print(*tup[midpoint:])

my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)


print_half_tuple(my_tuple)

Output:

12345

6 7 8 9 10

15. Create a phone dictionary to search a particular person name by giving


phone number

Input:
phone_dictionary = {
"Alice": "123-456-7890",
"Bob": "987-654-3210",
"Carol": "456-789-0123"
}
phone_number = input("Enter the phone number: ")
if phone_number in phone_dictionary:
name = phone_dictionary[phone_number]
print("The name associated with the phone number {} is
{}".format(phone_number, name))
else:
print("The phone number {} is not found in the phone
dictionary".format(phone_number))

Output:

Enter the phone number: 123-456-7890

The name associated with the phone number 123-456-7890 is Alice

16. Python program to merge two dictionaries

Input:

def Merge(dict1, dict2):

return(dict2.update(dict1))

dict1 = {'a': 10, 'b': 8}

dict2 = {'d': 6, 'c': 4}

print(Merge(dict1, dict2))

print(dict2)

Output:
None

{'c': 4, 'a': 10, 'b': 8, 'd': 6}

17. Write a python function to display elements in a dictionary

Input:

def display_dictionary_elements(my_dict):
for key, value in my_dict.items():
print(f"{key}: {value}")
my_dict = {"name": "John", "age": 30, "city": "New York"}
display_dictionary_elements(my_dict)

Output:

name: John

age: 30

city: New York

You might also like