DEV Community

Keerti Sadana S
Keerti Sadana S

Posted on

PYTHON - Functions,Arguments,Module,Error Types

What is a function?
-> It is a block of code used to perform a specific task.
-> It can be reused.

How to define a function?
-> It is defined using the keyword def.
Syntax:

Example:-
code:
def fun():
print("Welcome to GFG")

Function calling:
def fun():
print("Welcome to GFG")
fun() #function calling
Output:
Welcome to GFG.

What are Arguments?
-> Arguments are variables that are passed to a function during a function call.
Syntax:

Types of Arguments (tbd):
-> Positional Arguments
-> Keyword Arguments
-> Default Arguments
-> Variable Length Arguments

How to check the type of variable or function?
-> The type of a variable or function can be checked using the type() function.
Code :
def add(no1, no2):
return no1 + no2
developer = 'nimal anand'
print(type(add))
print(type(developer))

Duck-Typed Language:
-> Duck typing means the computer cares about what something can do, not what it is.
-> Datatypes are not explicitly mentioned
-> Python is a duck-typed language.

What is modularity?
-> Modularity means breaking a big program into smaller, easier-to-manage parts (modules).
-> Easy maintenance.

What is a module?
-> A module is a file that contains code for a specific task, so you can reuse it whenever you need it.
code:


Output:
250
Members of module:
-> function - add
-> variable - name

Types of Errors:
-> TypeError
-> NameError
-> SyntaxError
-> ModuleNotFoundError

TypeError:
-> It is raised when an operation or function is applied to an object of an inappropriate type.
Examples:

  1. Adding a string and an integer
  2. Using len() on an integer
  3. Calling a function with the wrong number of arguments

NameError:
-> A NameError is raised when a variable or function name is not defined.
Example:

  1. Variable not defined
  2. Misspelt variable name

Top comments (0)