Python_Unit2
Python_Unit2
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:
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
print (add(a=10,b1=5,c=12))
#Output:TypeError: add() got an unexpected keyword argument
'b1'
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.
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.
Comparing tuples:
t1=(40,20,30)
t2=(10,20,30)
t3=(10,20,30)
print(t2==t3) True