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

Python Programs for Beginners

The document contains a series of Python programming exercises aimed at beginners, covering various topics such as input/output, arithmetic operations, control structures, and functions. Each exercise includes a problem statement followed by a sample code snippet demonstrating the solution. The exercises range from simple tasks like calculating sums and checking even/odd numbers to more complex ones like generating squares and converting units.

Uploaded by

saviomartin208
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)
176 views13 pages

Python Programs for Beginners

The document contains a series of Python programming exercises aimed at beginners, covering various topics such as input/output, arithmetic operations, control structures, and functions. Each exercise includes a problem statement followed by a sample code snippet demonstrating the solution. The exercises range from simple tasks like calculating sums and checking even/odd numbers to more complex ones like generating squares and converting units.

Uploaded by

saviomartin208
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

Name: Savio Martin

Class: 11 A
Date: 4/9/2024
1. Input a welcome message and display it

a = input("Please enter your welcome message: ")


print("You entered: " + welcome_message)

2. Write a Python program to accept two integers and print their sum

a = int(input("Enter the first integer: "))


b = int(input("Enter the second integer: "))
r=a+b
print("The sum is:", sum_result)

3. Write a program to accept the radius of a circle from the user and
display its
area and Perimeter. (Area=πr2 and Perimeter=2πr).

import math
# Accepting the radius from the user
r = float(input("Enter the radius of the circle: "))
# Calculating the area and perimeter
a = [Link] * (r ** 2)
p = 2 * [Link] * radius# Displaying the results
print(f"The area of the circle is: {a}")
print(f"The perimeter of the circle is: {perimeter}")

4. Write a Python program to accept length and width of a rectangle


and compute its perimeter and area.

# Accepting length and width from the user


l = float(input("Enter the length of the rectangle: "))
b = float(input("Enter the width of the rectangle: "))
# Calculating the area and perimeter
a=l*b
perimeter = 2 * (length + width)

# Displaying the results


print(f"The area of the rectangle is: {area}")
print(f"The perimeter of the rectangle is: {perimeter}")

5. Write a Python program to find whether a given number is even or


odd?

# Accepting a number from the user


n = int(input("Enter a number: "))
# Checking if the number is even or odd
if n % 2 == 0:
print(f"{number} is an even number.")
else:
print(f"{n} is an odd number.")

6. Write a Python program to find largest among three numbers.

# Accepting three numbers from the user


num1 = float(input("Enter the first number: "))num2 =
float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Finding the largest number
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
# Displaying the largest number
print(f"The largest number is: {largest}")

7. Write a Python program to perform arithmetic calculation. This


program

accepts two operands and an operator then displays the calculated


result.
# Accepting two operands from the user
operand = float(input("Enter the first operand: "))
Operand1 = float(input("Enter the second operand: "))
# Accepting an operator from the user
operator = input("Enter an operator (+, -, *, /): ")
# Performing the calculation based on the operator
if operator == '+':
result = operand + operand1
elif operator == '-':
result = operand - operand1
elif operator == '*':
result = operand * operand1
elif operator == '/':
if operand1 != 0: # Check to avoid division by zero
result = operand / operand1
else:
result = "Error: Division by zero is not allowed."
else:
result = "Error: Invalid operator."# Displaying the result
print(f"The result is: {result}")

8. Write a Python Program to calculate factorial of given number


using for loop.

# Accepting a number from the user


number = int(input("Enter a number: "))
# Initializing the factorial variable
factorial = 1
# Calculating factorial using a for loop
for i in range(1, number + 1):
factorial *= i
# Displaying the result
print(f"The factorial of {number} is: {factorial}")

9. Write a program to accept a number from the user and display


whether it is a
palindrome number or not.

# Accepting a number from the user


number = input("Enter a number: ")
# Reversing the number and comparing with the original
if number == number[::-1]:
print(f"{number} is a palindrome number.")
else:
print(f"{number} is not a palindrome number.")

10. Write a program to display the following pattern :


1
1 21 2 3
1234
12345
# Displaying the pattern
for i in range(1, 6): # Outer loop for the number of lines
for j in range(1, i + 1): # Inner loop to print numbers on each line
print(j, end=" ")
print() # Move to the next line after each row is printed

11. Enter two numbers and swap them without taking the temporary
variable.

# Accepting two numbers from the user


a = float(input("Enter the first number (a): "))
b = float(input("Enter the second number (b): "))
# Swapping the numbers without using a temporary variable
a=a+b
b=a-b
a=a-b
# Displaying the swapped values
print(f"After swapping, the first number (a) is: {a}")
print(f"After swapping, the second number (b) is: {b}")

12. Enter a number and power then compute the power using an
exponential
operator

# Accepting the number and power from the user


n = float(input("Enter the number: "))
p = float(input("Enter the power: "))
# Calculating the result using the exponential operator
result = n** p# Displaying the result
print(f"{n} raised to the power of {p} is: {result}")

13. Enter two numbers and perform the floor division.

# Accepting two numbers from the user


num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Performing floor division
result = num1 // num2
# Displaying the result
print(f"The floor division of {num1} by {num2} is: {result}")

14. Enter two angles of a triangle and find the third angle.

# Accepting two angles from the user


a1 = float(input("Enter the first angle of the triangle: "))
a2 = float(input("Enter the second angle of the triangle: "))
# Calculating the third angle
a3 = 180 - (a1 + a2)
# Displaying the third angle
print(f"The third angle of the triangle is: {a3}")

15. Enter any three sides of a rectangle and find the fourth side.

# Accepting three sides of the rectangle from the user


s1 = float(input("Enter the first side of the rectangle: "))
s2 = float(input("Enter the second side of the rectangle: "))
s3 = float(input("Enter the third side of the rectangle: "))# Finding the
fourth side based on the equality of opposite sides
if s1 == s2:
s4 = s3
elif s1 == s3:
s4 = s2
else:
s4 = s1
# Displaying the fourth side
print(f"The fourth side of the rectangle is: {side4}")

16. Accept a number, then do the following using shorthand operator:

a. Increment the number by 5


b. Decrement the number by 2
c. Multiply the number with 3
d. Divide the number by 4
# Accepting a number from the user
number = float(input("Enter a number: "))
# a. Increment the number by 5
number += 5
print(f"After incrementing by 5: {number}")
# b. Decrement the number by 2
number -= 2
print(f"After decrementing by 2: {number}")
# c. Multiply the number by 3
number *= 3
print(f"After multiplying by 3: {number}")
# d. Divide the number by 4
number /= 4
print(f"After dividing by 4: {number}")

17. Conversion programs:

a. Centimeter to Meter and Kilometerb. Days into Years, Months and


weeks
c. Grams to Kilograms
d. Milliliters to liters
# a. Centimeter to Meter and Kilometer
centimeters = float(input("Enter length in centimeters: "))
meters = centimeters / 100
kilometers = centimeters / 100000
print(f"{centimeters} cm is {meters} meters or {kilometers}
kilometers")
# b. Days into Years, Months, and Weeks
days = int(input("Enter number of days: "))
years = days // 365
remaining_days = days % 365
months = remaining_days // 30
weeks = (remaining_days % 30) // 7
print(f"{days} days is approximately {years} years, {months} months,
and {weeks}
weeks")
# c. Grams to Kilograms
grams = float(input("Enter weight in grams: "))
kilograms = grams / 1000
print(f"{grams} grams is {kilograms} kilograms")
# d. Milliliters to Liters
milliliters = float(input("Enter volume in milliliters: "))
liters = milliliters / 1000
print(f"{milliliters} milliliters is {liters} liters")

18. Write a Python program to find the sum of all even numbers from
1 to 100.

# Initialize the sum variable


sum_of_evens = 0
# Loop through numbers from 1 to 100
for number in range(1, 101):
# Check if the number is even
if number % 2 == 0:
sum_of_evens += number# Display the result
print(f"The sum of all even numbers from 1 to 100 is:
{sum_of_evens}")

19. Write a Python program to find the sum of the digits of a given
number.

# Accepting a number from the user


number = int(input("Enter a number: "))
# Initialize the sum variable
sum_of_digits = 0
# Loop to calculate the sum of digits
while number > 0:
digit = number % 10 # Get the last digit
sum_of_digits += digit # Add the digit to the sum
number = number // 10 # Remove the last digit
# Display the result
print(f"The sum of the digits is: {sum_of_digits}")
20. Write a Python program to generate a list of squares of numbers
from 1 to n.

def generate_squares(n):
# Generate a list of squares from 1 to n
squares = [i**2 for i in range(1, n+1)]
return squares
# Get the value of n from the user
try:
n = int(input("Enter a positive integer n: "))
if n > 0:
print("List of squares from 1 to", n, ":", generate_squares(n))
else:
print("Please enter a positive integer.")
except ValueError:
print("Invalid input. Please enter a valid integer.")

You might also like