0% found this document useful (0 votes)
7 views7 pages

Python Functions and Parameters Guide

This document covers Python functions, including their definition, syntax, parameters, and scope of variables. It explains the difference between local and global variables, as well as the use of default parameter values. Additionally, it provides an overview of the Python Standard Library and lists popular libraries used for various programming tasks.

Uploaded by

veddhobi9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

Python Functions and Parameters Guide

This document covers Python functions, including their definition, syntax, parameters, and scope of variables. It explains the difference between local and global variables, as well as the use of default parameter values. Additionally, it provides an overview of the Python Standard Library and lists popular libraries used for various programming tasks.

Uploaded by

veddhobi9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PRAYOSHA ENGINEERING CLASSES SUB: PYTHON | UNIT 4

----------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------

UNIT 4 Functions 16 Marks


Q.1 –Explain python function

 Python Functions:
 Functions are a great deal, even for other programming languages.
 Functions are important in Python at the point that we have built-in functions (functions pre-
defined in Python).
 A function is a block of code which only runs when it is called.
 You can pass data, known as parameters, into a function.
 A function can return data as a result.

 Syntax of Python Function:In Python a function is defined using the def keyword:
def name_of_function(parameters ):

"""This is a docstring"""

# code block

 To define a Python function, use def keyword.


 Here's the simplest possible function that prints’ Hello World!' on the screen.

Creating a Function:

 Example
def my_function():
print("Hello from a function")

Calling a Function: To call a function, use the function name followed by parenthesis:

Example
def my_function():
print("Hello from a function")
my_function()

Output: Hello from a function

[Link] - 7600031265 [Link] - 9429235711


PRAYOSHA ENGINEERING CLASSES SUB: PYTHON | UNIT 4
----------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------

 Key-parts of the function:



1. def keyword: The "def keyword" is used to write functions.
2. function name: The function's name created by the def statement. This permits us to define
functions once and call them in many parts of our code.
3. function parameters: The parameters are used to pass data into the function's body.
4. Colon: The colon (:) is a cue for the function's body. That is, the function body gets indented
after the colon.
5. function code: The function code also called the function body contains statements that get
executed when the function gets called.

 Pass 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.
 The following example has a function with one argument (fname).
 When the function is called, we pass along a first name, which is used inside the function to print
the full name:

Example
def my_function(fname):

print(fname + " Refsnes")

my_function("Emil")

my_function("Tobias")

my_function("Linus")

[Link] - 7600031265 [Link] - 9429235711


PRAYOSHA ENGINEERING CLASSES SUB: PYTHON | UNIT 4
----------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------

Q.2 – Define Parameters with defaults Values:

 By default, a function must be called with the correct number of arguments.


 Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments,
not more, and not less.
 If you try to call the function with 1 or 3 arguments, you will get an error which is known as
TypeError.

Example: This function expects 2 arguments, but gets only 1:


def my_function(fname, lname):

print(fname + " " + lname)

my_function("Emil")

Output:TypeError: my_function() missing 1 required positional argument: 'lname'

Example: This function expects 2 arguments, and gets 2 arguments:


def my_function(fname, lname):

print(fname + " " + lname)

my_function("Emil", "Refsnes")

Output: Emil Refsnes

 The following example shows how to use a default parameter value.


 If we call the function without argument, it uses the default value:

Example
def my_function(country = "Norway"):

print("I am from " + country)

my_function("India")

my_function()

Output: I am from India


I am from Norway

Rules for the global

[Link] - 7600031265 [Link] - 9429235711


PRAYOSHA ENGINEERING CLASSES SUB: PYTHON | UNIT 4
----------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------
Q.3 – Scope of Variable:
\

 There are two types of variables: global variables and local variables.
1. Local variable:
 Local variables are those which are defined inside a function and its scope is limited to that
function only.
 In other words, we can say that local variables are accessible only inside the function in which it
was initialized.
 Local variables are those which are initialized inside a function and belong only to that particular
function.
 It cannot be accessed anywhere outside the function. Let’s see how to create a local variable.
Example:
def f():
# local variable
s = "I love india"
print(s)
# Driver code
f()
[Link] Variables:
 Global variables are those which are not defined inside any function and have a global scope.
 Global variables are accessible throughout the program and inside every function.
 These are those which are defined outside any function and which are accessible throughout the
program, i.e., inside and outside of every function.
 Let’s see how to create a global variable.
Example:
# This function uses global variable s
def f():
print("Inside Function", s)
# Global scope
s = "I love India"
f()
print("Outside Function", s)
 Rules of global keyword:
1. If a variable is assigned a value anywhere within the function's body, it's assumed to be a local
unless explicitly declared as global.
2. Variables that are only referenced inside a function are implicitly global.
3. We Use global keyword to use a global variable inside a function.
4. There is no need to use global keyword outside a function.

[Link] - 7600031265 [Link] - 9429235711


PRAYOSHA ENGINEERING CLASSES SUB: PYTHON | UNIT 4
----------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------
Example: # Python program to modify a global# value inside a function
x=15

def change(): #using a global keyword

global x #increments value of x by 5

x+=5

print(“value of x inside of function :”,x)

change()

print(“value of x outside of function :”,x)

 Q.4– Python standard library


\

 Normally, a library is a collection of books or is a room or place where many books are stored to be
used later.
 Similarly, in the programming world, a library is a collection of precompiled codes that can be used
later on in a program for some specific well-defined operations.
 Other than pre-compiled codes, a library may contain documentation, configuration data, message
templates, classes, and values, etc.
 A Python library is a collection of related modules. It contains bundles of code that can be used
repeatedly in different programs.
 It makes Python Programming simpler and convenient for the programmer.
 As we don’t need to write the same code again and again for different programs.
 Python libraries play a very vital role in fields of Machine Learning, Data Science, Data Visualization,
etc.
 The Python Standard Library contains the exact syntax, semantics, and tokens of Python.
 It contains built-in modules that provide access to basic system functionality like I/O and some other
core modules.
 Most of the Python Libraries are written in the C programming language.
 The Python standard library consists of more than 200 core modules.
 All these work together to make Python a high-level programming language.
 Python Standard Library plays a very important role.
 Without it, the programmers can’t have access to the functionalities of Python.
 But other than this, there are several other libraries in Python that make a programmer’s life easier.
Let’s have a look at some of the commonly used libraries:

[Link] - 7600031265 [Link] - 9429235711


PRAYOSHA ENGINEERING CLASSES SUB: PYTHON | UNIT 4
----------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------

These are some popular Python libraries and what they are commonly used for:

1. TensorFlow: It's a powerful library for high-level mathematical and scientific computations,
especially used in machine learning and deep learning.

2. Matplotlib: Used to create visual plots and charts, making it handy for data analysis and
visualization.

3. Pandas: A go-to tool for data scientists, it helps with data manipulation, cleaning, and analysis,
offering features like sorting, aggregation, and visualization.

4. NumPy: Stands for "Numerical Python" and is used for handling large arrays of numerical data,
often utilized in machine learning and scientific computations.

5. SciPy: Built on top of NumPy, it handles complex scientific computations and provides advanced
functionality for engineers and application developers.
6. Scrapy: Used to extract data from websites, making it valuable for web scraping, data mining,
and automated testing.

7. Scikit-learn: A popular machine learning library, it offers tools for various machine learning
algorithms, including regression, classification, and clustering.

8. PyGame: Ideal for developing video games, it provides tools for graphics, audio, and input, all
while using Python.

9. PyTorch: A machine learning library that excels in tensor computations, with strong GPU
support, often used for neural network applications.

10. PyBrain: Designed for beginners in machine learning, it offers easy-to-use algorithms for tasks
related to reinforcement learning, artificial intelligence, and neural networks.

[Link] - 7600031265 [Link] - 9429235711


PRAYOSHA ENGINEERING CLASSES SUB: PYTHON | UNIT 4
----------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------

[Link] - 7600031265 [Link] - 9429235711

You might also like