School Of Advance Computing
Practical file of python
Submitted By Submitted To
Poonam Devi Keshav Sir
20MCA01
1)Program for Arithmetic Operations in Python
Source code:
val1 = 2
val2 = 3
# using the addition operator
res = val1 + val2
print(res)
val1 = 2
val2 = 3
# using the subtraction operator
res = val1 - val2
print(res)
val1 = 2
val2 = 3
# using the multiplication operator
res = val1 * val2
print(res)
val1 = 3
val2 = 2
# using the division operator
res = val1 / val2
print(res)
val1 = 3
val2 = 2
# using the modulus operator
res = val1 % val2
print(res)
val1 = 2
val2 = 3
# using the exponentiation operator
res = val1 ** val2
print(res)
val1 = 3
val2 = 2
# using the floor division
res = val1 // val2
print(res)
Output:
2)Program to Generate a Random Number
Source code:
# Program to generate a random number between 0 and 9
# importing the random module
import random
print(random.randint(0,9))
Output:
3) Program to Convert Celsius To Fahrenheit
Source code:
celsius_1 = float(input("Temperature value in degree Celsius: " ))
# For Converting the temperature to degree Fahrenheit by using the above
# given formula
Fahrenheit_1 = (celsius_1 * 1.8) + 32
# print the result
print('The %.2f degree Celsius is equal to: %.2f Fahrenheit'
%(celsius_1, Fahrenheit_1))
print("----OR----")
celsius_2 = float (input("Temperature value in degree Celsius: " ))
Fahrenheit_2 = (celsius_2 * 9/5) + 32
# print the result
print ('The %.2f degree Celsius is equal to: %.2f Fahrenheit'
%(celsius_2, Fahrenheit_2))
Output:
4) Python Program to Check if a Number is Positive, Negative or zero
Source code:
# Default function to run if else condition
def NumberCheck(a):
# Checking if the number is positive
if a > 0:
print("Number given by you is Positive")
# Checking if the number is negative
elif a < 0:
print("Number given by you is Negative")
# Else the number is zero
else:
print("Number given by you is zero")
# Taking number from user
a = float(input("Enter a number as input value: "))
# Printing result
NumberCheck(a)
Output:
5) Python Program to Check if a Number is Odd or Even
Source code:
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even number".format(num))
else:
print("{0} is Odd number".format(num))
Output:
6)Python Program to Check Leap Year
Source code:
# Default function to implement conditions to check leap year
def CheckLeap(Year):
# Checking if the given year is leap year
if((Year % 400 == 0) or
(Year % 100 != 0) and
(Year % 4 == 0)):
print("Given Year is a leap Year");
# Else it is not a leap year
else:
print ("Given Year is not a leap Year")
# Taking an input year from user
Year = int(input("Enter the number: "))
# Printing result
CheckLeap(Year)
Output:
7)Python Program to Find ASCII Value of given Character
Source code:
# Python program to print
# ASCII Value of Character
# In c we can assign different
# characters of which we want ASCII value
c = 'g'
# print the ASCII value of assigned character in c
print("The ASCII value of '" + c + "' is", ord(c))
Output:
8)Python program to print grades based on marks scored by a student
Source code:
sub1=int(input("Enter marks of the first subject: "))
sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=90):
print("Grade: A")
elif(avg>=80&avg<90):
print("Grade: B")
elif(avg>=70&avg<80):
print("Grade: C")
elif(avg>=60&avg<70):
print("Grade: D")
else:
print("Grade: F")
Output:
9) Python program to print the sum of all numbers in the given range
Source code:
n = input("Enter Number to calculate sum")
n = int (n)
sum = 0.
for num in range(0, n+1, 1):
sum = sum+num.
print("SUM of first ", n, "numbers is: ", sum )
Output:
10) Python program to print all even numbers in given range
Source code:
start, end = 10, 29
# iteration
for num in range(start, end + 1):
# check
if num % 2 == 0:
print(num, end = " ")
Output:
11)Python program to print star pyramid pattern
Source code:
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern triangle
def triangle(n):
# number of spaces
k=n-1
# outer loop to handle number of rows
for i in range(0, n):
# inner loop to handle number spaces
# values changing acc. to requirement
for j in range(0, k):
print(end=" ")
# decrementing k after each loop
k=k-1
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range(0, i+1):
# printing stars
print("* ", end="")
# ending line after each row
print("\r")
# Driver Code
n=5
triangle(n)
Output:
12)Python program to print number pyramid pattern
Source code:
# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
def pypart(n):
myList = []
for i in range(1,n+1):
myList.append("*"*i)
print("\n".join(myList))
# Driver Code
n=5
pypart(n)
Output:
13)Python program to print Fibonacci numbers to given limit
Source code:
# Function for nth Fibonacci number
def Fibonacci(n):
# Check if input is 0 then it will
# print incorrect input
if n < 0:
print("Incorrect input")
# Check if n is 0
# then it will return 0
elif n == 0:
return 0
# Check if n is 1,2
# it will return 1
elif n == 1 or n == 2:
return 1
else:
return Fibonacci(n-1) + Fibonacci(n-2)
# Driver Program
print(Fibonacci(9))
Output:
14) Python program to print numbers from 1 to n except 5 multiples
Source code:
for a in range(1,10):
if a % 5 != 0:
print(a)
Output:
15)Python program to split and join a string
Source code:
def split_string(string):
# Splitting based on space delimiter
list_string = string.split(' ')
return list_string
def join_string(list_string):
# Joining based on '-' delimiter
string = '-'.join(list_string)
return string
string = 'Welcome to study tonight'
# Splitting a string
list_string = split_string(string)
print("After Splitting: ",list_string)
# Join list of strings into one
res_string = join_string(list_string)
print("After joining: ",res_string)
Output: