UNIT -3
REQUIRED ARGUMENTS IN PYTHON:
Required arguments are the arguments passed to a function in correct positional
order. Here, the number of arguments in the function call should match exactly with
the function definition.
To call the function printme(), you definitely need to pass one argument, otherwise it
gives a syntax error as follows −
Example
Live Demo
#!/usr/bin/python
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str
return;
# Now you can call printme function
printme()
Output
When the above code is executed, it produces the following result −
Traceback (most recent call last):
File "[Link]", line 11, in <module>
printme();
TypeError: printme() takes exactly 1 argument (0 given)
KEYWORD ARGUMENTS:
Keyword arguments (or named arguments) are values that, when
passed into a function, are identifiable by specific parameter names. A
keyword argument is preceded by a parameter and the assignment
operator, = . Keyword arguments can be likened to dictionaries in that
they map a value to a keyword.
DEFAULT ARGUMENTS:
Python allows function arguments to have default values; if the function is called
without the argument, the argument gets its default value
Default arguments:
Example
Python has a different way of representing syntax and default values for function
arguments. Default values indicate that the function argument will take that value if
no argument value is passed during function call. The default value is assigned by
using assignment (=) operator. Below is a typical syntax for default argument. Here,
foo parameter has a default value Hi!
def defaultArg(name, foo='Come here!'):
print name,foo
defaultArg('Joe')
Output
Joe Come here!
We see that in the above code there is one required argument and one default one
in the declaration. In the output we see that both the arguments are printed even
though only one argument was passed in the function call. The default argument is
passed automatically and appears in the output of the function call.
As the name implies, an argument with a variable length can take on a variety of
values. You define a variable argument using a '*', for example *args, to show that
the function can take a variable number of arguments.
Observations on Python's variable-length arguments are as follows.
The designation "*args" for variable length arguments is not required. The only
thing needed is *; the variable name can be anything, like *names or
*numbers.
You can send zero or more arguments to a function using a variable length
argument.
A tuple is used to store the values passed to *args.
A formal argument may come before a variable args but not after one. You
can use keyword arguments following a variable argument.
*args in function
To pass a variable number of arguments to a function in Python, use the special
syntax *args in the function specification. It is used to pass a variable-length,
keyword-free argument list. By convention, the sign * is frequently used with the
word args in the syntax for taking in a variable number of arguments.
You can accept additional arguments using *args than the number of formal
arguments you previously defined. Any number of additional arguments can be
added to your current formal parameters using *args (including zero extra
arguments).
For instance, we wish to create a multiply function that can multiple any number of
inputs simultaneously. The use of variable parameters makes your function more
adaptable in situations where the precise number of arguments is unknown at first.
Imagine that you have a function that adds numbers.
Example 1
The following example demonstrates the usage of a regular function with fixed
number of parameters.
def add(num1, num2):
return num1+num2
print(add(4,5))
Output
The output generated is as follows.
9
You can specify that a function accepts a variable number of arguments and can be
used to add up to 'n' numbers by altering the argument to *args.
STRING COMPARISON:
String comparison is a fundamental operation in any programming language,
including Python. It enables us to ascertain strings’ relative positions, ordering,
and equality. Python has a number of operators and techniques for comparing
strings, each with a specific function. We will examine numerous Python string
comparison methods in this article and comprehend how to use them.
Input: "Geek" == "Geek"
"Geek" < "geek"
"Geek" > "geek"
"Geek" != "Geek"
Output: True
True
False
False
Explanation: In this, we are comparing two strings if they
are equal to each other.
Python String Comparison
Using Relational Operators
Using Regular Expression
Using Is Operator
Creating a user-defined function.
Equal to String Python using Relational Operators
The relational operators compare the Unicode values of the characters of the
strings from the zeroth index till the end of the string. It then returns a boolean
value according to the operator used. It checks Python String Equivalence.
Python3
print("Geek" == "Geek")
print("Geek" < "geek")
print("Geek" > "geek")
print("Geek" != "Geek")
Output
True
True
False
False
Equal to String Python using Regular Expression
In Python, you can use regular expressions to check Python String Equivalence
using the re module. Regular expressions provide a flexible and powerful way
to define patterns and perform pattern-matching operations on strings.
IMPORT STATEMENT:
Import in python is similar to #include header_file in C/C++. Python modules
can get access to code from another module by importing the file/function using
import. The import statement is the most common way of invoking the import
machinery, but it is not the only way.
import module_name
When the import is used, it searches for the module initially in the local scope
by calling __import__() function. The value returned by the function is then
reflected in the output of the initial code.
PYTHON
import math
pie = [Link]
print("The value of pi is : ",pie)
Output:
The value of pi is : ', 3.141592653589793
import module_name.member_name
In the above code module, math is imported, and its variables can be accessed
by considering it to be a class and pi as its object.
The value of pi is returned by __import__(). pi as a whole can be imported into
our initial code, rather than importing the whole module.
PYTHON
from math import pi
PYTHON MODULE:
A module allows you to logically organize your Python code. Grouping related code
into a module makes the code easier to understand and use. A module is a Python
object with arbitrarily named attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions,
classes and variables. A module can also include runnable code.
Example
The Python code for a module named aname normally resides in a file
named [Link]. Here's an example of a simple module, [Link]
def print_func( par ):
print "Hello : ", par
return
The import Statement
You can use any Python source file as a module by executing an import statement
in some other Python source file. The import has the following syntax −
import module1[, module2[,... moduleN]
When the interpreter encounters an import statement, it imports the module if the
module is present in the search path. A search path is a list of directories that the
interpreter searches before importing a module. For example, to import the module
[Link], you need to put the following command at the top of the script −
#!/usr/bin/python
# Import module support
import support
# Now you can call defined function that module as follows
support.print_func("Zara")
When the above code is executed, it produces the following result −
Hello : Zara
A module is loaded only once, regardless of the number of times it is imported. This
prevents the module execution from happening over and over again if multiple
imports occur.
The from...import Statement
Python's from statement lets you import specific attributes from a module into the
current namespace. The from...import has the following syntax −
from modname import name1[, name2[, ... nameN]]
For example, to import the function fibonacci from the module fib, use the following
statement −
from fib import fibonacci
This statement does not import the entire module fib into the current namespace; it
just introduces the item fibonacci from the module fib into the global symbol table of
the importing module.
DIR()FUNCTION: