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

Python Programming Basics and Examples

The document contains a series of Python programming exercises that cover various topics such as reading user input, performing arithmetic operations, calculating simple interest, and determining if a number is even or odd. Each exercise includes a brief description, the corresponding Python code, and sample outputs. The exercises are aimed at helping learners practice fundamental programming concepts and syntax in Python.

Uploaded by

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

Python Programming Basics and Examples

The document contains a series of Python programming exercises that cover various topics such as reading user input, performing arithmetic operations, calculating simple interest, and determining if a number is even or odd. Each exercise includes a brief description, the corresponding Python code, and sample outputs. The exercises are aimed at helping learners practice fundamental programming concepts and syntax in Python.

Uploaded by

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

PYTHON PROGRAMMING

[Link] a program to read details like name, class, age of


a student and then print the details firstly in same line and
then in separate lines. Make sure to have two blank lines
in these two different types of prints.

n = input("Enter name of student: ")


c = int(input("Enter class of student: "))
a = int(input("Enter age of student: "))
print("Name:", n, "Class:", c, "Age:", a)
print()
print()
print("Name:", n)
print("Class:", c)
print("Age:", a)

OUTPUT
Enter name of student: Kavya
Enter class of student: 11
Enter age of student: 17
Name: Kavya Class: 11 Age: 17

Name: Kavya
Class: 11
Age: 17
2. To find square of number 5

num = 5
square = num ** 2
print(square)

# Output:
# 25

3. To find the sum of two numbers 15 and 20


n=15
m=20
sum= (n+m)
print (sum)

# output
# 35
4. To convert length given in
kilometres into meters.
km = int (input("Enter the distance between two cities
in kilometers: "))
mtr = (km * 1000)
print("Distance between two cities in meters is: ",mtr)

OUTPUT

# Enter the distance between two cities in


kilometers: 10
# Distance between two cities in meters is: 10000
5. To print the table of 5 up to five
terms
# printing the table of 5 up to five terms
number = 5
terms = 5
print (f"Multiplication Table of {number} up
to {terms} terms:")

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


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

OUTPUT
Multiplication Table of 5 up to 5 terms:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
6. To calculate Simple Interest if the principle_amount = 2000
rate_of_interest = 4.5 time = 10
To calculate Simple Interest based on the given values of principle amount, rate of interest, and time,
Given:
Principle Amount (P) = 2000
Rate of Interest (R) = 4.5 (assuming 4.5% per annum)
Time (T) = 10 years
Simple Interest=(Principle Amount×Rate of Interest×Time)/100​

Python program:

principle_amount = 2000
rate_of_interest = 4.5 # rate_of_interest is assumed to be in percentage (4.5%)
time = 10 # time is in years

# Calculating Simple Interest


simple_interest = (principle_amount * rate_of_interest * time) / 100

# displaying the result


print(f"The Simple Interest for Principal Amount {principle amount}, Rate of Interest{rate_of_interest}%
per annum, and Time {time} years is: {simple interest}")

OUTPUT

The Simple Interest for Principal Amount 2000, Rate of Interest 4.5% per annum, and Time 10 years is: 900.0
7. To calculate Area of a triangle with Base and Height
Formula
Area=1/2×Base×Height

Program:
# Function to calculate the area of a triangle
def calculate_triangle_area(base, height):
area = 0.5 * base * height
return area

# Example usage:
base = 10
height = 5

# Calculate the area


area_of_triangle = calculate_triangle_area (base, height)

# Print the result print (f"The area of the triangle with base {base} and height
{height} is: {area_of_triangle}")

OUTPUT

The area of the triangle with base 10 and height 5 is: 25.0
8. To calculating average
marks of 3 subjects

//To read the marks of three


Subjects and Display the Total, Avg
and Class
a = int(input("Enter the marks of
first subject: "))
b = int(input("Enter the marks of
second subject: "))
c = int(input("Enter the marks of
third subject: "))
total = a+b+c
avg = total/3
print("Total marks: ",total)
9. To calculate discounted amount
with discount %
Formula:
Discount = bill - (bill * discount / 100)

Program:
bill = int(input("Enter bill amount:"))
discount = int(input("Enter discount
percentage:"))
output = bill - (bill * discount / 100)
print("After discount your bill is: ", output)

Output:
Enter bill amount: 2500
Enter discount percentage: 15
After discount your bill is: 2125.0
[Link] Example Program to
Check if a Number is Even or Odd
( User Input )
number=int (input("enter an
integer no"))
remainder=number % 2
if (remainder==0):
print("number is
even",number)
else:
print("number is odd",
number)
11. Python Program to Swap Two
Numbers without using third variable

x=12
y=13
x,y=y,x
print(" the value of x is",
x)
print(" the value of y is
",y)
12. Python Program to Swap Two
Numbers with using third variable
x=13
y=12
temp=x
print(temp)
x=y
print(" the value of x:",x)
y=temp
print("the value of y:",y)
13. Is a Number Positive Negative or
Zero? Python Programs

num=14

if num > 0:
print("the number is positive")
elif num == 0:
print(" the number is zero")
else:
print("the number is negative")

Output:
the number is positive

You might also like