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

Unit 4-Functions_Python_strings_Modules

This document covers the fundamentals of functions, strings, and modules in Python. It explains the importance of functions for code reuse, the syntax for creating functions, and different types of arguments. Additionally, it discusses string creation, manipulation, and the advantages of using modules for code organization and reusability.

Uploaded by

kruthikaml17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 4-Functions_Python_strings_Modules

This document covers the fundamentals of functions, strings, and modules in Python. It explains the importance of functions for code reuse, the syntax for creating functions, and different types of arguments. Additionally, it discusses string creation, manipulation, and the advantages of using modules for code organization and reusability.

Uploaded by

kruthikaml17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

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

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

You might also like