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

Python_Unit2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python_Unit2

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Unit-II: Functions, Strings, List, Tuples, Dictionaries

Python Functions
• Function is a group of related statements that perform a specific
task.
• The idea is to put some commonly or repeatedly done tasks
together and make a function so that instead of writing the same
code again and again for different inputs, we can do the function
calls to reuse code contained in it over and over again.
Python Function Declaration
• The syntax to declare a function is:

Advantages of Using Functions:


• The main of advantage of functions is code is Reusability by making
modules.
• As our program grows larger and larger, functions make it more
organized and manageable. It helps Code Readability for developers.
Categories of functions
 1. Built in Functions
 2. User Defined Functions
1. Built in Functions:
• Functions that are built into Python.
• The function which are coming along with python software
automatically, are called built-in functions or pre-defined functions.
Example: id(), type(), input(), print() etc…
2. User Defined Functions:
 The functions which are developed by programmer explicitly
according to business requirements, are called User defined
functions.

Creating and Calling a Function in Python: We can define a function in


Python, using the def keyword.
Ex:

Arguments:
• Information can be passed into functions as arguments
• Arguments are specified after the function name, inside the
parentheses. You can add as many arguments as you want, just separate
them with a comma.
Types of Python Function Arguments
• Python supports various types of arguments that can be passed at the
time of the function call. In Python, we have the following function
argument types in Python:
• Default argument
• Keyword arguments (named arguments)
• Positional arguments
• Arbitrary arguments (variable-length arguments *args and **kwargs)
Note: Any number of arguments in a function can have a default value. But once we
have a default argument, all the arguments to its right must also have default
values.
Keyword Arguments
Scenario1:
1. Default Arguments Should Follow Non-Default Arguments
def add(a=5,b,c):
return (a+b+c)
#Output:SyntaxError: non-default argument follows default
argument
2. Keyword Arguments Should Follow Positional Arguments
def add(a,b,c):
return (a+b+c)

print (add(a=10,3,4))
#Output:SyntaxError: positional argument follows keyword
argument

3. All Keyword Arguments Passed Must Match One of the Arguments


Accepted by the Function, and Their Order Isn’t Important
def add(a,b,c):
return (a+b+c)

print (add(a=10,b1=5,c=12))
#Output:TypeError: add() got an unexpected keyword argument
'b1'

4. No Argument Should Receive a Value More Than Once


def add(a,b,c):
return (a+b+c)

print (add(a=10,b=5,b=10,c=12))
#Output:SyntaxError: keyword argument repeated
Python Anonymous (Lambda) Functions:
• These are anonymous functions means that the function is without a name.
• As we already know the def keyword is used to define a normal function in
Python. Similarly, the lambda keyword is used to define an anonymous
function in Python.
Python Lambda Function Syntax
Syntax: lambda arguments : expression
• This function can have any number of arguments but only one
expression, which is evaluated and returned.

Python Variable scope


Scope: The scope of a variable refers to the places where we can access
a variable.
• Depending on the scope, the variable can categorize into two
types local variable and the global variable.
Local variable
• A local variable is a variable that is accessible inside a block of
code(function) only where it is declared.
Global variable
• A Global variable is a variable that is defined outside of the method
(block of code). That is accessible anywhere in the code file.
Points to Remember:
• We cannot access the local variables from outside of the function. Because
the scope is local, those variables are not visible from the outside of the
function.
• Note: The inner function does have access to the outer function’s local
scope.
Example
Global Keyword in Function: Declaring a variable as global, which makes perform
the modification on global variable using the inside function.

Without global keyword:

With global keyword:


Nonlocal Variable in Function

In Python, nonlocal is the keyword used to declare a variable that acts as a


global variable for a nested function (i.e., function within another function).

Example
String data type :
 The most commonly used object in any project and in any
programming, language is Sting.
What is String?
 Any sequence of characters within either single or double
quotes is considered as a String.
Syntax: S=‘SNIST’ or s=“SNIST”
 In most of other languages like C,C++,java a single
character with in single quote is treated as char data type
value.
 But in python we are not having char data type.
 Hence it is treated as String only.

3. e
4. error
5. ell
String1=” Ravi Placings”

1. substr1 = string1[5:10]
• Output: "Placi"

2. substr2 = string1[:10]
• Output: "Ravi Placings"[:10] = "Ravi Placi"

3. substr3 = string1[5:]
• Output: "Placings"

4. substr4 = string1[:]
• Output: "Ravi Placings"

5. substr5 = string1[-8:-3]
• Output: "Placi"
6. substr6 = string1[5::2]
• Output: "Pains"

7. substr7 = string1[::-1]
• Output: "sgnicalP ivaR"

8. substr8 = string1[5:12].upper()
• Output: "PLACING"
List:
• group of individual objects as a single entity
• Insertion order preserved
• Duplicate objects are allowed
• Heterogeneous objects are allowed.

• List is dynamic because based on requirement we can increase the


size and decreases the size.
• List objects are mutable .i.e, we can change the content.
 Tuple is exactly same as List except that is immutable. i.e.
once we create Tuple object, we cannot perform any changes
in that object.
 Heterogeneous objects are allowed.
 Tuple supports both +ve and –ve index
 We can apply + and * operators for tuple.
By using tuple() function:
min() and max() functions:
 These functions return min and max values in a given tuple.

Comparing tuples:
t1=(40,20,30)

t2=(10,20,30)
t3=(10,20,30)

print(t2==t3) True

You might also like