Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Python Practicals 5CS3

Python lab file document with spiral

Uploaded by

sambhavdwivedi48
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Practicals 5CS3

Python lab file document with spiral

Uploaded by

sambhavdwivedi48
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

S.NO NAME OF PRACTICAL DATE PAGE NO. SIGN.

/REMARKS
1. Write a Program to Find the Area of a Circle 25.09.24

2. Write a Program to show operation on 25.09.24


string.
3. Write a Program to show operation on 26.09.24
List
4. Write a Program to show operation on 26.09.24
Tuples
5. Write a Program to find maximum of 16.10.24
three numbers
To write a Python program to find GCD 16.10.24
6. of two numbers
To write a Python program to find the 13.11.24
7. exponentiation of a number.
To write a Python program to show use 20.11.24
8. Python match-case Statement
To write a Python program to all prime 22.11.24
9. number s
To write a Python program to find 27.11.24
10. factorial of number using user defined
function
Practical:1 Write a Program to Find the Area of a Circle

# Program to find the area of a circle

# Importing the math module to use the value of pi


import math

# Input: radius of the circle


radius = float(input("Enter the radius of the circle: "))

# Formula to calculate area of the circle


area = math.pi * (radius ** 2)

# Output: display the area


print(f"The area of the circle with radius {radius} is: {area:.2f}")

Enter the radius of the circle: 5


The area of the circle with radius 5.0 is: 78.54
Practical:2Write a Program to show operation on string.

str = 'Hello World!'

print (str) # Prints complete string


print (str[0]) # Prints first character of the string
print (str[2:5]) # Prints characters starting from 3rd to 5th
print (str[2:]) # Prints string starting from 3rd character
print (str * 2) # Prints string two times
print (str + "TEST") # Prints concatenated string

OUTPUT−

Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Practical:3 Write a Program to show operation on List

list1 = ['abcd', 786, 2.23, 'john', 70.2]


tinylist1 = [123, 'john']
print(list1) # Prints complete list
print(list1[0]) # Prints first element of the list
print(list1[1:3]) # Prints elements starting from 2nd till 3rd
print(list1[2:]) # Prints elements starting from 3rd element
print(tinylist1 * 2) # Prints list two times
print(list1 + tinylist1) # Prints concatenated lists

Output:

['abcd', 786, 2.23, 'john', 70.2]


abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
Practical:4 Write a Program to show operation on Tuple

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )


tinytuple = (123, 'john')

print (tuple) # Prints the complete tuple


print (tuple[0]) # Prints first element of the tuple
print (tuple[1:3]) # Prints elements of the tuple starting from 2nd till 3rd
print (tuple[2:]) # Prints elements of the tuple starting from 3rd element
print (tinytuple * 2) # Prints the contents of the tuple twice
print (tuple + tinytuple) # Prints concatenated tuples

OUTPUT:
('abcd', 786, 2.23, 'john', 70.2)
abcd
(786, 2.23)
(2.23, 'john', 70.2)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
Practical:5 Write a Program to find maximum of three numbers

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


num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
if num1 >= num2 and num1 >= num3:
largest_number = num1
elif num2 >= num1 and num2 >= num3:
largest_number = num2 else:
largest_number = num3

print("The largest number is:", largest_number)

OUTPUT:

If you input the following numbers:

 num1 = 10
 num2 = 25
 num3 = 7

The output will be:

The largest number is: 25


Practical 6: To write a Python program to find GCD of two numbers
# Program to find GCD of two numbers using math.gcd()

import math

# Input: Two numbers


num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

# Find and display GCD using math.gcd()


gcd_value = math.gcd(num1, num2)
print(f"The GCD of {num1} and {num2} is: {gcd_value}")

OUTPUT:
Enter the first number: 56
Enter the second number: 98
The GCD of 56 and 98 is: 14
Practical 7: To write a Python program to find the exponentiation of a number.

# Program to find exponentiation using '**' operator

# Input: Base and Exponent


base = float(input("Enter the base: "))
exponent = float(input("Enter the exponent: "))

# Calculating exponentiation
result = base ** exponent

# Output: Display the result


print(f"{base} raised to the power of {exponent} is: {result}")

OUTPUT: Enter the base: 2


Enter the exponent: 3
2.0 raised to the power of 3.0 is: 8.0
Practical 8:To write a Python program to show use Python match-case Statement

def weekday(n):
match n:
case 0: return "Monday"
case 1: return "Tuesday"
case 2: return "Wednesday"
case 3: return "Thursday"
case 4: return "Friday"
case 5: return "Saturday"
case 6: return "Sunday"
case _: return "Invalid day number"

print (weekday(3))
print (weekday(6))
print (weekday(7))

OUTPUT:
Thursday
Sunday
Invalid day number
Practical 9:To write a Python program to all prime numbers

# Program to find all prime numbers up to a given limit

# Input: Upper limit


limit = int(input("Enter the upper limit to find prime numbers: "))

# Loop through numbers from 2 to the limit


print(f"Prime numbers up to {limit} are:")
for num in range(2, limit + 1):
# Check if the number is divisible by any number between 2 and num-1
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=" ")

OUTPUT:
Enter the upper limit to find prime numbers: 20
Prime numbers up to 20 are:
2 3 5 7 11 13 17 19
Practical 10: Write a Python program to find factorial of number using user defined function
# Function to calculate factorial of a number
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

# Input: Number for which to calculate the factorial


num = int(input("Enter a number to find its factorial: "))

# Call the factorial function and display the result


result = factorial(num)
print(f"The factorial of {num} is: {result}")

OUTPUT:
Enter a number to find its factorial: 5
The factorial of 5 is: 120

You might also like