PYTHON PROGRAM
1. Program using different types of operators.
# Examples of Arithmetic Operator
a=9
b=4
# Addition of numbers
add = a + b
# Subtraction of numbers
sub = a - b
# Multiplication of number
mul = a * b
# Division(float) of number
div1 = a / b
# Division(floor) of number
div2 = a // b
# Modulo of both number
mod = a % b
# Power
p = a ** b
# print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
OUTPUT:
13
36
2.25
6561
2. Program to Perform the GCD of two numbers.
# Python code to demonstrate naive
# method to compute gcd ( recursion )
def hcf(a, b):
if(b == 0):
return a
else:
return hcf(b, a % b)
a = 60
b = 48
# prints 12
print("The gcd of 60 and 48 is : ", end="")
print(hcf(60, 48))
OUTPUT:
The gcd of 60 and 48 is : 12
3. Program to implement Conditional Statements.
letter = "A"
if letter == "B":
print("letter is B")
else:
if letter == "C":
print("letter is C")
else:
if letter == "A":
print("letter is A")
else:
print("letter isn't A, B and C")
OUTPUT:
letter is A
4. Program to implement PRIME number using looping statement.
Number = int(input(" Please Enter any Number: "))
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break
if (count == 0 and Number != 1):
print(" %d is a Prime Number" %Number)
else:
print(" %d is not a Prime Number" %Number)
OUTPUT:
Please Enter any Number: 365
365 is not a Prime Number
Please Enter any Number: 179
179 is a Prime Number
5. Program to swap two numbers using function.
def swap_numbers(a, b):
temp = a
a=b
b = temp
print("After: num1 = {0} and num2 = {1}".format(a, b))
num1 = float(input(" Please Enter the First Value : "))
num2 = float(input(" Please Enter the Second Value : "))
print("Before: num1 = {0} and num2 = {1}".format(num1, num2))
swap_numbers(num1, num2)
OUTPUT:
Please Enter the First Value : 45
Please Enter the Second Value : 86
Before: num1 = 45.0 and num2 = 86.0
After: num1 = 86.0 and num2 = 45.0
6. Program to find Factorial of a number using recursion.
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
# take input from the user
num = int(input("Enter a number: "))
# check is the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))
OUTPUT:
Enter a number: 5
The factorial of 5 is 120
7. Program to implement list and its operations.
a) Creating List
# Python program to demonstrate
# Creation of List
# Creating a List
List = []
print("Blank List: ")
print(List)
# Creating a List of numbers
List = [10, 20, 14]
print("\nList of numbers: ")
print(List)
# size of list
print(len(List))
# Creating a List of strings and accessing
# using index
List = ["Geeks", "For", "Geeks"]
print("\nList Items: ")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Geeks', 'For'], ['Geeks']]
print("\nMulti-Dimensional List: ")
print(List)
OUTPUT:
Blank List:
[]
List of numbers:
[10, 20, 14]
List Items:
Geeks
Geeks
Multi-Dimensional List:
[['Geeks', 'For'], ['Geeks']]
b) Adding Elements
# Python program to demonstrate
# Addition of elements in a List
# Creating a List
List = []
print("Initial blank List: ")
print(List)
# Addition of Elements
# in the List
[Link](1)
[Link](2)
[Link](4)
print("\nList after Addition of Three elements: ")
print(List)
# Adding elements to the List
# using Iterator
for i in range(1, 4):
[Link](i)
print("\nList after Addition of elements from 1-3: ")
print(List)
# Adding Tuples to the List
[Link]((5, 6))
print("\nList after Addition of a Tuple: ")
print(List)
# Addition of List to a List
List2 = ['For', 'Geeks']
[Link](List2)
print("\nList after Addition of a List: ")
print(List)
OUTPUT:
Initial blank List:
[]
List after Addition of Three elements:
[1, 2, 4]
List after Addition of elements from 1-3:
[1, 2, 4, 1, 2, 3]
List after Addition of a Tuple:
[1, 2, 4, 1, 2, 3, (5, 6)]
List after Addition of a List:
[1, 2, 4, 1, 2, 3, (5, 6), ['For', 'Geeks']]
c) Adding element in a specific position
# Python program to demonstrate
# Addition of elements in a List
# Creating a List
List = [1,2,3,4]
print("Initial List: ")
print(List)
# Addition of Element at
# specific Position
# (using Insert Method)
[Link](3, 12)
[Link](0, 'Geeks')
print("\nList after performing Insert Operation: ")
print(List)
OUTPUT:
Initial List:
[1, 2, 3, 4]
List after performing Insert Operation:
['Geeks', 1, 2, 3, 12, 4]
8. Program to create an object using class.
class Employee:
'Common base class for all employees'
empCount = 0
def __init__(self, name, salary):
[Link] = name
[Link] = salary
[Link] += 1
def displayCount(self):
print("Total Employee %d" % [Link])
def displayEmployee(self):
print ("Name : ", [Link], ", Salary: ", [Link])
"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
[Link]()
[Link]()
print ("Total Employee %d" % [Link])
OUTPUT:
Name : Zara , Salary: 2000
Name : Manni , Salary: 5000
Total Employee 2
9. Program to implement inheritance using class.
class Calculation1:
def Summation(self,a,b):
return a+b;
class Calculation2:
def Multiplication(self,a,b):
return a*b;
class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
return a/b;
d = Derived()
print([Link](10,20))
print([Link](10,20))
print([Link](10,20))
OUTPUT:
30
200
0.5
10. Program using Exception Handling.
# Program to handle multiple errors with one
# except statement
# Python 3
def fun(a):
if a < 4:
# throws ZeroDivisionError for a = 3
b = a/(a-3)
# throws NameError if a >= 4
print("Value of b = ", b)
try:
fun(3)
fun(5)
# note that braces () are necessary here for
# multiple exceptions
except ZeroDivisionError:
print("Zero Division Error Occurred and Handled")
except NameError:
print("NameError Occurred and Handled")
OUTPUT:
Zero Division Error Occurred and Handled