Write a program to compute GCD of two numbers.
def gcd(x, y):
if x > y:
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf
num1 = 54
num2 = 24
print("The H.C.F. is", gcd(num1, num2))
Output
The H.C.F. is 6
Write a Program for checking whether the given number is an even number or not.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print(“Even number”)
else:
print(“Odd Number”)
Output
Enter a number: 43
Even number
Write a program to calculate the length of the string without using a library function.
string=raw_input("Enter string:")
count=0
for i in string:
count=count+1
print("Length of the string is:")
print(count)
Output:
Case 1:
Enter string:Hello
Length of the string is:
5
Case 2:
Enter string:Bangalore
Length of the string is:
9
Write a Python Program to find the largest of two numbers.
def maximum(a, b):
if a >= b:
return a
else:
return b
# Driver code
a=2
b=4
print(maximum(a, b))
O/p:
4
Write a Python function to reverse a string using library function.
def rev(s):
s = "".join(reversed(s))
return s
str = "edureka"
print(rev(str))
Output: akerude
Write a Python program to print the even numbers from a given list.
Sample List : [1, 2, 3, 4, 5, 6, 7, 8, 9]
def is_even_num(l):
enum = []
for n in l:
if n % 2 == 0:
enum.append(n)
return enum
print(is_even_num([1, 2, 3, 4, 5, 6, 7, 8, 9]))
Sample Output:
[2, 4, 6, 8]
Write a Python Program to compute arithmetic operations.
Write calculator program in record
Write a Python Program to generate a random numbers from 1 to 40 and append them to the list.
def Rand(start, end, num):
res = []
for j in range(num):
res.append(random.randint(start, end))
return res
# Driver Code
num = 10
start = 20
end = 40
print(Rand(start, end, num))
Output:
[23, 20, 30, 33, 30, 36, 37, 27, 28, 38]
Write a Python Program to find maximum and minimum number in a list.
list1 = [10, 20, 1, 45, 99]
print("Smallest element is:", min(list1))
print("largest element is:", max(list1))
Output:
Smallest element is:1
Largest element is: 99
Write a python program to check if the number is palindrome or not.
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
O/p:
Case 1
Enter number:121
The number is a palindrome!
Case 2
Enter number:567
The number isn't a palindrome!
Write a program to find smallest of two numbers
def minimum(a, b):
if a <= b:
return a
else:
return b
# Driver code
a=2
b=4
print(minimum(a, b))
O/P:
2
Write a python program to swap two numbers with using another variable
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
a=a+b
b=a-b
a=a-b
print("Swapped values of x is %.1f & y is %.1f" %(a,b))
Input:
54.2648
25.1239
Output:
25.12
54.26
Write a python program to find the given number is positive or negative
num = float(input("Enter a number: "))
if num >= 0:
if num == 0:
print("Zero")
else:
print("Positive number")
else:
print("Negative number")
O/p:
Enter a number: 2
Positive number
Write a python program to check whether the given year is leap year or not.
year = int(input("Enter Year: "))
if year % 4 == 0 and year % 100 != 0:
print(year, "is a Leap Year")
elif year % 100 == 0:
print(year, "is not a Leap Year")
elif year % 400 ==0:
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")
O/P:
Enter Year:2000
2000 is a leap year
Write a python program to find the sum of ‘N’ natural number using for loop
num=input("Enter numbet to calculate sum: ")
num=int(num)
sum=0;
for number in range(0,num+1,1):
sum=sum+number
print("Sum of first ",num,"natural numbers is:",sum )
O/P:
Enter number to calculate sum: 10
Sum is: 55
Write a Python program to accept two numbers find the quotient and remainder and print the result.
def find(n, m):
# for quotient
q = n//m
print("Quotient: ", q)
# for remainder
r = n%m
print("Remainder", r)
find(10, 3)
find(99, 5)
O/p:
Quotient: 3
Remainder 1
Quotient: 19
Remainder 4
Write a program for counting word frequency in a file
fname = input("Enter file name: ")
word=input("Enter word to be searched:")
k=0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
if(i==word):
k=k+1
print("Occurrences of the word:")
print(k)
O/P:
Contents of file:
hello world hello
hello
Output:
Enter file name: test.txt
Enter word to be searched:hello
Occurrences of the word:
3
.Python program to check if the number is an Armstrong number or not
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
2. Simple calculator
def add(x, y):
return x + y
# This function subtracts two numbers
def subtract(x, y):
return x - y
# This function multiplies two numbers
def multiply(x, y):
return x * y
# This function divides two numbers
def divide(x, y):
return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
while True:
# Take input from the user
choice = input("Enter choice(1/2/3/4): ")
# Check if choice is one of the four options
if choice in ('1', '2', '3', '4'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
break
else:
print("Invalid Input")
Factorial
num = int(input("Enter a number: "))
factorial = 1
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)
O/P:
Enter a number: 5
The factorial of 5 is 120
4. Random
import random
a=[]
n=int(input("Enter number of elements:"))
for j in range(n):
a.append(random.randint(1,20))
print('Randomised list is: ',a)
O/P:
Enter number of elements:5
Randomised list is: [6, 12, 13, 10, 19]
5. Length of string:
string=str(input("Enter string:"))
count=0
for i in string:
count=count+1
print("Length of the string is:")
print(count)
O/P:
Enter string:hi
Length of the string is:
2
6. Keys into dictionary:
keys = ['red', 'green', 'blue']
values = ['#FF0000','#008000', '#0000FF']
color_dictionary = dict(zip(keys, values))
print(color_dictionary)
O/P: {'red': '#FF0000', 'green': '#008000', 'blue': '#0000FF'}
7. Capitalize
fname = input("Enter file name: ")
with open(fname, 'r') as f:
for line in f:
l=line.title()
print(l)
O/P: Enter file name: message2.txt
I Love Programming And Its Related Features
I Want To Be Master In Python Java C C++
8. Shut down and restart
# Python Program - Shutdown Computer
import os;
check = input("Want to shut down your computer ? (y/n): ");
if check == 'n':
exit();
else:
os.system("shutdown /s /t 1");
# Python Program - Restart Computer
import os;
check = input("Want to restart your computer ? (y/n): ");
if check == 'n':
exit();
else:
os.system("shutdown /r /t 1");
9. Resolution of the image
def jpeg_res(filename):
""""This function prints the resolution of the jpeg image file passed into it"""
# open image for reading in binary mode
with open(filename,'rb') as img_file:
img_file.seek(163)
a = img_file.read(2)
height = (a[0] << 8) + a[1]
a = img_file.read(2)
width = (a[0] << 8) + a[1]
print("The resolution of the image is",width,"x",height)
jpeg_res("sample.jpg")
O/P:
The resolution of the image is 2055 x 1548
Write a python Program to get the 5 subjects marks and display the grade for the same.
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")
O/P:
Enter marks of the first subject: 81
Enter marks of the second subject: 72
Enter marks of the third subject: 94
Enter marks of the fourth subject: 85
Enter marks of the fifth subject: 80
Grade: B
Write a program to find the square of a number
num = 8
answer = num ** 0.5
print('The square root is”,answer)
O/P:
The square root is 2.828
Write a python Program to take temperature in Celsius and convert it to a Fahrenheit
celsius=int(input("Enter the temperature in celcius:"))
f=(celsius*1.8)+32
print("Temperature in farenheit is:",f)
O/P:
Enter the temperature in celcius:32
Temperature in Fahrenheit is: 89.6
Write a python program to calculate area and circumference of a circle.
PI = 3.14
radius = float(input(' Please Enter the radius of a circle: '))
diameter = 2 * radius
circumference = 2 * PI * radius
area = PI * radius * radius
print(" Diameter Of a Circle “,diameter)
print(" Circumference Of a Circle",circumference)
print(" Area Of a Circle”,area)
O/P:
Please Enter the radius of a circle:4
Diameter Of a Circle 8.0
Circumference Of a Circle 25.12
Area Of a Circle 50.24
Write a python program to calculate area and perimeter of a triangle.
a = float(input('Please Enter the First side of a Triangle: '))
b = float(input('Please Enter the Second side of a Triangle: '))
c = float(input('Please Enter the Third side of a Triangle: '))
Perimeter = a + b + c
s = (a + b + c) / 2
Area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print("The Perimeter of Traiangle “,Perimeter);
print(" The Semi Perimeter of Traiangle”,s);
print(" The Area of a Triangle is”, Area)
O/P:
Please Enter the First side of a Triangle: 2
Please Enter the Second side of a Triangle: 3
Please Enter the Third side of a Triangle: 3
The Perimeter of Traiangle 8.0
The Semi Perimeter of Traiangle 4.0
The Area of a Triangle is 2.8284271247461903