0% found this document useful (0 votes)
118 views

Practical File (Nimit)

The document provides Python code examples for various programs including: arithmetic operations on numbers, calculating the area of a triangle, solving a quadratic equation, swapping variable values, converting between units of measurement, checking number properties like even/odd and prime, working with sequences like Fibonacci and factorials, displaying multiplication tables, adding and multiplying matrices, sorting words, and removing punctuation from strings. The code examples each include the Python code, inputs/outputs, and a brief description of the program.

Uploaded by

Nimit Garg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Practical File (Nimit)

The document provides Python code examples for various programs including: arithmetic operations on numbers, calculating the area of a triangle, solving a quadratic equation, swapping variable values, converting between units of measurement, checking number properties like even/odd and prime, working with sequences like Fibonacci and factorials, displaying multiplication tables, adding and multiplying matrices, sorting words, and removing punctuation from strings. The code examples each include the Python code, inputs/outputs, and a brief description of the program.

Uploaded by

Nimit Garg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

PYTHON PROGRAMS

• Python program to do arithmetical operations.


Code:
# Store input numbers:
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# Add two numbers


sum = float(num1) + float(num2)
# Subtract two numbers
min = float(num1) - float(num2)
# Multiply two numbers
mul = float(num1) * float(num2)
#Divide two numbers
div = float(num1) / float(num2)
#Getting the modulus
mod=float(num1)%float(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
# Display the subtraction
print('The subtraction of {0} and {1} is {2}'.format(num1, num2, min))
# Display the multiplication
print('The multiplication of {0} and {1} is {2}'.format(num1, num2, mul))
# Display the division
print('The division of {0} and {1} is {2}'.format(num1, num2, div))
# Display the modulus
print('The modulus of {0} and {1} is {2}'.format(num1, num2, mod))
Output:

• Python program to find the area of a triangle.


Code:
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))

# calculate the semi-perimeter


s = (a + b + c) / 2

# calculate the area


area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
Output:
. Python program to solve quadratic equation.
Code:
def equationroots( a, b, c):

# calculating discriminant using formula


dis = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis))

# checking condition for discriminant


if dis > 0:
print(" real and different roots ")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))

elif dis == 0:
print(" real and same roots")
print(-b / (2 * a))

# when discriminant is less than 0


else:
print("Complex Roots")
print(- b / (2 * a), " + i", sqrt_val)
print(- b / (2 * a), " - i", sqrt_val)
# Driver Program
a =float(input("enter the cofficient of x^2:"))
b =float(input("enter the cofficient of x:"))
c =float(input("enter the constant term:"))

# If a is 0, then incorrect equation


if a == 0:
print("Input correct quadratic equation")

else:
equationroots(a, b, c)
Output:

• Python program to swap two variables.


Code:
a = input("Enter value of a: ")
b = input("Enter value of b: ")
# Swap the values
a, b = b, a
# Display the swapped values
print ("The value of a after swapping: ",a)
print ("The value of b after swapping: ",b)
Output:

• Python program to convert kilometers to miles.


Code:
# Taking kilometers input from the user
kilometers = float(input("Enter value in kilometers: "))

# conversion factor
conv_fac = 0.621371

# calculate miles
miles = kilometers * conv_fac
print('%0.2f kilometers is equal to %0.2f miles' %(kilometers,miles))
Output:

• Python program to convert Celsius to Fahrenheit.


Code:
celsius = float(input("Enter value in celcius: "))

# calculate Fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit'
%(celsius,fahrenheit))
Output:

• Python Program to Check if a Number is Odd or Even.


Code:
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Output:

• Python Program to Check Leap Year.


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))
Output:

• Python Program to Check Prime Number.


Code:
num = int(input("Enter a number: "))

# prime numbers are greater than 1


if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")

# if input number is less than


# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
Output:

• Python Program to Print all Prime Numbers in an Interval.


Code:
lower = int(input("enter the lower limit"))
upper = int(input("enter the upper limit"))

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):


# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num,end=",")
Output:
• Python Program to Find the Factorial of a Number.
Code:
num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
Output:

• Python Program to Display the multiplication Table.


Code:
num = int(input("Display multiplication table of? "))
# Iterate 10 times from i = 1 to 10
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
Output:

• Python Program to Print the Fibonacci sequence.


Code:
nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output:

• Python Program to Check Armstrong Number.


Code:
# take input from the user
num = int(input("Enter a number: "))
n=len(str(num))
# initialize sum
sum = 0

# find the sum of the cube of each digit


temp = num
while temp > 0:
digit = temp % 10
sum += digit **n
temp //= 10

# display the result


if num == sum:
print(num,"is an Armstrong number of order",n)
else:
print(num,"is not an Armstrong number of order",n)
Output:

• Python Program to Find Armstrong Number in an Interval.


Code:
lower = int(input("enter the lower limit:"))
upper = int(input("enter the upper limit:"))
print("armstrong number between",lower,"and",upper,"are",end=" : ")
for i in range(lower,upper+1):
n=len(str(i))
# initialize sum
sum = 0

# find the sum of the cube of each digit


temp =i
while temp > 0:
digit = temp % 10
sum += digit **n
temp //= 10

# display the result


if i== sum:
print(i,"of order",n,end=",")
Output:

• Python Program to Find the Sum of Natural Numbers.


Code:
num =int(input("enter how many number's sum you want? "))

if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
Output:

• Python Program to Add Two Matrices.


Code:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[5,8,1],
[6,7,3],
[4,5,9]]

result = [[0,0,0],
[0,0,0],
[0,0,0]]

# iterate through rows


for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Output:

• Python Program to Multiply Two Matrices.


Code:
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]

# iterate through rows of X


for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]

for r in result:
print(r)
Output:

• Python Program to Transpose a Matrix.


Code:
X = [[12,7],
[4 ,5],
[3 ,8]]

result = [[0,0,0],
[0,0,0]]

# iterate through rows


for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[j][i] = X[i][j]

for r in result:
print(r)
Output:
• Python Program to Sort Words in Alphabetic Order.
Code:
my_str = input("Enter a string: ")

# breakdown the string into a list of words


words = [word.lower() for word in my_str.split()]

# sort the list


words.sort()

# display the sorted words

print("The sorted words are:")


for word in words:
print(word)
Output:

• Python Program to Remove Punctuation From a String.


Code:
st=input("enter a string:")
a=""
d=[0,1,2,3,4,5,6,7,8,9]
for c in st:
if (ord(c)>=65 and ord(c)<=90) or (ord(c)>=97 and ord(c)<=122) or (c in d) or
c==" ":
a=a+c
print("the string after removal of punctuation:",a)
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy