Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

UNit 2 python

The document provides an overview of Boolean and void functions in Python, including examples for checking even/odd numbers and printing messages. It also covers various string operations, the math module's functions, and several Python programs like generating a Fibonacci series and checking for palindromes. Each section includes code snippets demonstrating the concepts discussed.

Uploaded by

robrayl02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

UNit 2 python

The document provides an overview of Boolean and void functions in Python, including examples for checking even/odd numbers and printing messages. It also covers various string operations, the math module's functions, and several Python programs like generating a Fibonacci series and checking for palindromes. Each section includes code snippets demonstrating the concepts discussed.

Uploaded by

robrayl02
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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(text.lower()) # Output: hello world
print(text.upper()) # Output: HELLO WORLD
print(text.title()) # 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 = s.split(",") # 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(s.find("Python")) # Output: 7 (index where "Python" starts)
print(s.replace("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


math.sqrt(x) Square root of x math.sqrt(25) → 5.0
math.factorial(x) Factorial of x math.factorial(5) → 120
math.pow(x, y) x raised to power y math.pow(2, 3) → 8.0
math.ceil(x) Rounds up x math.ceil(3.2) → 4
math.floor(x) Rounds down x math.floor(3.8) → 3
math.pi Value of π math.pi → 3.141592653589793
math.sin(x) Sine of x (in radians) math.sin(math.pi/2) → 1.0

Example: Using math Functions

import math

print(math.sqrt(16)) # Output: 4.0


print(math.factorial(5)) # Output: 120
print(math.ceil(2.3)) # Output: 3
print(math.floor(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

You might also like