[go: up one dir, main page]

0% found this document useful (0 votes)
23 views4 pages

Lab3-Pythonsem223 24

Uploaded by

Aisyah Hanis
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)
23 views4 pages

Lab3-Pythonsem223 24

Uploaded by

Aisyah Hanis
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
You are on page 1/ 4

Centre for Diploma Studies Page 1/4

Department of Information Technology Session 2023/2024


Lab Practical Title: Introduction to Python III Semester 2

CENTRE FOR DIPLOMA STUDIES

ARTIFICIAL INTELLIGENCE

LABORATORY INSTRUCTION SHEET

Course Code DAT 21003

Lab Practical Title Introduction to Python

Lab Practical 3
Centre for Diploma Studies Page 2/4
Department of Information Technology Session 2023/2024
Lab Practical Title: Introduction to Python III Semester 2

AIM: Know about Python Functions

1.0 OBJECTIVE

At the end of this lab practical, students will be able to:

1. Write simple coding for Python.

2.0 TUTORIAL

A function is a block of code that performs a specific task. There are two types of function in
Python programming:
• Standard library functions - These are built-in functions in Python that are available to
use.
• User-defined functions - We can create our own functions based on our requirements.

Calling a Function in Python


1. When the function is called, the control of the program goes to the function definition.
2. All codes inside the function are executed.
3. The control of the program jumps to the next statement after the function call.
4. A function can also have arguments. An argument is a value that is accepted by a
function.
5. A Python function may or may not return a value. If you want the function to return
some value to a function call, you have to use the return statement. The return
statement also denotes that the function has ended. Any code after return is not
executed.
6. In Python, standard library functions are the built-in functions that can be used directly
in our program. For example: print(), sqrt() and pow(). The library functions are
defined inside the module and to use them you must include the module inside the
program.
7. Benefits of Using Functions:
• Code Reusable – you can use the same function multiple times in your program
which makes your code reusable.
• Code Readability - Functions help you break the code into chunks to make the
program readable and easy to understand.

Python Function Arguments


1. In computer programming, an argument is a value that is accepted by a function. In
Python, you can provide default values to function arguments.
2. In keyword arguments, arguments are assigned based on the name of arguments. The
position of arguments doesn't matter.
Centre for Diploma Studies Page 3/4
Department of Information Technology Session 2023/2024
Lab Practical Title: Introduction to Python III Semester 2

3. Sometimes, you do not know in advance the number of arguments that will be passed
into a function. To handle this kind of situation, you can use arbitrary arguments in
Python. Arbitrary arguments allow us to pass a varying number of values during a
function call. You use an asterisk (*) before the parameter name to denote this kind of
argument.

Python Recursion
1. Recursion is the process of defining something in terms of itself. A physical world
example would be to place two parallel mirrors facing each other. Any object in
between them would be reflected recursively.
2. In Python, we know that a function can call other functions. It is even possible for the
function to call itself. These types of construct are termed as recursive functions.
3. Advantages of Recursion:
• Recursive functions make the code look clean and elegant.
• A complex task can be broken down into simpler sub-problems using recursion.
• Sequence generation is easier with recursion than using some nested iteration.
4. Disadvantages of Recursion:
• Sometimes the logic behind recursion is hard to follow through.
• Recursive calls are expensive (inefficient) as they take up a lot of memory and
time.
• Recursive functions are hard to debug.

Python Variable Scope


1. In Python, you can declare variables in three different scopes: local scope, global, and
nonlocal scope. A variable scope specifies the region where you can access a variable.
2. Python Local Variables:
• When you declare variables inside a function, these variables will have a local
scope (within the function). You cannot access them outside the function.
These types of variables are called local variables.
3. Python Global Variables
• In Python, a variable declared outside of the function or in global scope is
known as a global variable. This means that a global variable can be accessed
inside or outside of the function.
4. Python Nonlocal Variables
• In Python, nonlocal variables are used in nested functions whose local scope is
not defined. This means that the variable can be neither in the local nor the
global scope.

Python Modules
As a program grows bigger, it may contain many lines of code. Instead of putting everything
in a single file, you can use modules to separate codes in separate files as per their
functionality. This makes your code organized and easier to maintain.
1. Module is a file that contains code to perform a specific task. A module may contain
variables, functions, classes etc.
Centre for Diploma Studies Page 4/4
Department of Information Technology Session 2023/2024
Lab Practical Title: Introduction to Python III Semester 2

2. You can import the definitions inside a module to another module or the interactive
interpreter in Python. You use the import keyword to do this.
3. Python has tons of standard modules. You can check out the full list of Python
standard modules and their use cases. Standard modules can be imported the same way
as we import our user-defined modules. Suppose you want to get the value of pi, first
you import the math module and use math.pi.
4. In Python, you can also import a module by renaming it. You have renamed the math
module as m. This can save you typing time in some cases.
5. You can import specific names from a module without importing the whole module.
6. You can import all names (definitions) from a module using the following construct:
you have imported all the definitions from the math module. This includes all names
visible in your scope except those beginning with an underscore (private definitions).
However, importing everything with the asterisk (*) symbol is not a good
programming practice. This can lead to duplicate definitions for an identifier. It also
hampers the readability of your code.
7. In Python, you can use the dir() function to list all the function names in a module.

3.0 EXERCISE

1. Write a program to create a function that takes two arguments (laptop and year) and print
their value. (3 marks)

2. Write a program that calculates the multiply of numbers ranging from 1 to 10. (3 marks)

3. Write a program to create a recursive function to calculate total numbers from 0 to 20.
(4 marks)

4. Write a program to create function calculate ( ) such that it can accept two variables and
calculate add and subtract. (6 marks)

You might also like