Here are detailed answers to your questions:
1. Boolean Function with Examples
A Boolean function is a function that returns a Boolean value (True or False). These
functions are commonly used in decision-making processes in programming.
Example 1: Checking Even or Odd
def is_even(num):
return num % 2 == 0 # Returns True if number is even, False otherwise
print(is_even(4)) # Output: True
print(is_even(7)) # Output: False
Example 2: Checking If a Number is Prime
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
print(is_prime(11)) # Output: True
print(is_prime(10)) # Output: False
2. Void Function with Examples
A Void function is a function that does not return a value (returns None by default). It is
often used to perform an action rather than compute and return a result.
Example 1: Printing a Message
def greet():
print("Hello, welcome to Python!")
greet() # Output: Hello, welcome to Python!
Example 2: Displaying a List
def display_list(items):
for item in items:
print(item)
fruits = ["Apple", "Banana", "Cherry"]
display_list(fruits)
# Output:
# Apple
# Banana
# Cherry
3. Various String Operations in Python
Python provides various string operations, including:
1. String Concatenation
s1 = "Hello"
s2 = "World"
result = s1 + " " + s2
print(result) # Output: Hello World
2. String Slicing
s = "Python"
print(s[0:4]) # Output: Pyth
print(s[-1]) # Output: n
3. Converting Case
text = "Hello World"
print([Link]()) # Output: hello world
print([Link]()) # Output: HELLO WORLD
print([Link]()) # Output: Hello World
4. Checking Substring
s = "Python Programming"
print("Python" in s) # Output: True
print("Java" in s) # Output: False
5. String Splitting and Joining
s = "apple,banana,grape"
words = [Link](",") # Splits based on ","
print(words) # Output: ['apple', 'banana', 'grape']
joined = "-".join(words)
print(joined) # Output: apple-banana-grape
6. Finding and Replacing a Substring
s = "I love Python"
print([Link]("Python")) # Output: 7 (index where "Python" starts)
print([Link]("Python", "Java")) # Output: I love Java
4. Math Module
The math module in Python provides various mathematical functions.
1. Importing the Math Module
import math
2. Common Functions in math Module
Function Description Example
[Link](x) Square root of x [Link](25) → 5.0
[Link](x) Factorial of x [Link](5) → 120
[Link](x, y) x raised to power y [Link](2, 3) → 8.0
[Link](x) Rounds up x [Link](3.2) → 4
[Link](x) Rounds down x [Link](3.8) → 3
[Link] Value of π [Link] → 3.141592653589793
[Link](x) Sine of x (in radians) [Link]([Link]/2) → 1.0
Example: Using math Functions
import math
print([Link](16)) # Output: 4.0
print([Link](5)) # Output: 120
print([Link](2.3)) # Output: 3
print([Link](4.8)) # Output: 4
5. Python Programs
(a) Fibonacci Series
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
fibonacci(10) # Output: 0 1 1 2 3 5 8 13 21 34
(b) Factorial Using Recursion
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5)) # Output: 120
(c) Check Whether a String is Palindrome
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome("madam")) # Output: True
print(is_palindrome("hello")) # Output: False
(d) Find the Square of a Number Using Functions
def square(n):
return n * n
print(square(5)) # Output: 25