• Click below to download Unit 1 programs in pdf file: -




Unit 1


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


a=int(input("Enter value of 'a' : "))

b=int(input("Enter value of 'b' : "))

a=a+b

b=a-b

a=a-b

print("a is:",a," b is:",b)



# 2. Write a program to display sum of two complex numbers.


print('Enter value in this format : a+bj ')

a = complex(input('Enter first complex number: '))

b = complex(input('Enter first complex number: '))

c = a + b

print('Sum = ', c)



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


n=[10,20,100,150,200,0,90]

x=bytearray(n)

print("original byte array")

for i in x:

    print(i)

    print()

x[2]=255

x[5]=21

print("modified array")

for i in x:

    print(i)



# 4.Create a sequence of numbers using range datatype to display 1 to 30, with an increment of 2.


r=range(0,32,2)

for i in r:

    print(i)



# 5.Write a program to find out and display the common and the non common elements in the list using membership operators


list1=["python",10,40,50,100,"welcom"]

print("Element of list1 are:",list1)

list2=[10,140,150,100,"Good morning”, “python"]

print("Element of list2 are:",list2)

print("Common element in list1 and list2\n")

for i in list1:

    for j in list2:

        if(i==j):

         print(i)

print()

print("Uncommon elements are:")

print("of list1")

for i in  list1: 

    if i not in list2:

     print(i)

print("of list2")

for i in  list2:

    if i not in list1:

     print(i)



# 6.Create a program to display memory locations of two variables using id() function, and then use identity operators two compare whether two objects are same or not.


a=int(input("First value:"))

b=int(input("Second value:"))

print("ID of variable a is ",id(a))

print("ID of variable b is ",id(b))

if(a is b):

    print("same: by using id values")

else:

    print("Different: by using id values")

if(a==b):

    print("same: by object value comparison")

else:

    print("Different: by object value comparison")

if(a is not b):

    print("Different: by using is not operator")

else:

 print("same:by using is not operator")

""" 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 """


print(eval(input("Enter an expression:")))



""" 8.Write a python program to find the sum of even numbers

using command line arguments. """


#run in cmd

import sys

even=int(sys.argv[1])

for i in range(1,even):

    if(i %2 ==0):

        print(i)



""" 9. Write a menu driven python program which perform the

following:

Find area of circle

Find area of triangle

Find area of square and rectangle

Find Simple Interest

Exit.( Hint: Use infinite while loop for Menu) """


import sys

while(True):

 print("1 = Radius Of Circle")

 print("2 = Radius Of Triangle")

 print("3 = Radius Of Square")

 print("4 = Radius Of Rectangle")

 print("5 = Simple Interest")

 print("6 = Exit")

 Choice = int(input("Enter Value  "))

 while Choice>=1 and Choice <=6:

  if Choice == 1:

    r = int(input("Enter Radius Of Circle"))

    print("Radius Of Circle is",3.14*r*r)

    break

  elif Choice == 2:

    h = int(input("Enter Height Of Triangle"))

    b = int(input("Enter Base Of Triangle"))

    print("Area Of Triangle Is ",h*b/2)

    break

  elif Choice == 3:

    a = int(input("Enter Side Of Square : "))

    print("Area Of Square is ",a*a)

    break

  elif Choice == 4:

    l = int(input("Enter Length Of Rectangle"))

    w = int(input("Enter Width Of Rectangle"))

    print("Area Of Rectangle is ",l*w)

    break

  elif Choice == 5:

    p = int(input("Enter Price"))

    r = int(input("Enter Rate"))

    n = int(input("Enter Time"))

    print("Simple Interest Is",p*r*n/100)

    break

  elif Choice == 6:

    sys.exit()



""" 10 Write a program to assert the user enters a number

greater than zero. """


import sys

x=int(input("Enter a Number:"))

assert x>0;["Incorrect input"]

print("Your entered number",x)


""" 11. Write a program to search an element in the list using

for loop and also demonstrate the use of “else” with for

loop. """


list1 = [100,200,300,400,500]

t = int(input("Enter Value To be Found:"))

for i in list1:

    if(i == t):

        print("Value Find",i)

        break

    

else:

        print("Value Can't Be Found")


""" 12. Write a python program that asks the user to enter a

length in centimeters. If the user enters a negative

length, the program should tell the user that the entry is

invalid. Otherwise, the program should convert the

length to inches and print out the result. (2.54 = 1 inch). """


c = float(input("Enter Centimeters:"))

if(c<=-1):

    print("Enter Positive Value")

else:

    print("Inche =",c/2.54)