Simple Program Code In Python ( Python For Beginners) Part - 2
1. Check for Palindrome:
def is_palindrome(s):
return s == s[::-1]
string = input("Enter a string: ")
if is_palindrome(string):
print("Palindrome")
else:
print("Not a palindrome")
2. Find the Largest Among Three Numbers:
a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
c = float(input("Enter the third number: "))
max_num = max(a, b, c)
print("Largest number:", max_num)
3. Print Multiplication Table:
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
4. Convert Celsius to Fahrenheit:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print("Temperature in Fahrenheit:", fahrenheit)
5. Simple String Operations:
string = "Hello, World!"
print("Length of the string:", len(string))
print("Uppercase:", string.upper())
print("Lowercase:", string.lower())
print("Reversed string:", string[::-1])
6. Bubble Sort Algorithm:
def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print("Sorted array:", arr)
7. Check Leap Year:
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
return False
year = int(input("Enter a year: "))
if is_leap_year(year):
print("Leap year")
else:
print("Not a leap year")
8. Count Vowels in a String:
def count_vowels(s):
vowels = 'aeiouAEIOU'
count = 0
for char in s:
if char in vowels:
count += 1
return count
string = input("Enter a string: ")
print("Number of vowels:", count_vowels(string))
9. Find the LCM of Two Numbers:
def compute_lcm(x, y):
if x > y:
greater = x
else:
greater = y
while True:
if greater % x == 0 and greater % y == 0:
lcm = greater
break
greater += 1
return lcm
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("LCM:", compute_lcm(num1, num2))
10. Basic Class and Object:
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
length = float(input("Enter length of the rectangle: "))
width = float(input("Enter width of the rectangle: "))
rect = Rectangle(length, width)
print("Area of the rectangle:", rect.area())
0 Comments