Lab3-Pythonsem223 24
Lab3-Pythonsem223 24
ARTIFICIAL INTELLIGENCE
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
1.0 OBJECTIVE
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.
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 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)