Python Examples
Python Examples
Programming
# To check if a number is prime or not
Program ;
num = 29
# To take input from the user #num = int(input("Enter a
number: "))
# define a flag variable flag = False
# prime numbers are greater than 1 if num > 1:
# check for factors for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True flag = True
# break out of loop break
# check if flag is True
if flag: print(num, "is not a prime number")
else:
print(num, "is a prime number")
Example : Add Two Numbers With User Input
Example : Add Two Numbers With User Input
Output
# Python program to check if the input number is odd or
even
Output 1
Enter a number: 43
Example: Simple Calculator by Using Functions
# This function adds two numbers 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
Output
Select operation.
1.Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4): 3
Enter first number: 15
Enter second number: 14
15.0 * 14.0 = 210.0
# Python program to convert decimal into other number systems
.
dec = 344
print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
Output
The decimal value of 344 is:
0b101011000 in binary.
Python Program to Find Numbers Divisible by Another Number
Source Code
# Take a list of numbers
my_list = [12, 65, 54, 39, 102, 339, 221,]
# use anonymous function to filter
result = list(filter(lambda x: (x % 13 == 0), my_list))
# display the result
print("Numbers divisible by 13 are",result)
Output
Numbers divisible by 13 are [65, 39, 221]
Python Program to Find the Factors of a Number
Source Code
# Python Program to find the factors of a number
# This function computes the factor of the argument passed
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = 320
print_factors(num)
Output
The factors of 320 are: 1 2 4 5 8 10 16 20 32 40 64 80 160 320
# Python Program to calculate the square root
# Note: change this value for a different result
num = 8 #
To take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num
Output
The square root of 8.000 is 2.828
Source Code
celsius * 1.8 = fahrenheit - 32
# Python Program to convert temperature in celsius to Fah
# change this value for a different result
celsius = 37.5
# calculate Fahrenheit
Fahrenheit = (celsius * 1.8) + 32
print('%0.1f degree Celsius is equal to %0.
1f degree Fahrenheit' %(celsius,fahrenheit))
Output
37.5 degree Celsius is equal to 99.5 degree Fahrenheit
# change the values of num1, num2 and num3
# for a different result num1 = 10 num2 = 14 num3
# uncomment following lines to take three numbers
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: ")) #nu
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else: largest = num3
print("The largest number is", largest)
Output