0% found this document useful (0 votes)
72 views13 pages

Python Examples

Python programming examples

Uploaded by

gadaderohan19
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
72 views13 pages

Python Examples

Python programming examples

Uploaded by

gadaderohan19
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 13

Python

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

# Store input numbers


num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
# Add two numbers sum = float(num1) + float(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
Output:
Enter first number: 1.5
Enter second number: 6.3
The sum of 1.5 and 6.3 is 7.8
Python program to find the factorial of a number provided
by the user
# change the value for a different result
num = 7
# To take input from the user
#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 check if the input number is odd or
even

# A number is even if division by


2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else: print("{0} is Odd".format(num))

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
.

60 = 0b11100 = 0o74 = 0x3c


Source Code

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

You might also like