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.
void function
• It is possible to define a function without a return statement.
• These functions are known as void functions, and they return None.
Anonymous Functions
• These functions are called anonymous because they are not declared
in the standard manner by using the def keyword.
• Lambda keyword is used to create small anonymous functions.
• Lambda expression can take any number of arguments but return just
one value in the form of an expression.
• They cannot contain commands or multiple expressions. An
anonymous function cannot be a direct call to print because lambda
requires an expression.
Scope and Lifetime of Variables
• The scope of a variable: The scope determines the portion of the
program where we can access a particular identifier.
• There are two basic scopes of variables in Python:
• Global variables
• Local variables
Scope and Lifetime of Variables
• Variables that are defined inside a function body have a local scope,
and those defined outside a function have a global scope.
Immutable sequence of characters
Written in ‘ ‘ or “ “
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’))
Follow the steps that
is given in the picture
• Naming a Module
• Can name the module file whatever you like, but it must have the file
extension .py
• Re-naming a Module
• Can create an alias when you import a module, by using the as keyword
Python Module Attributes
• Python module attributes refer to the characteristics or properties
associated with a module.
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