0% found this document useful (0 votes)
3 views25 pages

Python 2

The document contains a series of Python programming exercises, including temperature conversion, string reversal, Fibonacci series generation, square root calculation, and more. Each exercise is accompanied by code snippets and sample outputs demonstrating the functionality of the programs. The document serves as a practical guide for beginners to learn basic Python programming concepts and functions.

Uploaded by

lalithstore6303
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)
3 views25 pages

Python 2

The document contains a series of Python programming exercises, including temperature conversion, string reversal, Fibonacci series generation, square root calculation, and more. Each exercise is accompanied by code snippets and sample outputs demonstrating the functionality of the programs. The document serves as a practical guide for beginners to learn basic Python programming concepts and functions.

Uploaded by

lalithstore6303
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

1

Copy of Copy of Copy of Copy of [Link] 1st YEAR 2th [Link]


2

1. write a python Program to convert temperature to and from


Celsius to Fahrenheit .

# Program to convert temperature to and from Celsius and Fahrenheit

# Take user choice


print("Temperature Converter")
print("1. Convert Celsius to Fahrenheit")
print("2. Convert Fahrenheit to Celsius")

choice = int(input("Enter your choice (1 or 2): "))

# Conversion formulas
if choice == 1:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F")

elif choice == 2:
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print(f"{fahrenheit}°F is equal to {celsius}°C")

else: print("Invalid choice! Please enter 1 or 2.")


3

Output :

Temperature Converter
1. Convert Celsius to Fahrenheit
2. Convert Fahrenheit to Celsius
Enter your choice (1 or 2): 1
Enter temperature in Celsius: 37
37.0°C is equal to 98.6°F
4

2. Write a python Program that accepts a word from the user


and reverse it .

# Get input from the user


word = input("Enter a word: ")

# Reverse the word using slicing


Reversed word = word[::-1]

# Display the result


print("The reversed word is:", reversed word)
5

Output :
Enter a word: Python
The reversed word is: nohtyP
6

3. Write a python Program to get the Fibonacci series between


0 and 50.

# Fibonacci series in Python


# Get the number of terms from user
n = int(input("Enter the number of terms: "))

# First two terms


a, b = 0, 1
count = 0
print("Fibonacci Series:")
# Check if the number of terms is valid
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print(a)
else:
while count < n:
print(a, end=" ")
c=a+b
# update values
a=b
b=c
count += 1
7

Output :
Fibonacci series between 0 and 50:
0 1 1 2 3 5 8 13 21 34
8

4. Write a python Program to find the square root of a number .

import math

# Get number from the user


Num = float(input("Enter a number: "))

# Calculate square root


sqrt = [Link](Num)

# Display the result


print(f" The square root of {Num} is {sqrt}")
9

Output :
Enter a number: 25
The square root of 25.0 is 5.0
10

5. Write a python Program that a string and calculate the


number of digits and letters.

# Get input from the user


text = input("Enter a string: ")

# Initialize counters
letters = 0
digits = 0

# Loop through each character in the string


for char in text:
if [Link]():
letters += 1
Elif [Link] ():
digits += 1

# Display the result


print(f" Letters: {letters}")
print(f" Digits: {digits}")
11

Output :
Enter a string: Python123
Letters: 6
Digits: 3
12

6. Write a python Program to check whether an alphabet is a


vowel or consonant .

# Get input from the user


Ch = input("Enter an alphabet: ").lower()

# Check if the input is a single alphabet


if [Link] () and Len(Ch) == 1:
if Ch in ['a', 'e', 'i', 'o', 'u']:
print(f"{Ch} is a vowel.")
else:
print(f"{Ch} is a consonant.")
else:
print("Please enter a single alphabet only.")
13

Output : 1
Enter an alphabet: A
a is a vowel.

Output : 2
Enter an alphabet: b
b is a consonant
14

7. Write a python Program to calculate the sum and average of


n integers .

# Get number of integers from the user


n = int(input("Enter how many numbers: "))

# Initialize sum
total = 0

# Loop to get n numbers


for I in range(n):
Num = int (input(f" Enter number {i+1}: "))
total += Num

# Calculate average
average = total / n

# Display results
print(f"\n Sum of {n} numbers = {total}")
print(f" Average of {n} numbers = {average}")
15

Output :
Enter how many numbers: 5
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Enter number 5: 50

Sum of 5 numbers = 150


Average of 5 numbers = 30.0
16

8. write a python Function to find the maximum of three


numbers .

def max of three(a, b, c):


if a >= b and a >= c:
return a
Elif b >= a and b >= c:
return b
else:
return c

# Get three numbers from the user


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))

# Call the function and display the result


maximum = max of three(num1, num2, num3)
print(f" the maximum of {num1}, {num2}, and {num3} is {maximum}")
17

Output :
Enter first number: 25
Enter second number: 78
Enter third number: 55
The maximum of 25.0, 78.0, and 55.0 is 78.0
18

9. write a python Function that takes a number as a parameter


and check the number is prime or not .

def is prime(Num):
if Num <= 1:
return False
for I in range(2, int(Num**0.5) + 1):
if Num % I == 0:
return False
return True

# Get number from the user


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

# Call the function and display the result


if is prime(n):
print(f"{n} is a prime number.")
else:
print(f"{n} is not a prime number.")
19

Output :1
Enter a number: 7
7 is a prime number.

Output :2
Enter a number: 12
12 is not a prime number
20

10. Write a python function to check whether a number is


perfect or not.

def is perfect(Num):
if Num <= 0:
return False
sum of divisors = 0

# Find divisors and calculate their sum


for I in range(1, Num):
if Num % I == 0:
sum of divisors += I

# Check if sum of divisors equals the number


return sum of divisors == Num

# Get number from the user


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

# Call the function and display the result


if is perfect(n):
print(f"{n} is a perfect number.")
else:
print(f"{n} is not a perfect number.")
21

Output : 1
Enter a number: 6
6 is a perfect number.

Output : 2
Enter a number: 10
10 is not a perfect number.
22

11. Write a python Function that check whether a passed


string is palindrome or not.

def is palindrome(s):
# Remove spaces and convert to lowercase for uniform comparison
s = replace(" ", "").lower()
return s == s[::-1]

# Get input from the user


text = input("Enter a string: ")

# Call the function and display the result


if is palindrome(text):
print(f"'{text}' is a palindrome.")
else:
print(f"'{text}' is not a palindrome.")
23

Output : 1
Enter a string: madam
'madam' is a palindrome.

Output : 2
Enter a string: hello
'hello' is not a palindrome.
24

12. Write a python Program for Sequential Search .

# Get list of numbers from the user


numbers = list(map(int, input("Enter numbers separated by spaces:
").split()))

# Get the element to search


key = int(input("Enter the number to search: "))

# Sequential search process


found = False
for I in range(Len(numbers)):
if numbers[I] == key:
print (element {key} found at position {i+1}.")
found = True
break

if not found:
print (element {key} not found in the list.")
25

Output : 1
Enter numbers separated by spaces: 10 25 30 45 50
Enter the number to search: 30
Element 30 found at position 3.

Output : 2
Enter numbers separated by spaces: 5 15 25 35 45
Enter the number to search: 20
Element 20 not found in the list.

You might also like