100+ Python Programs
1) Write a Program to print ‘Hello World’
print('Hello World!')
All the developers would have started their career with this simple program. We have
used the print( ) function here. Text inside this function enclosed within single-quote(’ ‘)
or double-quotes(” “) gets printed as it is. Python is one of the most loved and the
easiest programming language because of its simple syntax.
Syntax - An approved set of rules or pre-defined protocols that we need to follow while
working in a programming language.
Output of the Program -
2) Write a Python program to Find the area of circle , square and rectangle by
getting input from the user.
We get the input of the shape and store it in the variable shape. Using the if-condition ,
we find the shape and apply the respective formula. Finally, print the output from the
variable area.
shape = input("Enter a shape: ")
if shape=="circle":
radius = float(input("Enter a radius: "))
area = 3.14 * radius ** 2
elif shape == "square" or shape=="rectangle":
length = int(input("Enter the length: "))
area = length * length
if shape == "rectangle":
100+ Python Programs 1
breadth = int(input("Enter the breadth: "))
area = length * breadth
print("area of",shape,"is",area,"sq units")
Output of the Program -
3) Write a Python program which accepts a sequence of comma-separated
numbers in single-line from user and generate a list and tuple with it.
values = input("Enter numbers seperated by commas:").split(",")
print(values)
print(tuple(values))
Get the input of the values separated with comma simply using the input method. In
default, this gets stored as string. The split() method splits a string into a list. You can
100+ Python Programs 2
specify the separator, default separator is any whitespace. Here we have specified the
separator as ','(comma) . Pass this on the tuple( ) to get the final output.
Output of the Program -
4) Write a Python program to get the volume of a cube, sphere and cuboid with by
getting input from the user.
shape = input("Enter a shape: ")
if shape=="sphere":
radius = float(input("Enter a radius: "))
volume = 1.33 * 3.14 * radius ** 3
elif shape == "cube" or shape=="cuboid":
length = int(input("Enter the length: "))
volume = length ** 3
if shape == "cuboid":
breadth = int(input("Enter the breadth: "))
height = int(input("Enter the height: "))
volume = length * height * breadth
print("area of",shape,"is",volume,"sq units")
Output of the program :
100+ Python Programs 3
Get the name of the shape as input and store it in the variable ‘shape’. Based on the
value entered by the user , find the area of the shape respectively based on their
formula using the if-else-if condition.
5) Take a list and print First , Last and Middle value(if list has odd length) of the
list.
list1 = ["python","java","html","css","js"]
i = len(list1)
print("First value is",list1[0])
print("Last value is",list1[i-1])
if i%2 != 0:
print("mid value is:",list1[i//2])
100+ Python Programs 4
Index number of the list starts with 0. First element has the index 0 and last element -
one less than length of the list or -1. Use floor division inside the if condition because list
indices must be integers or slices, not float.
Output of the Program :
6) Write a Python program to find whether a given number is even or odd by
getting input from the user.
num = int(input("Enter a number: "))
if num%2==0:
print(num,"is even")
else:
print(num,"is odd")
The modulo operator(%) is one of the arithmetic operator that gives the remainder
of an integer division as the output.
if A and B are two integers , A%B = remainder of (A/B). A even number , odd number
divided by 2 gives 0 and 1 as remainder.
Output of the Program :
100+ Python Programs 5
7)
100+ Python Programs 6