Unit 4-Functions Python Strings Modules
Unit 4-Functions Python Strings Modules
Modules
UNIT 4
A block of organized, reusable code that is
used to perform a specific task.
Example:-
Calling Function
def greet():
print('Hello World!’) greet()
Parameter and argument
• A parameter is a variable in a
function definition. It is a
placeholder and hence does not
have a concrete value.
• An argument is a value passed
during function invocation.
• In a way, arguments fill in the place
the parameters have held for them.
1. Positional Arguments or Required Arguments
• During a function call, values passed through arguments should be in
the order of parameters in the function definition. This is called
positional arguments.
2. default arguments
• Default arguments are values that are provided while
defining functions.
• The assignment operator = is used to assign a default
value to the argument.
• Default arguments become optional during the
function calls.
• If we provide a value to the default arguments during
function calls, it overrides the default value.
3. Keyword Arguments
• Functions can also be called using keyword
arguments
• During a function call, values passed through
arguments don’t need to be in the order of
parameters in the function definition. This can be
achieved by keyword arguments.
4. Variable-Length arguments
• We may not know how many arguments a function will
get ahead of time. To accommodate this type of
scenario, a Python function can be built to accept any
number of arguments.
Return
Statement
• The return statement exits
a function. The return
statement can return an
expression to the caller
function.
Immutable sequence of characters
Written in ‘ ‘ or “ “
Double quotes
• “My Final Year”
Triple quotes
• ‘’’My Final Year’’’
a = “python programming”
• print(a)
subject=“python programming”
• print(subject)
Assigning Multiline String to a Variable
subject=“””python programming,
computer Networks,Java,c
programming etc.”””
• print(subject)
Assigning Multiline String to a Variable
subject=‘’’python programming,
computer Networks,Java,c
programming etc.’’’
• print(subject)
Accessing String
Escape Sequence
Assignment
Learn what is escape sequence
Write a python program to implement escape sequence (all types)
Program and the output needs to be uploaded in assignment link
Deleting a string
+ * [] [:]
Fetches the
characters in
Concatenates Returns the the range
Appends the
multiple character at specified by
second string
copies of the the given two index
to the first
same string index operands
separated by
the : symbol
String Operators
In not in %
Returns True
Returns True
if a character Performs
if a character
does not String
exists in the
exist in the formatting
given string
given string
String Methods
String Methods
String Methods
String Methods
Modules
File that is containing a python code
Advantage of Modules
• Easy Access of data
Need of Modules
• Code reusability
• Time Saving
• Readable
Types of Modules
In case of error
!pip install hypothesis
import hypothesis
print(help("modules"))
In case of error while
using command
print(help(‘modules’))
Module Name
Module Variables
Module Functions
Module Name Module Variables
Example.py
main.py
Module Documentation (Docstring)
Module Functions
example_module.py
example_module.py
Main.py
Main.py
Namespace in Modules
• A namespace is a container that holds a set of identifiers (such as
functions, classes, variables) and provides a way to organize and avoid
naming conflicts.
Purpose
• Avoiding Naming Conflicts: Namespaces prevent naming collisions
by providing a scope for identifiers, ensuring that names are unique
within that scope.
• Organizing Code: Namespaces help in organizing code by grouping
related identifiers together.
Namespace in Modules
Types of Namespace
Built-In
Global
Enclosing
Local