Python Last
Python Last
ROLL NO: 23
def swap_numbers(a,b):
print(" b= ",b)
a=a+b
b=a-b
a=a-b
print("a = ",a)
print("b = ",b)
swap_numbers(n1,n2)
1|Page
NAME: KARDANI NIP K. ROLL NO: 23
byte_array = bytearray([10,20,30,40,50])
for i in byte_array:
print(i)
byte_array[2] = 60
for i in byte_array:
print(i)
5.Write a program to find out and display the common and the non common
elements in the list using membership operators
2|Page
NAME: KARDANI NIP K. ROLL NO: 23
common_ele = []
for i in list1:
if i in list2:
common_ele.append(i)
non_common_ele = []
for i in list1:
if i not in list2:
non_common_ele.append(i)
for i in list2:
if i not in list1:
non_common_ele.append(i)
a = [5,2,6,7,2,1,3,9]
b = [1,8,3,6,2,4,9,5]
find_common_el(a,b)
3|Page
NAME: KARDANI NIP K. ROLL NO: 23
7.Write a program that evaluates an expression given by the user at run time
using eval() function. Example:
Enter and expression: 10+8-9*2-(10*2)
Result: -20
result = eval(expression)
print("result ",result)
9.Write a program to search an element in the list using for loop and also
demonstrate the use of “else” with for loop.
def search_element(element,lst):
if item == element:
print("element found")
break
4|Page
NAME: KARDANI NIP K. ROLL NO: 23
print("not found")
my_list = [1,2,3,4,5,6]
search_element(3,my_list)
search_element(7,my_list)
Unit 2
array = [1,2,3,4,5,6,7,8]
print(array)
new_array = []
new_array[:] = array
5|Page
NAME: KARDANI NIP K. ROLL NO: 23
my_array = array('i',[1,2,3,4,5])
my_array.append(6)
my_array.insert(2,10)
my_array.remove(3)
popped_ele = my_array.pop(4)
index = my_array.index(4)
print("index of 4 ",index)
my_list = my_array.tolist()
6|Page
NAME: KARDANI NIP K. ROLL NO: 23
count = my_array.count(2)
print("count of 2 ",count)
def search_element_position(arr,element):
try:
position = arr.index(element)
return position
except ValueError:
return -1
my_array = array('i',[10,20,30,40,50])
element = 30
7|Page
NAME: KARDANI NIP K. ROLL NO: 23
if position != -1:
else:
7.Create one function that has only one function and returns the results of
addition, subtraction, multiplication and division.
def maths(ele):
minimum = min(ele)
maximum = max(ele)
total = sum(ele)
print("minimum is ",minimum)
print("maximum is ",maximum)
print("sum is ",total)
print("average ",average)
8|Page
NAME: KARDANI NIP K. ROLL NO: 23
maths(elements)
def po_ar(a,b,c):
print(a,b,c)
def key_ar(a,b,c):
print(a,b,c)
def def_ar(a,b,c=3):
print(a,b,c)
a=1
b=2
c=3
po_ar(a,b,c)
9|Page
NAME: KARDANI NIP K. ROLL NO: 23
key_ar(a = 1 ,b = 2 ,c = 3)
def_ar(a,b)
10 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23
Employee.py
def calculate_da(basic_salary):
def calculate_hra(basic_salary):
def calculate_pf(basic_salary):
def calculate_itax(basic_salary):
import emp
def calculate_gross_salary(basic_salary):
da = emp.calculate_da(basic_salary)
hra = emp.calculate_hra(basic_salary)
pf = emp.calculate_pf(basic_salary)
return gross_salary
11 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23
itax = emp.calculate_itax(basic_salary)
gross_salary = calculate_gross_salary(basic_salary)
return net_salary
basic_salary = 50000
gross_salary = calculate_gross_salary(basic_salary)
net_salary = calculate_net_salary(basic_salary)
15.Write a program to combine two List, perform repetition of lists and create
cloning of lists.
list1 = [1,2,3]
list2 = [4,5,6]
12 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23
repeated_list = list1 * 3
original_list = [7,8,9]
cloned_list = list(original_list)
nested_list = [[1,2,3],[4,5,6],[7,8,9]]
for i in sublist:
print()
13 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23
def get_key(item):
return item[1]
def sort_nested_tuple(tuple_to_sort):
sorted_tuple = tuple(sorted(tuple_to_sort,key=get_key))
return sorted_tuple
sorted_tuple = sort_nested_tuple(tuple_to_sort)
21.Create a dictionary that will accept cricket players name and scores in a
match. Also we are retrieving runs by entering the player’s name.
player_scores = {}
while True:
if player_name == 'exit':
14 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23
player_scores[player_name] = score
while True:
if player_name == 'exit':
break
if player_name in player_scores:
runs = player_scores[player_name]
else:
15 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23
def function(arg):
function(a)
16 | P a g e