UNit 2 python
UNit 2 python
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.
def is_even(num):
return num % 2 == 0 # Returns True if number is even, False otherwise
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
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.
def greet():
print("Hello, welcome to Python!")
def display_list(items):
for item in items:
print(item)
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
4. Checking Substring
s = "Python Programming"
print("Python" in s) # Output: True
print("Java" in s) # Output: False
s = "apple,banana,grape"
words = s.split(",") # Splits based on ","
print(words) # Output: ['apple', 'banana', 'grape']
joined = "-".join(words)
print(joined) # Output: apple-banana-grape
4. Math Module
import math
5. Python Programs
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
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
def is_palindrome(s):
return s == s[::-1]
def square(n):
return n * n
print(square(5)) # Output: 25