0% found this document useful (0 votes)
4 views2 pages

Python Number and String Operations

The document provides several Python code snippets for basic programming tasks. These include checking if a number is even or odd, calculating the factorial of a number, reversing a string, checking if a number is prime, counting vowels in a string, generating a Fibonacci series, checking for palindromes, and finding the minimum and maximum in a list. Each snippet includes user input and relevant output statements.

Uploaded by

ssrsreenivash
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)
4 views2 pages

Python Number and String Operations

The document provides several Python code snippets for basic programming tasks. These include checking if a number is even or odd, calculating the factorial of a number, reversing a string, checking if a number is prime, counting vowels in a string, generating a Fibonacci series, checking for palindromes, and finding the minimum and maximum in a list. Each snippet includes user input and relevant output statements.

Uploaded by

ssrsreenivash
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

Checking if a Number is Even or Odd:

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

if number % 2 == 0:

print(f"{number} is an even number.")

else:

print(f"{number} is an odd number.")

Calculating Factorial of a Number:


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

fact = 1

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

fact *= i

print("Factorial:", fact)

Reversing a String:
my_string = input("Enter a string: ")
reversed_string = my_string[::-1]
print(f"The reversed string is: {reversed_string}")
Check if a Number is Prime
num = int(input("Enter a number: "))

if num > 1:

for i in range(2, num):

if num % i == 0:

print("Not Prime")

break

else:

print("Prime")

else:

print("Not Prime")

Count Vowels in a String


text = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
print("Number of vowels:", count)
Fibonacci Series
n = int(input("How many terms? "))

a, b = 0, 1

for _ in range(n):

print(a, end=" ")

a, b = b, a + b

Palindrome Check (String)

text = input("Enter a word: ")

if text == text[::-1]:

print("Palindrome")

else:

print("Not a palindrome")

Find Minimum and Maximum in a List

numbers = [int(x) for x in input("Enter numbers separated by space:


").split()]

print("Minimum:", min(numbers))

print("Maximum:", max(numbers))

You might also like