Python Basic
04Python:
The Python language allows for flexibility in how arguments are passed to functions:
- Positional-or-Keyword Arguments: When a function’s definition doesn’t contain ”/” or ”*”, arguments can be passed either by position or by keyword.
- Positional-Only Parameters: Certain parameters can be marked as positional-only, meaning their order matters and they cannot be passed by keyword. These parameters are placed before a ”/” in the function definition. If there’s no ”/”, there are no positional-only parameters.
- Keyword-Only Arguments: Parameters can also be marked as keyword-only, requiring them to be passed by keyword argument. To do this, place an ”*” in the arguments list just before the first keyword-only parameter.
In summary, Python provides various ways to specify how arguments should be passed to functions, offering flexibility and control over function invocation.
#Example:
def ask_me2(response,/): # /: with this you can only pass position arguments
print("Are you okay?")
print(response)
# ask_me2(response='okay Mam'): Error: This is passed as keyword argument
ask_me2("okay") # this is positional argument
def sum(*numbers):
sum=0
for num in numbers:
sum = sum + num
print(sum)
sum(1,2,3) # only arguments are send
- Arbitrary Argument Lists: This option allows a function to be called with any number of arguments, which are then wrapped up in a tuple. These arguments are specified using *args in the function definition.
Python offers the flexibility to define functions that accept an arbitrary number of arguments using *args, which collects additional arguments into a tuple. This allows for versatile function definitions and invocation patterns.
- Optional Metadata: Function annotations are optional and serve as metadata about the types used in the function. They do not affect the function’s behaviour but provide additional information about the function’s parameters and return value.
- Modules:
Python allows users to define functions and variables, which are lost when the interpreter session is exited. To create longer programs, it’s recommended to use a text editor to prepare a script file. As programs grow, they can be split into multiple files for easier maintenance. Python provides a way to organize and reuse code by using modules.
A module is a file containing Python definitions and statements, with the file name being the module name with the “.py” extension. Within a module, the module’s name is available as the value of the global variable name. Modules can be imported into other modules or into the main module, allowing the use of definitions and functions across multiple programs.
As an example, a module named “fibo.py” is created to define functions for generating Fibonacci numbers. It contains two functions: fib() to print Fibonacci series up to a given number, and fib2() to return a list containing Fibonacci series up to a given number. These functions can then be imported and used in other Python scripts.
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a+b
return result. Make a short summary of the above content.