Unit -3
Important Python Programs
\
#[Link] Program to Check if a Number is Positive, Negative or 0
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
#2. Python program to check if the input number is odd or even.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
#[Link] program to check if year is a leap year or not
year = int(input("Enter a year: "))
if (year % 400 == 0) and (year % 100 == 0):
print("{0} is a leap year".format(year))
elif (year % 4 ==0) and (year % 100 != 0):
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
#[Link] program to find the largest number among the three input numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num3) :
largest = num2
else:
largest = num3
print("The largest number is", largest)
#5. Program to check if a number is prime or not
num = int(input("Enter a number: "))
flag = False
if num > 1:
for i in range(2, num):
if (num % i) == 0:
flag = True
break
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
#[Link] program to display all the prime numbers within an interval
lower = int(input(“enter a number”)
upper = int(input(“enter a number”)
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
#7 Python program to find the factorial of a number provided by the user.
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)
#8. Multiplication table (from 1 to 10) in Python
num = int(input("Display multiplication table of? "))
for i in range(1, 11):
print(num, 'x', i, '=', num*i)
#[Link] to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
n1=-1
n2 = 1
for i in range(1,nterms):
n3 = n1 + n2
print(n3)
n1 = n2
n2 = n3
# [Link] 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")
#11. Sum of natural numbers up to num
num = 16
if num < 0:
print("Enter a positive number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
#12. Take a list of numbers
my_list = [12, 65, 54, 39, 102, 339, 221,]
result = list(filter(lambda x: (x % 13 == 0), my_list))
print("Numbers divisible by 13 are",result)
#13. Python program to convert decimal into other number systems
dec = int(input(“enter a decimal numner”)
print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
#[Link] to find the ASCII value of the given character
c = input(“enter a character”)
print("The ASCII value of '" + c + "' is", ord(c))
# [Link] to find HCF the Using Euclidian algorithm
def compute_hcf(x, y):
while(y):
x, y = y, x % y
return x
hcf = compute_hcf(300, 400)
print("The HCF is", hcf)
#[Link] Program to find the L.C.M. of two input number
def compute_lcm(x, y):
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
return lcm
num1 = 54
num2 = 24
print("The L.C.M. is", compute_lcm(num1, num2))
# [Link] make a simple calculator
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print("Select operation.")
print("[Link]")
print("[Link]")
print("[Link]")
print("[Link]")
while True:
choice = input("Enter choice(1/2/3/4): ")
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))
next_calculation = input("Let's do next calculation? (yes/no): ")
if next_calculation == "no":
break
else:
print("Invalid Input")
# [Link] to display calendar of the given month and year
import calendar
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
print([Link](yy, mm))
#[Link] of a number using recursion
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input(“enter a number”)
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))
#20. Python Program to find GCD of Two Numbers
a = float(input(" Please Enter the First Value a: "))
b = float(input(" Please Enter the Second Value b: "))
i=1
while(i <= a and i <= b):
if(a % i == 0 and b % i == 0):
gcd = i
i=i+1
print("\n GCD of {0} and {1} = {2}".format(a, b, gcd))
#21. Python Program to find reverse of a given number
num = int(input(“enter a number”)
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed Number: ",reversed_num)
#22. Python Program to find sum of digits of a given number
num = int(input(“enter a number”)
sum = 0
while num != 0:
digit = num % 10
sum = sum+digit
num //= 10
print("Sum of digits: " ,sum)
#23. Python Program to find the given number is palindrome or not
num = int(input(“enter a number”)
num1=num
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed Number: ",reversed_num)
if(num1==reversed_num):
print(“given number is palindrome”)
else:
print(“given number is not a palindrome”)
#24. Python Program to find exponential value
num=int(input("Enter number: "))
exp=int(input("Enter exponential value: "))
result=1
for i in range(1,exp+1):
result=result*num
print("Result is:",result)
#[Link] Program to find Sum of Even and Odd Numbers from 1 to N
maximum = int(input(" Please Enter the Maximum Value : "))
even_total = 0
odd_total = 0
for number in range(1, maximum + 1):
if(number % 2 == 0):
even_total = even_total + number
else:
odd_total = odd_total + number
print("The Sum of Even Numbers from 1 to {0} = {1}".format(number, even_total))
print("The Sum of Odd Numbers from 1 to {0} = {1}".format(number, odd_total))
#26. Python Program to find sum of array elements
arr = [1, 2, 3, 4, 5];
1. sum = 0;
2. for i in range(0, len(arr)):
3. sum = sum + arr[i];
print("Sum of all the elements of an array: ", sum);
#27. Python Program to find Linear Search
def linear_search(alist, key):
if alist[i] == key:
return i
return -1
alist = input('Enter the list of numbers: ')
alist = [Link]()
alist = [int(x) for x in alist]
key = int(input('The number to search for: '))
index = linear_search(alist, key)
if index < 0:
print('{} was not found.'.format(key))
else:
print('{} was found at index {}.'.format(key, index))
#28. Python Program to find Binary Search
1. def binary_search(list1, n):
2. low = 0
3. high = len(list1) - 1
4. mid = 0
while low <= high:
5. mid = (high + low) // 2
6. if list1[mid] < n:
7. low = mid + 1
8. elif list1[mid] > n:
9. high = mid - 1
10. else:
11. return mid
12. return -1
13.
14. list1 = [12, 24, 32, 39, 45, 50, 54]
15. n = 45
16. result = binary_search(list1, n)
17. if result != -1:
18. print("Element is present at index", str(result))
19. else:
20. print("Element is not present in list1")
#29. Python Program to find max and min in list or array
my_list = []
count = int(input("How many numbers you want to add : "))
for i in range(1,count+1):
my_list.append(int(input("Enter number {} : ".format(i))))
print("Input Numbers : ")
print(my_list)
min = my_list[0]
max = my_list[0]
for no in my_list:
if no < min :
min = no
elif no > max :
max = no
print("Minimum number : {}, Maximum number : {}".format(min,max))
#30. Python Program to circulate the values of n variables
import deque
lst=[1,2,3,4,5]
d=deque(lst)
print d
[Link](2)
print d