1.
Reversing a String
Reversing a string is a common operation in programming In Python you can achieve this using slicing
. , ,
which allows you to extract a portion of a string This snippet takes a string as input and outputs the
.
string in reverse order .
def reverse_string(string):
return string[::-1]
string = "Hello World"
reversed_string = reverse_string(string)
print(f"The reversed string is: {reversed_string}")
Output The reversed string is dlroW olleH
: :
2. Checking if a Number is Even or Odd
.
This Python code snippet checks if a given integer is even or odd The modulo operator (%) determines
the remainder after division. If the remainder after dividing by 2 is 0, the number is even; otherwise, it's
odd.
def is_even(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
number = 15
result = is_even(number)
print(f"{number} is {result}")
Output : 15 is Odd
3. Calculating the Area of a Circle
This snippet uses the formula for calculating the area of a circle (πr²), where r is the radius. The Python
code uses the built-in math module's pi constant and multiplies it by the square of the radius.
import math
def circle_area(radius):
return math.pi * radius ** 2
radius = 5
area = circle_area(radius)
print(f"The area of the circle is: {area:.2f}")
Output: The area of the circle is: 78.54
4. Generating a Random Password
This snippet demonstrates how to create a random password using the random module in Python. It
combines random letters, numbers, and symbols to form a strong password. The strength of the
password depends on the length and the character pool used.
import random
import string
def generate_password(length):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
length = 12
password = generate_password(length)
print(f"Generated password: {password}")
Output: Generated password: #D$P7^L?Tq9
5. Printing the Fibonacci Sequence
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding
numbers. This snippet generates the first n terms of the Fibonacci sequence using a loop.
def fibonacci(n):
a, b = 0, 1
for i in range(n):
yield a
a, b = b, a + b
n = 10
for number in fibonacci(n):
print(number, end=" ")
print()
Output: 0 1 1 2 3 5 8 13 21 34
6. Checking if a Number is Prime
This code snippet checks if a given number is prime. A prime number is a natural number greater than 1
that has no positive divisors other than 1 and itself. The code uses a loop to check for divisibility by
numbers from 2 up to the square root of the given number.
def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
number = 17
if is_prime(number):
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
Output: 17 is a prime number
7. Calculating the Factorial of a Number
The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or
equal to n. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120. This snippet calculates the factorial of a given
number using recursion.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
n=5
result = factorial(n)
print(f"The factorial of {n} is {result}")
Output: The factorial of 5 is 120
8. Checking if a String
is a Palindrome
A palindrome is a word, phrase, number, or other sequence
of characters that reads the same backward as forward.
This snippet checks if a given string is a palindrome by
comparing the original string with its reversed version.
def is_palindrome(string):
return string == string[::-1]
string = "racecar"
if is_palindrome(string):
print(f"{string} is a palindrome")
else:
print(f"{string} is not a palindrome")
Output: racecar is a palindrome
9. Calculating the Average of a List of
Numbers
This snippet demonstrates how to calculate the average (mean) of a list of numbers. It uses a loop to
.
iterate through the list and sum up the numbers The sum is then divided by the length of the list to
obtain the average .
def calculate_average(numbers):
if len(numbers) == 0:
return 0
total = sum(numbers)
average = total / len(numbers)
return average
numbers = [10, 20, 30, 40, 50]
average = calculate_average(numbers)
print(f"The average of the list is: {average}")
:
Output The average of the list is : 30.0