Skip to main content

Python Unit-1 University Program


UNIT - 1


==>1.

'''PRO[1]->Write a program to swap two numbers
without taking a temporary variable'''

a= int(input('enter Num1: '))
b = int(input('enter Num2: '))

print(":::BEFORE SWAP:::")
print(f'Num1={a}')
print(f'Num2={b}')

b,a=a,b

print(":::AFTER SWAP:::")
print(f'Num1={a}')
print(f'Num2={b}')



 ==>2.

'''PRO[2]->Write a program to display sum of two
complex numbers'''

# Define two complex
complex_num1_real =float(input('Enter Num1_real: '))#3 + 2j
complex_num1_imag =float(input('Enter Num1_imag: '))
   
complex_num2_real = float(input('\nEnter Num2_real: '))#2 + 2j
complex_num2_imag = float(input('Enter Num2_imag: '))

complex_num1 = complex(complex_num1_real,complex_num1_imag)
complex_num2 = complex(complex_num2_real,complex_num2_imag)
   
# Calculate the sum of the complex numbers
sum_complex = complex_num1 + complex_num2

# Display the result
print("Complex Number 1:", complex_num1)
print("Complex Number 2:", complex_num2)
print("Sum:", sum_complex)


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

byteArray = array.array('b',[])#b is for byte type
n=int(input('enter size of array: '))

for i in range(n):
    el=int(input('enter element: '))
    byteArray.append(el)

for j in range(n):
    print(byteArray[j],end=' ')


#bytearray basically array of byte    
print("Original Array: ", bytearray(byteArray))
byteArray[0] = 4
byteArray[1] = 5
byteArray[2] = 6
print("Modified Array: ", bytearray(byteArray))






==>4.
'''PRO[4]->Create a sequence of numbers using
range datatype to
display 1 to 30, with an increment of 2.'''

'''for i in range(1,300,2):
   print(i,end=' ')'''

lst1 = [i for i in range(1,300,2)]
print(lst1)

==>5.
'''PRO[5]->Write a program to find out and display
the common and the non common elements in the
list using membership operators'''

lst1=[]
print(":::::LIST1:::::")
n1=int(input('enter size of list1:'))
for i in range(n1):
    el1=int(input('enter element of list: '))
    lst1.append(el1)

for j in range(0,7):
    print("*-*-*",end='')
#print(lst)

print("\n:::::LIST2:::::")
lst2=[]
n2=int(input('enter size of list2:'))
for i in range(n2):
    el2=int(input('enter element of list: '))
    lst2.append(el2)

#print(lst1)
#print(lst2)

#comparing elements
#1.common 2.uncommon
com=[]
uncom=[]
for val in lst1:
    if val in lst2:
        if val not in com:
            com.append(val)
    else:
        uncom.append(val)

for val in lst2:
    if val not in com:
        uncom.append(val)
for j in range(0,7):
    print("*-*-*",end='')
   
print("\nCOMMON ELEMENTS IN LIST: \n",com)
print("UNCOMMON ELEMENTS IN LIST: \n",uncom)


==>6.
'''PRO[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 = 10
b = 11

print("id(a): ",id(a))#id(a):140718104962120
print("id(b): ",id(b))#id(b):140718104962152
c=10
print("id(c): ",id(c))#id(c):140718104962120 as a and c
#value(10) is same so same id

d = b
print("id(d): ",id(d))#same id as id(b)

print(a is c)#return true as both value is same

#try to create a list obj and check
lst1=[12,34,53]
lst2=[12,34,53]
lst3=[12,34,54]
lst4=lst1
print("id(lst1):",id(lst1))
print("id(lst2):",id(lst2))
print("id(lst3):",id(lst3))
print("id(lst4):",id(lst4))

print("lst1 is lst4: ",lst1 is lst4)
ax = 256
xc = 256
print(id(ax))#140718104969992
print(id(xc))#140718104969992
ax=232
xc=232
print(ax is xc)
#now check for num>256

ac = 257
ca = 257
print(id(ac))
print(id(ca))
print(ac is ca)


==>7.
'''PRO[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)


==>8.
'''PRO[8]->Write a python program to find the
sum of even numbers
using command line arguments.'''

import sys

n=len(sys.argv)
print("\nNumbers are passed:", end = " ")
for i in range(1, n):
    print(sys.argv[i], end = " ")
     
# Addition of even numbers
Sum = 0

for i in range(1, n):
    vl=int(sys.argv[i])
    if vl%2==0:
        Sum=Sum+vl
     
print("\n\nResult:", Sum)

==>9.
'''PRO[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)'''

def circle(r):
    pi=3.14
    area=pi*r*r
    print("Area of Circle is : %.2f"%area)

def square(s):
    area=s*s
    print("Area of sqaure is : %.2f"%area)

def rectangle(l,b):
    area=l*b
    print("Area of Rectangle is : %.2f"%area)

def triangle(bs,h):
    area=1/2*(bs*h)
    print("Area of triangle is : %.2f"%area)

def simpleintrest(p,n,r):
    result=(p*n*r)/100
    result=result+p
    print("Total Amount: %2.f"%result)

while True:

    print("-=-=-=-MENU-=-=-=-")
    print("1.circle")
    print("2.Square")
    print("3.Rectangle")
    print("4.Triangle")
    print("5.Simple Intrest")
    print("6.Exit")

    ch=int(input("Enter your choice: "))

    if ch==1:
        r = float(input("Enter radis : "))
        circle(r)
    elif ch==2:
        s=float(input("Enter Side: "))
        square(s)
    elif ch==3:
        l=float(input("Enter length: "))
        b=float(input("Enter Breadth: "))
        rectangle(l,b)
    elif ch==4:
        bs=float(input("Enter base: "))
        h=float(input("Enter height: "))
        triangle(bs,h)
    elif ch==5:
        p=float(input("Enter Amount: "))
        n=int(input("Enter No. of year: "))
        r=float(input("Enter rate: "))
        simpleintrest(p,n,r)
    elif ch==6:
        break
    else:
        print("!!Enter Valid Choice between 1-5!!")


==>10.
'''PRO[10]-> Write a program to assert the user
enters a number
greater than zero'''

val=int(input('enter number: '))
assert val>0,"Please enter number greter than 0"

print("Entered number is",val)


==>11.
'''PRO[11]->Write a program to search an element
in the list using
for loop and also demonstrate the use of
“else” with for
loop'''

lst=[]
n=int(input('enter size of list'))

for i in range(n):
    el=int(input("Enter Element: "))
    lst.append(el)

print(lst)

val=int(input("Enter value for search"))

for i in lst:
    #flage=0
    if i == val:
        print(f"Element{val}is founded on
        {lst.index(i)+1} position")
        #flage=1
        break

else:
    print(f'{val} not found')

==>12.
'''PRO[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).'''

cm=float(input('Enter length in Centimeter(cm.) : '))
assert cm>0,"Please enter length greter than 0"

inch=cm/2.54

print(f"{cm}Cm = %.2finch."%inch)