python sample programs
python sample programs
PROCEDURE:
SOURCE CODE:
a=(input("Enter a"))
b=(input("Enter b"))
c=(input("Enter c"))
if a>b and a>c:
print("a is largest")
if b>a and b>c:
print("b is Largest")
if c>a and c>b:
print("c is Largest")
5
OUTPUT:
6
PROCEDURE:
SOURCE CODE:
def simple_interest(p,r,t):
return(p*r*t)/100
p=float(input("Enter the principal amount?"))
r=float(input("Enter the rate of interest?"))
t=float(input("Enter the time in year?"))
print("Simple interest",simple_interest(p,r,t))
7
OUTPUT:
8
3. Students Marksheet
PROCEDURE:
SOURCE CODE:
marks=int(input("Enter the marks"))
if marks>85 and marks<=100:
print("Congrats ! you scored grade A...")
elif marks>60 and marks<=85:
print("You scored grade B++...")
elif marks>40 and marks<=60:
print("you scored grade B...")
elif marks>30 and marks<=40:
print("you scored grade c...")
else:
print("Sorry you are fail")
9
Output:
10
4. Leap Year
PROCEDURE:
SOURCE CODE:
year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
11
Output:
12
PROCEDURE:
SOURCE CODE:
def my_func():
x = 10
print("Value inside function:",x)
x = 20
my_func()
print("Value outside function:",x)
13
Output:
14
AIM: To Write python program find the Factorial Value to the given number.
PROCEDURE:
SOURCE CODE:
def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))
num = 3
print("The factorial of", num, "is", factorial(num))
15
Output:
16
PROCEDURE:
SOURCE CODE:
num_1 = int(input('Enter your first number: '))
num_2 = int(input('Enter your second number: '))
print('{} + {} = '.format(num_1, num_2))
print(num_1 + num_2)
print('{} - {} = '.format(num_1, num_2))
print(num_1 - num_2)
print('{} * {} = '.format(num_1, num_2))
print(num_1 * num_2)
print('{} / {} = '.format(num_1, num_2))
print(num_1 / num_2)
17
Output:
18
AIM: To Write python program to get the difference between the two lists.
PROCEDURE:
SOURCE CODE:
list1 = [1, 3, 5, 7, 9]
list2=[1, 2, 4, 6, 7, 8]
diff_list1_list2 = list(set(list1) - set(list2))
diff_list2_list1 = list(set(list2) - set(list1))
total_diff = diff_list1_list2 + diff_list2_list1
print(total_diff)
19
Output:
20
9. Implement Inheritance.
PROCEDURE:
SOURCE CODE:
class Family:
def show_family(self):
print("This is our family:")
class Father(Family):
fathername = ""
def show_father(self):
print(self.fathername)
class Mother(Family):
mothername = ""
def show_mother(self):
print(self.mothername)
class Son(Father, Mother):
def show_parent(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
21
s1 = Son()
s1.fathername = "Mark"
s1.mothername = "Sonia"
s1.show_family()
s1.show_parent()
22
Output:
23
PROCEDURE:
SOURCE CODE:
class Person:
"This is a person class"
age = 10
def greet(self):
print('Hello')
harry = Person()
print(Person.greet)
print(harry.greet)
harry.greet()
24
Output: