0% found this document useful (0 votes)
8 views

Python Programs

The document contains a series of Python programs that demonstrate various programming concepts, including mathematical calculations, input/output operations, and list manipulations. Each program is labeled with a brief description of its functionality, such as calculating square numbers, converting kilometers to meters, and performing arithmetic operations. The document serves as a collection of simple coding exercises suitable for beginners to practice Python programming.

Uploaded by

reshu.arora09
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Programs

The document contains a series of Python programs that demonstrate various programming concepts, including mathematical calculations, input/output operations, and list manipulations. Each program is labeled with a brief description of its functionality, such as calculating square numbers, converting kilometers to meters, and performing arithmetic operations. The document serves as a collection of simple coding exercises suitable for beginners to practice Python programming.

Uploaded by

reshu.arora09
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

Prog 1:- Find the Square number of 7

a=7

sq= a*a

print('square of 7 is ', sq)

______________________________________________

Prog 2:- To convert length given in kilometer into meter

km=float (input('enter the length in kilometers='))

# 1km= 1000m

m=km*1000

print('enter',km,'kilometer is equal to',m,'meter')

___________________________________________________________________

1
___________________________________________________________________

Prog 3:- To print the table of 5 into 5 terms

n=5

for i in range(1,6):

tb= n*i

print('\t',n,'*',i,'=',tb)

___________________________________________________________________

2
___________________________________________________________________

Prog 4:- To calculate the Simple Interest if the

# Principal amount = 2000

# Rate of interest = 4.5

# Time = 10

p = 2000

r = 4.5

t = 10

si= (p*t*r)/100

print('\tPrincipal =',p)

print('\tRate of interest =',r)

print('\ttime =',t)

print('-'*20)

print('\tsimple interest =',si)

___________________________________________________________________

3
___________________________________________________________________

Prog 5:- # To calculate the Area and Parimeter of

# a Rectangle

#Area of a rectangle = Length × Width

#Perimeter of a rectangle = 2(Length + Width)

l= float (input(' enter the rectangle length='))

w= float (input(' enter the rectangle width='))

a=l*w

p=2*(l + w)

print('-'*30)

print(" Area of a ractangle using length", l, "and width", w, " = ", a)

print('-'*30)

print(" Perimeter of a Rectangle using length", l, "and width", w, " = ", p)

4
___________________________________________________________________

Prog 6:- #To calculate the area of triangle with base

# and height

b=float(input(' enter the base of triangle='))

h=float(input(' enter the height of triangle='))

a= (b*h)/2

print(' area of triangle=',a)

__________________________________________
_________________________

Prog 7:- #To calculating average marks of 3 subject

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('-'*40)

print(" Total marks = ",total)

print(" Average marks = ",avg)

5
___________________________________________________________________

Prog 8:- #To calculate surface area and volume of a cuboid

#Total Surface area of cuboid = 2 (length × breadth + breadth × height + length × height)

#Volume of the cuboid = (length × breadth × height)

l=float(input(' enter the lenght of cuboid = '))

b=float(input(' enter the breadth of cuboid = '))

h=float(input(' enter the height of cuboid = '))

sa=2*(l*b+b*h+l*h)

vc= l*b*h

print('-'*40)

print(' Total Surface area of cuboid = ',sa)

print(' Volume of the cuboid = ',vc)

___________________________________________________________________

6
Prog 8:- #Program to make calculator (use all arithmetic operators +,-,*,/,//,%,**) using if-elif-
else statement

# Function to perform the calculation

def calculator(a, b, operator):

if operator == '+':

return a + b

elif operator == '-':

return a - b

elif operator == '*':

return a * b

elif operator == '/':

if b != 0:

return a / b

else:

return "Error: Division by zero is not allowed"

elif operator == '//':

if b != 0:

return a // b

else:

return "Error: Division by zero is not allowed"

elif operator == '%':

if b != 0:

return a % b

else:

return "Error: Division by zero is not allowed"

7
elif operator == '**':

return a ** b

else:

return "Invalid operator"

# Input from the user

a = float(input("Enter the first number: "))

b = float(input("Enter the second number: "))

operator = input("Enter the operator (+, -, *, /, //, %, **): ")

# Perform calculation and display the result

result = calculator(a, b, operator)

print(f"The result is: {result}")

Prog 9:- #Program to print grade according to the marks entered by the user: (use if-elif-else
statement)

A: 90 and above

A+: between 80 and 89

B: between 70 and 79

B+: between 60 and 69

C: between 50 and 59

8
D : below 50

# Program to determine grade based on marks

def determine_grade(marks):

if marks >= 90:

return "A"

elif 80 <= marks <= 89:

return "A+"

elif 70 <= marks <= 79:

return "B"

elif 60 <= marks <= 69:

return "B+"

elif 50 <= marks <= 59:

return "C"

else:

return "D"

# Input from the user

marks = float(input("Enter the marks: "))

# Determine grade and display the result

grade = determine_grade(marks)

print(f"The grade is: {grade}")

9
Prog 10:- Program to display 1 to 50 natural numbers.

# Displaying natural numbers from 1 to 50

for number in range(1, 51): # range(start, end) generates numbers from start to end-1

print(number, end=" ") # Print numbers on the same line separated by a space

Prog 11:- Program to print characters of your name.

def print_characters(name):

for char in name:

print(char)

# Input from the user

name = input("Enter your name: ")

# Call the function to print characters of the name

print_characters(name)

10
Prog 12:- Program to print odd numbers from 1 to 100 in reverse order using for loop.

def print_reverse_odds():

for number in range(99, 0, -2):

print(number, end=" ")

# Call the function to display odd numbers in reverse order

print_reverse_odds()

Prog 13:- Program to print table of a number. (take number from user)

def print_table(number):

for i in range(1, 11):

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

11
# Input from the user

number = int(input("Enter a number to display its table: "))

# Call the function to print the table

print_table(number)

Prog 14:- Program to display sum of even numbers from 1 to 100.

def sum_of_evens():

total = 0

for number in range(2, 101, 2):

total += number

return total

# Call the function and display the result

result = sum_of_evens()

print(f"The sum of even numbers from 1 to 100 is: {result}")

12
Prog 15:- Program to display sum of odd numbers from 1 to 100.

def sum_of_odds():

total = 0

for number in range(1, 101, 2):

total += number

return total

# Call the function and display the result

result = sum_of_odds()

print(f"The sum of odd numbers from 1 to 100 is: {result}")

Prog 16:- Program to perform to perform following list operations using list functions on the given list

a=[23,54,11,78,90,32,10,26]:

i. Insert 15 at 4th position

a = [23, 54, 11, 78, 90, 32, 10, 26]

# Inserting 15 at the 4th position

a.insert(3, 15) # Index starts at 0, so the 4th position is index 3

# Printing the updated list

print("Updated list:", a)

13
Add element 100 at the end of the list

# Given list

a = [23, 54, 11, 78, 90, 32, 10, 26]

# Adding 100 at the end of the list

a.append(100)

# Printing the updated list

print("Updated list:", a)

Add the list [7,21,35] in the list a.

# Given list

a = [23, 54, 11, 78, 90, 32, 10, 26]

# Adding [7, 21, 35] to the list

a.extend([7, 21, 35]) # Use extend to add elements from another list

14
# Printing the updated list

print("Updated list:", a)

Display list in descending order

# Given list

a = [23, 54, 11, 78, 90, 32, 10, 26]

# Sorting the list in descending order

a.sort(reverse=True)

# Printing the list in descending order

print("List in descending order:", a)

Remove the element 90 from the list

# Given list

a = [23, 54, 11, 78, 90, 32, 10, 26]

# Removing the element 90 from the list

a.remove(90)

# Printing the updated list

print("Updated list:", a)

15
Display the length of the list

# Given list

a = [23, 54, 11, 78, 90, 32, 10, 26]

# Finding the length of the list

length = len(a)

# Printing the length of the list

print("Length of the list:", length)

Delete last element of a list.

# Given list

a = [23, 54, 11, 78, 90, 32, 10, 26]

# Deleting the last element

a.pop() # Removes the last element from the list

# Printing the updated list

print("Updated list after deleting the last element:", a)

Print largest element and smallest element of the list

# Given list

a = [23, 54, 11, 78, 90, 32, 10, 26]

16
# Finding the largest and smallest elements

largest = max(a)

smallest = min(a)

# Printing the largest and smallest elements

print("Largest element:", largest)

print("Smallest element:", smallest)

Write the output of :

print(a[3:7])

17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy