0% found this document useful (0 votes)
14 views7 pages

6-2 Homework

The document contains a series of Python programming exercises covering various topics such as string manipulation, recursion, data structures, and mathematical computations. Each exercise includes a brief description, sample input, and output demonstrating the functionality of the code. Topics include reversing strings, checking for palindromes, calculating factorials, finding prime numbers, and more.

Uploaded by

vocosen806
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)
14 views7 pages

6-2 Homework

The document contains a series of Python programming exercises covering various topics such as string manipulation, recursion, data structures, and mathematical computations. Each exercise includes a brief description, sample input, and output demonstrating the functionality of the code. Topics include reversing strings, checking for palindromes, calculating factorials, finding prime numbers, and more.

Uploaded by

vocosen806
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
You are on page 1/ 7

#1) Write a Python program to Reverse a String.

a=(input("Enter a string"))
print("Revese string is",a[::-1])

Enter a string Hello

Revese string is olleH

#2)Write a Python program to Check Palindrome.

a=(input("Enter a text"))
if a == a[::-1]:
print("It is Palindrome")
else:
print("it is not Palindome")

Enter a text level

It is Palindrome

#3)Write a Python program to Count Vowels in a String.

v= "aeiouAEIOU"
a=input("Enter a text")
count=0
if i in v:
count+=1
print(b)

Enter a text hello

#4)Write a Python program to find Factorial with Recursion.

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


f = 1
for i in range(1, num + 1):
f *= i
print("Factorial ",f)

Enter a number: 5

Factorial 120

#5)Write a Python program to find Fibonacci Sequence.

n = int(input("Enter the number of terms: "))


a, b = 0, 1
print("Fibonacci Sequence ", a, b, end=" ")

for i in range(n - 2):


a, b = b, a + b
print(b, end=" ")

Enter the number of terms: 5

Fibonacci Sequence 0 1 1 2 3

#6)Write a Python program to find the Maximum Element in a List.

List=[23, 87, 12, 45, 67, 89, 34, 56, 78, 90]
print("Max element",max(List))

Max element 90

#8)Write a Python program to find Prime Numbers.

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

if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(num, "is not a prime number.")
break
else:
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")

Enter a number: 5

5 is a prime number.

#9)Write a Python program to check for Pangram.


sentence = input("Enter a sentence: ")
alphabet = "abcdefghijklmnopqrstuvwxyz"

if all(letter in sentence for letter in alphabet):


print("Pangram")
else:
print("Not a Pangram")

Enter a sentence: hello

Not a Pangram
#10)Write a Python program to basic Data Structure Operations (e.g.,
list manipulation, string manipulation)?

# List Manipulation
list = [10, 20, 30, 40, 50]

list.append(60)
list.insert(2, 25)
list.remove(40)
list.pop()
list.sort()
list.reverse()

print("Modified List:", list)

# String Manipulation
string = "Hello, Python!"

print("Uppercase:", string.upper())
print("Lowercase:", string.lower())
print("Reversed:", string[::-1])
print("Word Split:", string.split())
print("Replace:", string.replace("Python", "World"))

# Tuple Manipulation
tuple = (1, 2, 3, 4, 5)
print("Tuple Element ", tuple[2])

# Dictionary Manipulation
dict = {"name": "Alice", "age": 25, "city": "New York"}

dict["age"] = 26
dict["country"] = "USA"
del dict["city"]

print("Modified Dictionary:", dict)

Modified List: [50, 30, 25, 20, 10]


Uppercase: HELLO, PYTHON!
Lowercase: hello, python!
Reversed: !nohtyP ,olleH
Word Split: ['Hello,', 'Python!']
Replace: Hello, World!
Tuple Element 3
Modified Dictionary: {'name': 'Alice', 'age': 26, 'country': 'USA'}

#11) Write a Python program to find the Minimum Element in a List.


List=[23, 87, 12, 45, 67, 89, 34, 56, 78, 90]
print("Min element",min(List))

Min element 12

#12)Write a Python program to calculate the Sum of Digits in a Number.

num = int(input("Enter a number"))


sum1 = 0
for digit in str(num):
sum1 += int(digit)
print("Sum of digits", sum1)

Enter a number 55

Sum of digits 10

# 13)Write a Python program to check for Armstrong Number.

num = int(input("Enter a number "))


order = len(str(num))
sum1 = sum(int(digit) ** order for digit in str(num))
if num == sum1:
print(num, "is Armstrong number")
else:
print(num, "is not Armstrong number")

Enter a number 5

5 is Armstrong number

# 14)Write a Python program to check for Leap Year.

year = int(input("Enter a year: "))


if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print(year, "is a leap year")
else:
print(year, "is not a leap year")

Enter a year: 2003

2003 is not a leap year

# 15)Write a Python program to calculate Factorial without Recursion.

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


factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial:", factorial)

Enter a number: 5

Factorial: 120

# 17)Write a Python program to Merge Two Sorted Lists.

list1 = [1, 3, 5, 7]
list2 = [2,2, 6, 8]
merged_list = sorted(list1 + list2)
print("Merged List", merged_list)

Merged List [1, 2, 2, 3, 5, 6, 7, 8]

#18)Write a Python program to Remove Duplicates from a String.


string = "banana"
result = "".join(dict.fromkeys(string))
print(result)

ban

#19)Write a Python program to Check for Perfect Numbers.

num = int(input("Enter a number"))

sum1 = 0
for i in range(1, num):
if num % i == 0:
sum1 += i

if sum1 == num:
print(num, "is a perfect number")
else:
print(num, "is not a perfect number")

Enter a number 5

5 is not a perfect number

#20)Write a Python program to Find the Maximum Difference between Two


Elements in a List.
numbers = [1, 2, 90, 10, 110]
max_diff = max(numbers) - min(numbers)
print("Maximum difference:", max_diff)

Maximum difference: 109


#21)Write a Python program to check if a Number is Even or Odd.

num = int(input("Enter a number"))

if num % 2 == 0:
print(num, "is even")
else:
print(num, "is odd")

#22)Write a Python program to Count Words in a Sentence.

s = input("Enter a sentence: ")


count = 0
for i in s:
count += 1
print(count)

Enter a sentence: hello

#23)Write a Python program to Convert Decimal to Binary.


num = int(input())
binary = ""
while num > 0:
binary = str(num % 2) + binary
num //= 2
print(binary)

101

#24)Write a Python program to Find Second Largest Element in a List.

numbers = [10, 20, 4, 45, 99]


numbers.sort()
print(numbers[-2])

45

#25)Write a Python program to Reverse Words in a String.


s = input("Enter a sentence: ")
w = s[::-1]
for word in w:
print(word, end=" ")
Enter a sentence: Hello

o l l e H

#27)Write a Python program to check if a Number is a Power of Two.

num = int(input("Enter a number "))

while num > 1 and num % 2 == 0:


num //= 2
if num == 1:
print("Yes, it is a power of two.")
else:
print("No, it is not a power of two.")

Enter a number 4

Yes, it is a power of two.

#28)Write a Python program to convert Celsius to Fahrenheit.

cel = float(input("Enter temp in Celsius: "))


fa = (cel * 9/5) + 32
print("Temperature in Fahrenheit:", fa)

Enter temp in Celsius: 5.5

Temperature in Fahrenheit: 41.9

#29)Write a Python program to calculate the LCM (Least Common


Multiple) of Two Numbers.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

g = max(num1, num2)
while True:
if g % num1 == 0 and g % num2 == 0:
print("LCM is:", g)
break
g += 1

Enter first number: 5


Enter second number: 6

LCM is: 30

You might also like