Unit 4-Functions Python Strings Modules

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

Functions, Python strings &

Modules
UNIT 4
A block of organized, reusable code that is
used to perform a specific task.

Importance of functions – to reuse the


code

Functions are also called methods,


Functions procedures, subprograms, or subroutines.

Functions help reduce code redundancy


and give clarity.

Dividing the large problem into smaller


chunks
Syntax
Creation of Function
Syntax:-
def function_name():
\Statements

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 “ “

Example :- ‘I am learning python’


“I am learning python”
Creation of Strings
Single quotes
• ‘My Final Year’

Double quotes
• “My Final Year”

Triple quotes
• ‘’’My Final Year’’’

Triple double quotes


• ‘’’’’’My Final Year’’’’’’
Assigning String to a Variable

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

Updating and Deletion can not be done

Entire string can be deleted using the


keyword called del

Assigning new string to the existing


variable name can be done.
String Operators

+ * [] [:]
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

It can have statements, variables or function.

Advantage of Modules
• Easy Access of data

Need of Modules
• Code reusability
• Time Saving
• Readable
Types of Modules

Built-in Data User Defined


Type Data Type
1. Built-in Data Type
• To check what modules are available in python we use the command
print(help(“modules”))
This list will list out all the modules that are available inside python.

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
Python Module Attributes
• Python module attributes refer to the characteristics or properties
associated with a module.

Module Name

Module Documentation(Doc string)

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

You might also like