0% found this document useful (0 votes)
5 views10 pages

Python Basics: Input/Output Exercises

Uploaded by

dipanjonghosh69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

Python Basics: Input/Output Exercises

Uploaded by

dipanjonghosh69
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PART 1: Variable, Input/Output (1–10)

Write a program to input your name and print a greeting.

name = input("Enter your name: ")

print("Hello", name)

Write a program to input two numbers and print their sum.

a = int(input("Enter first number: "))

b = int(input("Enter second number: "))

print("Sum =", a + b)
Print the square and cube of a given number.

num = int(input("Enter a number: "))

print("Square =", num ** 2)

print("Cube =", num ** 3)

Take input of radius and find area of circle.

r = float(input("Enter radius: "))

area = 3.1416 * r * r

print("Area =", area)


Take age and print if eligible to vote.

age = int(input("Enter your age: "))


if age >= 18:
print("Eligible to vote")
else:
print("Not eligible") Swap two numbers without a third variable.

a=5
b = 10
a, b = b, a
print(a, b)

Calculate average of three numbers.


a = float(input())
b = float(input())
c = float(input())
avg = (a + b + c) / 3
print("Average =", avg)

Check whether a number is even or odd.

num = int(input())
if num % 2 == 0:
print("Even")
else:
print("Odd")

Print numbers from 1 to 10.


for i in range(1, 11):

print(i)

Print even numbers from 1 to 20.

for i in range(2, 21, 2):

print(i)

Sum of numbers from 1 to 100.


print(sum(range(1, 101)))

Print multiplication table of 5.

for i in range(1, 11):

print(f"5 x {i} = {5*i}")

Factorial of a number.

n=5
fact = 1

for i in range(1, n + 1):

fact *= i

print(fact)

Print all prime numbers between 1 and 50.

for num in range(2, 51):

for i in range(2, num):

if num % i == 0:

break

else:

print(num)
Use while loop to print 1–5.

i = 1 while i <= 5:

print(i) i += 1

Break loop when number is 5.

for i in range(10):

if i == 5:

break

print(i)
Continue loop when number is 5.

for i in range(10):

if i == 5:

continue

print(i)

Reverse a number.

num = 1234

rev = 0

while num > 0:


rev = rev * 10 + num % 10

num //= 10

print(rev)

You might also like