Python unit 4
Python unit 4
When the user enters data, the input() method always returns a string
data type. If we want to change the data type of our inputs, we have to
use a different conversion method.
The input() method in Python allows you to get user-supplied string input.
The input() method asks the user to enter a string and returns the result
as a string. In this example, the below code takes the user to input their
name and stores it in the variable “name”. Then, it prints out the entered
name with a descriptive message.
Example:
Output
print(“Hello World!”)
Output
Hello World!
String handling functions are essential tools for manipulating text data.
Here’s an overview of some commonly used string functions in Python:
Example:
print(“\nConverted String:”)
print(text.upper())
print(“\nConverted String:”)
print(text.lower())
print(“\nConverted String:”)
print(text.title())
print(“\nConverted String:”)
print(text.swapcase())
print(“\nConverted String:”)
print(text.capitalize())
print(“\nOriginal String”)
print(text)
Output
Converted String:
Converted String:
Converted String:
Converted String:
Original String
Function Prototypes:
Example
def add():
a=int(input("enter a"))
b=int(input("enter b"))
c=a+b
return c
c=add()
print(c)
OUTPUT:
Enter a 5
Enter b 10
15
In this type arguments are passed through the function call and output is
return to the main function.
Example
def add(a,b):
c=a+b
return c
a=int(input(“enter a”))
b=int(input(“enter b”))
c=add(a,b)
print(c)
OUTPUT:
Enter a 5
Enter b 10
15
Return Statement
Example:
return a + b
result = add(3, 5)
print(result) # Output: 8
Nesting of Functions
def outer_function(x):
def inner_function(y):
return x + y
return inner_function
closure = outer_function(10)
print(closure(5)) # Output: 15
Categories of Functions
Example
print(result) # Output: 9
Example
def greet(name):
Example
add = lambda x, y: x + y
Example
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n – 1)
Example
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n – 1)
Immutable objects (e.g., int, float, str, tuple) behave like pass by value
because changes inside the function do not affect the original object.
Mutable objects (e.g., list, dict, set) behave like pass by reference
because changes inside the function modify the original object.
def modify(x):
print(“Inside function:”, x)
a= 5
modify(a)
Output
Inside function: 10
Outside function: 5
Pass by Reference (Mutable Objects)
def modify_list(lst):
numbers = [1, 2, 3]
modify_list(numbers)
Output:
[1, 2, 3, 4]
Local variables in Python are those which are initialized inside a function
and belong only to that particular function. It cannot be accessed
anywhere outside the function.
def func():
X = 10 # Local variable
print(x)
func() #Output 10
These are those which are defined outside any function and which are
accessible throughout the program, i.e., inside and outside of every
function. Let’s see how to create a Python global variable.
x = 10 # Global variable
def func():
global x
func()
print(x) # Output: 20
Storage Classes in Python
Python does not have explicit storage classes like automatic, external,
static, and register. However, Python provides similar behavior through
variable scope and lifetime management.
Variables declared inside a function are local and exist only during
the function’s execution.
Automatically created and destroyed when the function finishes
execution.
def func():
func()
Output: 10
Example
def compute():
return a + b
print(compute()) # Output: 15
Example
def outer():
x = 0 # Static-like variable
def inner():
x += 1
print(x)
return inner
counter = outer()
counter() # Output: 1
counter() # Output: 2
counter() # Output: 3
def compute():
return a + b
print(compute()) # Output: 15