Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Python Practical

Uploaded by

vowoha2854
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python Practical

Uploaded by

vowoha2854
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical of Python (506):

1. For loop to print Celsius/Fahrenheit table:

f=0
print(("Cel = Fah"))
for c in range(0,101,1):
f = (c*9/5)+32
print(c, " = ", f)

2. While loop to print the table of sin, cos and tan:

import math

x = float(0)
print("x = sin \t cos \t tan")
while x<=10:
print(x, " = ", round(math.sin(x),4), " | ",round(math.cos(x),4),
" | ",round(math.tan(x),4))
x = round(x+float(0.2),1)
3. Program to read an integer value and print Leap
year or not:

y = int(input("Enter year: "))


if((y%400)==0):
print("Leap year")
elif((y%4)==0 & (y%100)!=0):
print("Leap year")
else:
print("Not a Leap year")

4. Program that takes a positive integer n and then


produces n lines of * pattern:

n = int(input("Enter number: "))

for i in range(0,n+1,1):
for j in range(0,i,1):
print("*",end="")
print()
5. Function that takes integer n as input and calculates
the value of 1 + 1/1! + 1/2! + 1/3! + … + 1/n!

import math

def calculate():
n = int(input("Enter number = "))
series_sum = 0.0
for i in range(1, n+1, 1):
series_sum += 1/math.factorial(i)
return series_sum

result = calculate()
print("Result of series = ", result)

6. Function that takes an integer input and calculates


the factorial of that number:

import math

def fact():
n = int(input("Enter number = "))
fact = 1
for i in range(1, n+1):
fact = fact*i
print("Factorial of ", n, " = ", fact)

fact()
7. Function that takes string input and checks
Palindrome or not:

def check():
str = input("Enter string = ")
length = len(str)
for i in range(length):
if str[i] != str[length-1-i]:
return False
return True

if check():
print("String is palindrome...... ")
else:
print("String is not palindrome...... ")

8. List function to convert a string into a list, as in


list(“abc”) gives [‘a’, ‘b’, ‘c’]:

def list():
str = input("Enter string = ")
list = []
length = len(str)
for i in range(length):
list.append(str[i])
print("List = ", list)

list()
9. Program to generate Fibonacci series:

n = int(input("Enter terms = "))


a=0
b=1
c=0
i=2
print("Fibonacci series of ", n, " terms = 0, 1, ", end="")
while i<n:
c = a+b
a=b
b=c
if i==n-1:
print(c, end="")
else:
print(c, end=", ")
i += 1

10. Program to check whether the input number is


even or odd:

n = int(input("Enter number = "))


if n%2 == 0:
print("Even number")
else:
print("Odd number")
11. Program to compare three numbers and print
the largest one:

a = int(input("Enter 1st number = "))


b = int(input("Enter 2nd number = "))
c = int(input("Enter 3rd number = "))

if (a>b) & (a>c):


print(a, " is largest..")
elif b>c:
print(b, " is largest..")
else:
print(c, " is largest..")

12. Program to print factors of a given number:

n = int(input("Enter number = "))


print("Factors of",n,"are = ",end="")

for i in range(1,n+1):
if n%i == 0:
if i == n:
print(i)
else:
print(i, end=", ")
13. Program to calculate Greatest Common
Difference (GCD) of two numbers:

a = int(input("Enter 1st number = "))


b = int(input("Enter 2nd number = "))

def gcd(a,b):
if b==0:
return a
else:
return gcd(b, a%b)

result = gcd(a,b)
print("Greatest Common Difference between ",a ," ,",b ," =
",result)

You might also like