0% found this document useful (0 votes)
16 views6 pages

Codes

The document contains a series of Python code snippets that demonstrate various programming concepts and tasks, including temperature conversion, work completion calculations, number swapping, traffic signal messages, simple calculations, and more. Each section provides a specific functionality, such as calculating the sum of natural numbers, checking for prime numbers, and manipulating strings. The code is designed for educational purposes, showcasing fundamental programming techniques.

Uploaded by

priyanibose576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Codes

The document contains a series of Python code snippets that demonstrate various programming concepts and tasks, including temperature conversion, work completion calculations, number swapping, traffic signal messages, simple calculations, and more. Each section provides a specific functionality, such as calculating the sum of natural numbers, checking for prime numbers, and manipulating strings. The code is designed for educational purposes, showcasing fundamental programming techniques.

Uploaded by

priyanibose576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

# 1.

Convert Celsius to Fahrenheit

celsius = float(input("Enter temperature in Celsius: "))

fahrenheit = (celsius * 9/5) + 32

print(f "Temperature in Fahrenheit: {fahrenheit}")

# 2. Days to complete work by A, B, C together

x = float(input("Days taken by A: "))

y = float(input("Days taken by B: "))

z = float(input("Days taken by C: "))

days = (x * y * z) / (x * y + y * z + x * z)

print(f"Work will be completed in {days} days")

# 3. Swap two numbers without third variable

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

b = int(input("Enter second number: "))

a, b = b, a

print(f"After swapping: a = {a}, b = {b}")

# 4. Traffic signal message

signal = input("Enter signal color (red/yellow/green): ").lower()

if signal == "red":

print("Stop!")

elif signal == "yellow":

print("Ready to go!")

elif signal == "green":

print("Go!")

else:

print("Invalid color")
# 5. Simple calculator

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

num2 = float(input("Enter second number: "))

op = input("Enter operation (+, -, *, /): ")

if op == '+':

print("Result:", num1 + num2)

elif op == '-':

print("Result:", num1 - num2)

elif op == '*':

print("Result:", num1 * num2)

elif op == '/':

if num2 != 0:

print("Result:", num1 / num2)

else:

print("Division by zero not allowed")

else:

print("Invalid operator")

# 6. Sum of first 10 natural numbers

s=0

for i in range(1, 11):

s+= i

print("Sum =", s)

# 7. Factors of a number using while loop

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

i=1

while i <= n:

if n % i == 0:

print(i, end=" ")

i += 1
print()

#8. Grade of a student based on percentage

percentage = float(input("Enter percentage: "))

if percentage > 90:

grade = "A"

elif percentage > 80:

grade = "B"

elif percentage > 70:

grade = "C"

elif percentage > 60:

grade = "D"

else:

grade = "E"

print(f"Grade: {grade}")

# 9. Pattern 1: Stars

rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):

print("*" * i)

# Pattern 2: Numbers decreasing

rows = int(input("Enter number of rows: "))

for i in range(rows, 0, -1):

for j in range(1, i + 1):

print(j, end="")

print()

# Pattern 3: Alphabets

rows = int(input("Enter number of rows: "))

for i in range(1, rows + 1):


for j in range(65, 65 + i): # ASCII 65 = 'A'

print(chr(j), end="")

print()

# 10. Prime numbers between 2 and 50

for num in range(2, 51):

prime = True

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

prime = False

break

if prime:

print(num, end=" ")

print()

# 11. Factorial of a number

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

fact = 1

for i in range(1, n + 1):

fact *= i

print("Factorial =", fact)

# 12. Leap year check

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

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

print("Leap year")

else:

print("Not a leap year")

# 13. Sum of series 1 + 1/8 + 1/27 + ... + 1/n^3

n = int(input("Enter value of n: "))


total = 0

for i in range(1, n + 1):

total += 1 / (i ** 3)

print("Sum =", total)

# 14. Prime check

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

if num > 1:

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

print("Not prime")

break

else:

print("Prime")

else:

print("Not prime")

# 15. Count character occurrences

string = input("Enter a string: ")

char = input("Enter character to count: ")

count = string.count(char)

print(f"'{char}' occurs {count} times")

# 16. Sum of digits of a number

num = input("Enter a number: ")

digit_sum = sum(int(d) for d in num)

print("Sum of digits =", digit_sum)

# 17. Replace spaces with hyphens

sentence = input("Enter a sentence: ")

print(sentence.replace(" ", "-"))


# 18. Capitalize first letter of each word

sentence = input("Enter a sentence: ")

print(sentence.title())

# 19. Count vowels, consonants, uppercase, lowercase

string = input("Enter a string: ")

vowels = "aeiouAEIOU"

vowel_count = consonant_count = upper_count = lower_count = 0

for ch in string:

if ch.isalpha():

if ch in vowels:

vowel_count += 1

else:

consonant_count += 1

if ch.isupper():

upper_count += 1

elif ch.islower():

lower_count += 1

print(f"Vowels: {vowel_count}")

print(f"Consonants: {consonant_count}")

print(f"Uppercase: {upper_count}")

print(f"Lowercase: {lower_count}")

# 20. Palindrome check + case conversion

string = input("Enter a string: ")

if string.lower() == string[::-1].lower():

print("Palindrome")

else:

print("Not a palindrome")

print("Case converted:", string.swapcase())

You might also like