Introduction To Phyton
Introduction To Phyton
Introduction to Phyton:
1.WAP to display basic details of a User like Name, Roll no, Division and
Address.
CODE:-
def personal_details():
Name = "Karan"
Rollno = "18EC054"
Division = "EC-1"
Address = "Jamnagar, Gujarat, India"
print("Name: {}\nRollno: {}\nDivision: {}\nAddress: {}".format(Name,
Rollno, Division, Address))
personal_details()
OUTPUT:
CODE:-
val1 = 178.35
val2 = 36.92
OUTPUT:-
CODE:-
num1 = 45
num2 = 12
sum = num1 + num2
print("The sum is :", sum)
CSPIT(EC)
KARAN PAREKH 18EC054
OUTPUT:-
CODE:-
num = 40
OUTPUT:-
CODE:-
principal = 0
rate = 0.6
time = 0
print('Enter Principal Amount: ')
principal = int(input())
print('Enter Tenure in Years: ')
time = int(input())
simple_interest = (principal * rate * time)/100
print('Simple Interest: ', simple_interest)
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
print('Enter Frequency: ')
frequency = int(input())
time = 1/frequency
print('Required Time: ', time)
OUTPUT:-
CODE:-
import math
print('Enter Radius: ')
rad = int(input())
circle_area = math.pi * rad * rad;
sphere_area = (4/3) * math.pi * rad*rad*rad
print('Area of circle: ', circle_area)
print('Area of sphere: ', sphere_area)
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
OUTPUT:-
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
11.Store a message in a variable, and print that message. Then change the value
of your variable to a new message, and print the new message.
CODE:-
message = "how are you" #storing the message
print("The message:",message) #printing the message
message = "I'm fine" #storing the new message
print("The message:",message)
OUTPUT:-
13. Develop a program that computes the body mass index (BMI) of an
individual. Program should begin by reading a height and weight from the user.
If you read the height in centimeters then need to convert it in meters and the
weight in kilograms.
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
OUTPUT:-
14. Store the names of a few of your friends in a list called names. Print each
person’s name by accessing each element in the list, one at a time.
CODE:-
names = ['yash', 'meet', 'rushi']
print(names[0])
print(names[1])
print(names[2])
OUTPUT:-
15.Start with the list you used in above example, but instead of just printing
each person’s name, print a message to them. The text of each message should
be the same, but each message should be personalized with the person’s name
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
names = ["rushi","meet","yash","ankit"] #list of friends
for i in names: #for loop for individual name accessing
print("Friend name is:",i,"\n")
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CSPIT(EC)
KARAN PAREKH 18EC054
17.Write a program in python that reads a number of feet from the user,
followed by a number of inches. After that program should compute and display
the equivalent number of centimeters.
CODE:-
foot = float(input("Enter the no of foot:")) #taking foots and inches
inches = float(input("Enter the no of inches:"))
inches += 12*foot #computing foots and inches in CM
CM = inches*2.54
print("No of centimeters:",CM,"cm") #printing CM value
OUTPUT:-
18.Write a program to create dictionary (key is roll number and value is student
name). Print it by key
CODE:-
dic = {1:'Ankit' ,2:'rushi' ,3:'Meet' ,4:'Jay'} #creating dictionary
for i in dic: #for printing every element of dictionary
using keys
print(dic.get(i))
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
if __name__ == "__main__":
print(input_str)
print('Reverse String using slicing =', reverse_slicing(input_str))
OUTPUT:-
new = ""
for x in s:
new += x
return new
s = ['a', 'b', 'c', 'd']
print(convert(s))
OUTPUT:-
21.Append one string to another str1= Fal str2=guni str3 should be Falguni
CODE:-
test_string ="Fal"
add_string = "guni"
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
import cmath, math, numpy
c1 = 50 + 50j
print('Magnitude: ', abs(c1))
print('Phase: ', cmath.phase(c1))
print('Phase in Degrees: ', numpy.degrees(cmath.phase(c1)))
OUTPUT:-
CODE:-
f = 2.4*(10**9) #calculations
r = 2.32
h = 0.16
c = 3*(10**8)
w = c/(2*f*(((r+1)/2)**(-1/2)))
e = (r+1)/2 + ((r-1)/2)*((1+10*h/w)**(-1/2))
l = c/(2*f*(e**1/2))
D = h/(e**1/2)
L = l-2*D
print("Value of W and L is:",round(w,2),"and",round(L,2)) #printing values of
W and L
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE :-
ZL=50+50j #initializing the given values
ZO=50
T=(ZL-ZO)/(ZL+ZO) #calculation for T
SWR=(1+T)/(1-T) #calculation for SWR
print("Values of SWR and T is:",SWR,",",T) #printing the values of SWR and
T
OUTPUT :-
CODE:-
num = 25
OUTPUT:-
CODE:-
num1 = '00001'
CSPIT(EC)
KARAN PAREKH 18EC054
num2 = '10001'
OUTPUT:-
CODE:-
x = 158
y = 108
temp = x
x=y
y = temp
OUTPUT:-
CODE:-
def add(x, y):
return x + y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
CSPIT(EC)
KARAN PAREKH 18EC054
print("4.Divide")
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
OUTPUT:-
CODE:-
import math #to import math
CSPIT(EC)
KARAN PAREKH 18EC054
cal=""
while cal!="close": #for countineous use of cal
suff=""
pre=""
base=2.72
op=""
suff = input("suffix:") #fetching suffix
if suff=="sin" or suff=="cos" or suff=="tan":
pre=input("enter -1 for inverse ortherwise skip:")
if suff == "log":
base=int(input("Base:"))
num1 = float(input("Enter first number:")) #taking num1 and num2
if suff == "":
op = input("Enter the operation :") #taking operation for num
if op == '^' or op == '+' or op == '-' or op == '*' or op == '/':
num2 = float(input("Enter second number:"))
if (suff=="sin" or suff=="cos" or suff=="tan") and pre=="":
num1=num1*math.pi/180 #converting degree to radian
if op == '+': #'if' and 'elif' statement for different operation
out = num1 + num2 #calculating the operation selected
print("=",out) #printing result by calculation
elif op == '-':
out = num1 - num2
print("=",out)
elif op == '*':
out = num1 * num2
print("=",out)
elif op == '/':
out = num1 / num2
print("=",out)
elif op == '^':
out = num1 ** num2
print("=",out)
elif suff=="sin": #calculation for sin,cos,tan and invers
if pre=="-1":
out = math.asin(num1)
print("=",out*180/math.pi)
else:
out = math.sin(num1)
print("=",out)
elif suff=="cos":
if pre=="-1":
out = math.acos(num1)
CSPIT(EC)
KARAN PAREKH 18EC054
print("=",out*180/math.pi)
else:
out = math.cos(num1)
print("=",out)
elif suff=="tan":
if pre=="-1":
out = math.atan(num1)
print("=",out*180/math.pi)
else:
out = math.tan(num1)
print("=",out)
elif suff=="log" : #calculation for log
if base==2.72:
out = math.log(num1)
else:
out = math.log(num1,base)
print("=",out)
else:
print("syntax error") #for for syntax error
cal=input("Write close to close the calculator :") #to close the loop
OUTPUT:-
Flow Control-
CODE:-
num1=int(input("enter the first number:"))
num2=int(input("enter the second number:"))
if num1>=num2:
large=num1
else:
large=num2
CSPIT(EC)
KARAN PAREKH 18EC054
OUTPUT:-
CODE:-
num = 47
if (num % 2) == 0:
print("num is even",(num))
else:
print("num is odd",(num))
OUTPUT:-
CODE:-
num= 66
if num > 0:
print("positive number")
else:
print("negative number")
OUTPUT:-
CODE:-
a=100
b=120
if b>a:
print(" A")
else:
print(" B")
CSPIT(EC)
KARAN PAREKH 18EC054
OUTPUT:-
CODE:-
CODE:-
a=100
b=100
if a==b:
print(" YES")
else:
print(" NO")
OUTPUT:-
7.Print "1" if a is equal to b, print "2" if a is greater than b, otherwise print "3".
CODE:-
a = input("Enter value of 'a':") #fetching the value of a and b
b = input("Enter value of 'b':")
if a==b: #comparision
print("1") #printing given statement
elif a>b:
print("2")
else:
print("3")
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
a = input("Enter value of 'a':") #fetching the value of a and b
b = input("Enter value of 'b':")
c = input("Enter value of 'c':") #fetching the value of c and d
d = input("Enter value of 'd':")
if a==b and c==d: #comparision
print("Hello") #printing given statement
OUTPUT:-
CODE:-
a = input("Enter value of 'a':") #fetching the value of a and b
b = input("Enter value of 'b':")
c = input("Enter value of 'c':") #fetching the value of c and d
d = input("Enter value of 'd':")
if a==b or c==d: #comparision
print("Hello") #printing given statement
OUTPUT:-
10. Prepare grade sheet. For each of following subjects the marks should be
given by user and grades should be calculated according to the given table and
grade should be displayed for each subject. The list of subject is
1. RF & Microwave
2. Python
3. Antenna
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
M = [0,0,0,0]
for j in range(0,4):
for i in range(0,4):
CSPIT(EC)
KARAN PAREKH 18EC054
else:
print(subjects[i]," grade :FF")
OUTPUT:-
11.WAP to prompt the user for hours and rate per hour to compute gross pay.
Company will give the employee 1.5 times the Hourly rate for hours worked
above 40 hours.
CODE:-
hours = float(input("Enter the no of working hours :")) #fetching hours and rate
else:
print("Minimum 40 hours is required")
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
result+=i
print("Result is:",result) #printing result
OUTPUT:-
CODE:-
i_list=[1,"TWO",3,"FOUR",5,"SIX",7,"EIGHT",9]
for i in i_list:
print(i)
OUTPUT:-
CODE:-
CSPIT(EC)
KARAN PAREKH 18EC054
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
for number in range(1, 100):
if(number % 2 != 0):
print("{0}".format(number))
OUTPUT:-
CODE:-
for number in range(1, 100):
if(number % 2 == 0):
print("{0}".format(number))
OUPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
for i in range(1,21): #for loop for printing
print("Result is:",i*5) #printing result
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
print("Second Number Pattern ")
lastNumber = 6
for row in range(1, lastNumber):
for column in range(1, row + 1):
print(column, end=' ')
print("")
OUTPUT:-
Second Number Pattern
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
for num in range(6):
for i in range(num):
print (num, end=" ")
print("\n")
OUTPUT:-
CODE:-
rows = input("Enter number of rows ")
rows = int (rows)
CSPIT(EC)
KARAN PAREKH 18EC054
print("\r")
OUTPUT:-
CODE:-
rows = input("Enter max star to be display on single line")
rows = int (rows)
for i in range (0, rows):
for j in range(0, i + 1):
print("*", end=' ')
print("\r")
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
i = 10
while i>6: #while loop use
print(i) #printing value of i
i-=1
OUTPUT:-
23. Stop the loop if i is 3. (start with i=1) (use while loop)
CODE:-
i=1
while i!=3: #while loop use
print(i) #printing value of i
i+=1
OUTPUT:-
CODE:-
num = int(input("Enter a number: "))
sum = 0
CSPIT(EC)
KARAN PAREKH 18EC054
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
OUTPUT:-
25.Print the word ‘hello’ indefinitely because the condition will always be true.
CODE:-
a=0
while a>=0: #while loop for indefinite loop
print("Hello") #printing hello
a+=1
OUTPUT:-
CSPIT(EC)
KARAN PAREKH 18EC054
CODE:-
def Pattern(line):
pat=""
for i in range(0,line):
for j in range(0,line):
if ((j == 1 and i != 0 and i != line-1) or ((i == 0 or
i == line-1) and j > 1 and j < line-2) or (i == ((line-1)/2)
and j > line-5 and j < line-1) or (j == line-2 and
i != 0 and i != line-1 and i >=((line-1)/2))):
CSPIT(EC)
KARAN PAREKH 18EC054
pat=pat+"*"
else:
pat=pat+" "
pat=pat+"\n"
return pat
line = 7
print(Pattern(line))
OUTPUT:-
CODE:-
n=7
OUTPUT:-
CODE:-
CSPIT(EC)
KARAN PAREKH 18EC054
sum+=x
x-=1
print("Sum of 1 to 100 is:",sum) #to print the summation
OUTPUT:-
CSPIT(EC)