Example 1 :Python Program to Print Hello world!
print('Hello, world!')
Output
Hello, world!
Example 2: Add Two Numbers #
This program adds two numbers
num1 = 1.5 num2 = 6.3 sum =
num1 + num2
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum
Output
The sum of 1.5 and 6.3 is 7.8
The program below calculates the sum of two numbers entered by the user.
Example 3: 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
Example 4 :Python Program to Find the Square Root
# Python Program to calculate the square num =
# 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_sqrt))
Output
The square root of 8.000 is 2.828
Example 5 :to Calculate the Area of a Triangle
# Python Program to find the area of triangle
a =5
b=6
c =7
# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area area = (s*(s-a)*(s-b)*(s-c)) **
0.5 print('The area of the triangle is %0.2f' %area)
Output
The area of the triangle is 14.70
Example 6:to Solve Quadratic Equation
# Solve the quadratic equation ax**2 + bx + c = 0
# import complex math module import
cmath
a=1b
=5c=
# calculate the discriminant d =
(b**2) - (4*a*c)
# find two solutions sol1 = (-b-
[Link](d))/(2*a) sol2 = (-
b+[Link](d))/(2*a)
print('The solution are {0} and {1}'.format(sol1,sol2))
Output
Enter a: 1 Enter
b: 5
Enter c: 6
The solutions are (-3+0j) and (-2+0j)
Example 7: to Swap Two Variables
# Python program to swap two variables
x = 5 y = 10
# To take inputs from the user
#x = input('Enter value of x: ')
#y = input('Enter value of y: ')
# create a temporary variable and swap the values
temp = x x = y
y = temp
print('The value of x after swapping: {}'.format(x)) print('The
value of y after swapping: {}'.format(y))
Output
The value of x after swapping: 10
The value of y after swapping: 5
Example 8 : performs simple arithmetic operations
# given two numbers #
given first number
numberone = 29
numbertwo = 13
# adding the given two numbers addnumb =
numberone+numbertwo
# printing the result of sum of two numbers print("Adding the given two numbers",
numberone, "+", numbertwo, "=", addnumb)
# subtracting the given two numbers diffnumb =
numberone-numbertwo
# printing the result of difference of two numbers
print("Subtracting the given two numbers",
numberone, "-", numbertwo, "=", diffnumb) # multiply
the given two numbers mulnumb =
numberone*numbertwo
# printing the result of product of two numbers
print("Multiplying the given two numbers", numberone, "*",
numbertwo, "=", mulnumb)
# diving the given two numbers divnumb =
numberone+numbertwo
# printing the result of sum of two numbers
print("Dividing the given two numbers",
numberone, "/", numbertwo, "=", divnumb)
Output:
Adding the given two numbers 29 + 13 = 42
Subtracting the given two numbers 29 - 13 = 16
Multiplying the given two numbers 29 * 13 = 377
Dividing the given two numbers 29 / 13 = 42
Example 9 : to Generate a Random Number
We’ll write a program that produces a random number between a specific range.
Below is the implementation:
# Program which generates the random number between a specific range.
# importing random module as below import
random
# generating random number randNumber =
[Link](1, 450)
# print the random number which is generated above
print("Generating random numbers between 1 to 450 = ", randNumber)
Output:
Generating random numbers between 1 to 450 = 420
3)Program to Convert Celsius To Fahrenheit
We will convert the given temp in celsius to fahrenheit as below.
Below is the implementation:
# Python program for converting celsius to fahrenheit temperature.
# given temp in celsius celsiusTemp =
42.5
# converting the given temp in celsius to fahrenheit temperature
fahrenheitTemp = (celsiusTemp * 1.8) + 32 # print the converted
temperature value print('Converting the given temp in celsius =',
celsiusTemp, " to Fahrenhiet = ", fahrenheitTemp) Output:
Converting the given temp in celsius = 42.5 to Fahrenhiet = 108.
Example 10 : to Check if a Number is Positive, Negative or zero
# Python Program to Check if a Number is Positive number, Negative number or zero
# Function which prints whether the given number #is
positive number ,negative number or zero def
checkNumber(given_Num):
# checking if the given number is positive number if(given_Num > 0):
print("The given number", given_Num, "is positive") #
checking if the given number is negative number
elif(given_Num < 0):
print("The given number", given_Num, "is negative")
# if the above conditions are not satisfied then the given number is zero else:
print("The given number", given_Num, "is zero")
# given numbers #
given number 1
given_num1 = 169
# passing the given number to checkNumber Function which prints the type of
number(+ve,-ve,0) checkNumber(given_num1) # given number 2 given_num1 = -374
# passing the given number to checkNumber Function which prints the type of
number(+ve,-ve,0) checkNumber(given_num1) # given number 3 given_num1 = 0
# passing the given number to checkNumber Function which prints the type of
number(+ve,-ve,0) checkNumber(given_num1) Output:
The given number 169 is positive
The given number -374 is negative
The given number 0 is zero
Example 11: Python Program to Check if a Number is Odd or Even
# Python Program to Check if a Number is Odd or Even
# Function which prints whether the given number
# is even number ,odd number def
checkNumber(given_Num):
# checking if the given number is even number if(given_Num % 2
== 0):
print("The given number", given_Num, "is even number")
# checking if the given number is odd number that is if given number is not even then it is odd
number else:
print("The given number", given_Num, "is odd number")
# given numbers #
given number 1
given_num1 = 169
# passing the given number to checkNumber Function which # prints
the type of number(even number or odd number)
checkNumber(given_num1)
# given number 2 given_num1 =
26
# passing the given number to checkNumber Function which #
prints the type of number(even number or odd number)
checkNumber(given_num1) Output:
The given number 169 is odd number
The given number 26 is even number
Example12:Python Program to Check Leap Year
# Python program to check if year is a leap year
# given year given_year =
2024 if (given_year % 4)
== 0: if (given_year %
100) == 0: if (given_year
% 400) == 0:
print("Given year", given_year, "is leap year") else:
print("Given year", given_year, "is not leap year") else:
print("Given year", given_year, "is leap year") else:
print("Given year", given_year, "is not leap year") Output:
Given year 2024 is leap year
Example 13 : Python Program to Find ASCII Value of given_Character
# Python Program to Find ASCII Value of given_Character
# given character given_character =
's'
# finding ascii value of given character
asciiValue = ord(given_character) # printing
ascii value of given character
print(" ascii value of given character", given_character, "=", asciiValue)
Output: ascii value of given character s = 115
Example 14 :Python program to print grades based on marks scored by a student
# Python program to print grades based on marks scored by a student
# given marks scored by student
given_marks = 68 markGrade =
given_marks//10 # cheecking if grade
greater than 9 if(markGrade >= 9):
print("grade A+") elif(markGrade < 9 and
markGrade >= 8): print("grade A")
elif(markGrade < 8 and markGrade >= 7):
print("grade B+") elif(markGrade < 7 and
markGrade >= 6):
print("grade B") elif(markGrade < 6 and
markGrade >= 5):
print("grade C+") else:
print("Fail")
Output: grade
Example 15: Python program to print the sum of all numbers in the given range
# Python program to print the sum of all numbers in the given range
# given lower limit range
lower_range = 17 # given
upper limit range
upper_range = 126
# initializing sum of range to 0 rangeSum = 0
# Using for loop to traverse from lower limit range to upper limit range for i in
range(lower_range, upper_range+1): rangeSum = rangeSum+i print("the sum
of all numbers between", lower_range, "to", upper_range, "=", rangeSum)
Output: the sum of all numbers between 17 to 126 = 7865
Example 16 : program to print all even numbers in given range
# Python program to print the even numbers in
# given range(lower limit range and upper limit range)
# given lower limit range lower_range =
17
# given upper limit range upper_range =
126
# printing the even numbers in the giveen range using looop #
Iterate from lowe limit range to upper limit range for numb in
range(lower_range, upper_range+1): # checking if the number is
divisible by 2 if(numb % 2 == 0):
# we will print the number if the number is divisible by 2
print(numb) Output:
18
20
..124
126
Example 17 :Python program to print star pyramid pattern
# Python program to print star pyramid pattern
# given number of rows
given_rows = 15 for i in
range(given_rows+1): for j in
range(i): print("*", end=" ")
print() Output:
**
** *
** * *
** * * *
** * * * *
** * * * * *
** * * * * * *
** * * * * * * *
** * * * * * * * *
** * * * * * * * * *
** * * * * * * * * * *
** * * * * * * * * * * *
** * * * * * * * * * * * *
** * * * * * * * * * * * * *
Example 18 :Python program to print number pyramid pattern
# Python program to print star pyramid pattern
# given number of rows
given_rows = 15 for i in range(1,
given_rows+1): for j in range(1,
i+1): print(j, end=" ") print()
Output:
12
123
1234
12345
123456
1234567
12345678
123456789
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example 19 :Python program to print Fibonacci numbers to given limit
# Python program to print the all the fibonacci numbers in the given range
# given lower limit range lower_range = 2 # given upper limit
range upper_range = 1000 first, second = 0, 1 # using while
loop while (first < upper_range): if(upper_range >=
lower_range and first <= upper_range): print(first)
first, second = second, first+second Output:
13
21
34
55
89
144
233
377
610
987
Example 20 :Python program to print numbers from 1 to n except 5 multiples #
Python program to print numbers from 1 to n except 5 multiples
# given number numb =
49
# using for loop to iterate from 1 to n for
num in range(1, numb+1):
# if number is not divisible by 5 then print it
if(num % 5 != 0): print(num) Output:
..47
48
49