Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

NAME: KARDANI NIP K.

ROLL NO: 23

SUB: PYTHON DIV: ‘A’

1.Write a program to swap two numbers without taking a temporary variable.

def swap_numbers(a,b):

print("before swapping ")

print(" a= " ,a)

print(" b= ",b)

a=a+b

b=a-b

a=a-b

print("after swapping ")

print("a = ",a)

print("b = ",b)

n1 = int(input("Enter first number "))

n2 = int(input("Enter second number "))

swap_numbers(n1,n2)

1|Page
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


3.Write a program to create a byte type array, read, modify, and display the
elements of the array

byte_array = bytearray([10,20,30,40,50])

print("original array ")

for i in byte_array:

print(i)

byte_array[2] = 60

print("after modify ")

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

SUB: PYTHON DIV: ‘A’


def find_common_el(list1,list2):

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)

print("common elements ",common_ele)

print("non common elements ",non_common_ele)

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

SUB: PYTHON DIV: ‘A’

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

expression = input("enter an expression ")

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

for item in lst:

if item == element:

print("element found")

break

4|Page
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


else:

print("not found")

my_list = [1,2,3,4,5,6]

search_element(3,my_list)

search_element(7,my_list)

Unit 2

1.Write a program to create one array from another array.

array = [1,2,3,4,5,6,7,8]

print("original array ")

print(array)

new_array = []

new_array[:] = array

print("new array ",new_array)

5|Page
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’

3.Write a program to understand various methods of array class mentioned:


append, insert, remove, pop, index, tolist and count.

from array import array

my_array = array('i',[1,2,3,4,5])

my_array.append(6)

print("after appending ",my_array)

my_array.insert(2,10)

print("after insert ",my_array)

my_array.remove(3)

print("after remove ",my_array)

popped_ele = my_array.pop(4)

print("popped element at index 4 ",popped_ele)

print("after popping element ",my_array)

index = my_array.index(4)

print("index of 4 ",index)

my_list = my_array.tolist()

6|Page
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


print("array converted to list ",my_list)

count = my_array.count(2)

print("count of 2 ",count)

5. a program to search the position of an element in an array using index()


method of array class.

from array import array

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

SUB: PYTHON DIV: ‘A’


position = search_element_position(my_array,element)

if position != -1:

print(f"the element {element} is found at position {position} ")

else:

print(f"the element {element} is not found in the array ")

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)

average = total / len(ele)

print("minimum is ",minimum)

print("maximum is ",maximum)

print("sum is ",total)

print("average ",average)

elements = tuple(map(int,input("enter elements ").split(",")))

8|Page
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’

maths(elements)

9.Write a program to demonstrate the use of Positional argument, keyword


argument and default arguments.

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

print("positional argument function ")

po_ar(a,b,c)

9|Page
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


print("keywprd argument function ")

key_ar(a = 1 ,b = 2 ,c = 3)

print("default argunment function ")

def_ar(a,b)

11.Write a lambda/Anonymous function to find bigger number in two given


numbers.

x = int(input("enter first number "))

y = int(input("enter second number "))

find_bigger = lambda x,y: max(x,y)

print("bigger number ",find_bigger(x,y))

10 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’

13.Create a program name “employee.py” and implement the functions DA,


HRA, PF, and ITAX. Create another program that uses the function of
employee module and calculates gross and net salaries of an employee.

Employee.py

def calculate_da(basic_salary):

return 0.15 * basic_salary

def calculate_hra(basic_salary):

return 0.10 * basic_salary

def calculate_pf(basic_salary):

return 0.12 * basic_salary

def calculate_itax(basic_salary):

return 0.20 * 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)

gross_salary = basic_salary + da + hra - pf

return gross_salary

11 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


def calculate_net_salary(basic_salary):

itax = emp.calculate_itax(basic_salary)

gross_salary = calculate_gross_salary(basic_salary)

net_salary = gross_salary - itax

return net_salary

basic_salary = 50000

gross_salary = calculate_gross_salary(basic_salary)

net_salary = calculate_net_salary(basic_salary)

print("basic salary ",basic_salary)

print("gross salary ",gross_salary)

print("net salary ",net_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]

combined_list = list1 + list2

12 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


print("Combined list ",combined_list)

repeated_list = list1 * 3

print("repeated list ",repeated_list)

original_list = [7,8,9]

cloned_list = list(original_list)

print("cloned list ",cloned_list)

17.Write a program to create nested list and display its elements.

nested_list = [[1,2,3],[4,5,6],[7,8,9]]

for sublist in nested_list:

for i in sublist:

print(i ,end =' ')

print()

13 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


19.Create a program to sort tuple with nested tuples.

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

tuple_to_sort = (("nip",22) , ("meet",20) , ("raj",21))

sorted_tuple = sort_nested_tuple(tuple_to_sort)

print("sorted tuple ",sorted_tuple)

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:

player_name = input("enter player name (or exit to finish): ")

if player_name == 'exit':

14 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


break

score = int(input("enter score for {} ".format(player_name)))

player_scores[player_name] = score

while True:

player_name = input("enter player's name to retrieve runs (or exit to finish)")

if player_name == 'exit':

break

if player_name in player_scores:

runs = player_scores[player_name]

print("runs scored by {} : {} ".format(player_name,runs))

else:

print("player not found")

15 | P a g e
NAME: KARDANI NIP K. ROLL NO: 23

SUB: PYTHON DIV: ‘A’


23.Create a python function to accept python function as a dictionary and
display its elements.

def function(arg):

for key in arg:

print(f"key {key} value : {arg[key]}")

a = {'a' : 1 ,'b':2 , 'c':3}

function(a)

16 | P a g e

You might also like