[go: up one dir, main page]

0% found this document useful (0 votes)
22 views15 pages

Unit 4

Uploaded by

berawor167
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)
22 views15 pages

Unit 4

Uploaded by

berawor167
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/ 15

4.

Functions, Tuples,
Dictionaries, Exceptions,
and Data Processing

By
M. Bhavani
M-Tech
UGC-NET qualified
Functions

1. A function is a block of code that performs a specific task when the


function is called (invoked). You can use functions to make your code
reusable, better organized, and more readable. Functions can have
parameters and return values.
2. There are at least four basic types of functions in Python:
a. built-in functions which are an integral part of Python (such as the
print() function). You can see a complete list of built-in Python
functions at https://docs.python.org/3/library/functions.html.
b. the ones that come from pre-installed modules
c. user-defined functions which are written by users for users ‒ you
can write your own functions and use them freely in your code,
d. the lambda functions
Continued
3. You can define your own function using the def keyword and the
following syntax:

def your_function(optional parameters):

# the body of the function

4. Function can be defined with any no. of parameters


How functions communicate with their
environment

1. You can pass information to functions by using parameters. Your


functions can have as many parameters as you need.
2. You can pass arguments to a function using the following techniques:
2.1. positional argument passing in which the order of arguments
passed matters (Ex. 1 of code in next slide)
2.2. keyword (named) argument passing in which the order of
arguments passed doesn't matter (Ex. 2 of code in next slide)
2.3. a mix of positional and keyword argument passing (Ex. 3 of code in
next slide)
Continued

3. It's important to remember that positional


arguments mustn't follow keyword arguments.
Python will not let you do it by signalling a
SyntaxError.
4. You can use the keyword argument-passing
technique to pre-define a value for a given
argument
Returning a result from a function
1. You can use the return
keyword to tell a function to
return some value. The return
statement exits the function
2. The result of a function can be
easily assigned to a variable
3. You can use a list as a function's
argument
4. A list can be a function result,
too.
Scopes in Python

1. The scope of a variable is the part of a


code where the name is properly
recognizable.
2. A variable that exists outside a function
has scope inside the function body unless
the function defines a variable of the
same name.
3. A variable that exists inside a function has
scope inside the function body.
4. You can use the global keyword followed
by a variable name to make the variable's
scope global.
Creating multi-parameter functions
1. A function can call other functions, or even itself. When a function calls
itself, this situation is known as recursion, and the function which calls
itself and contains a specified termination condition (i.e., the base case −
a condition which doesn't tell the function to make any further calls to
that function) is called a recursive function.
2. You can use recursive functions in Python to write clean, elegant code,
and divide it into smaller, organized chunks. On the other hand, you need
to be very careful as it might be easy to make a mistake and create a
function which never terminates. You also need to remember that
recursive calls consume a lot of memory, and therefore may sometimes
be inefficient.
3. When using recursion, you need to take all its advantages and
disadvantages into consideration.
LAB 12
Code to find the factorial of a number using Recursion
Tuples and dictionaries
● A sequence type is a type of data in Python which is able to store more
than one value (or less than one, as a sequence may be empty), and these
values can be sequentially (hence the name) browsed, element by
element.
● As the for loop is a tool especially designed to iterate through sequences,
we can express the definition as: a sequence is data which can be
scanned by the for loop
● mutability − is a property of any Python data that describes its readiness
to be freely changed during program execution. There are two kinds of
Python data: mutable and immutable.
● Immutable data cannot be modified in this way.
Tuples
● Tuples are ordered and unchangeable (immutable) collections of data.
They can be thought of as immutable lists. They are written in round
brackets: Eg., my_tuple = (1, 2, True, "a string", (3, 4), [5, 6], None)
● Empty tuple can be created like this my_tup=()
● You can access tuple elements by indexing them eg., my_tuple[3]
● When you try to change the tuple, python will throw typeError exception
but you can delete a tuple as a whole. Eg., del my_tuple
● You can also create a tuple using a Python built-in function called tuple().
This is particularly useful when you want to convert a certain iterable
(e.g., a list, range, string, etc.) to a tuple. Eg., tup2=tuple((1, 2, "string"))
● By the same fashion, when you want to convert an iterable to a list, you
can use a Python built-in function called list()
Dictionaries
1. Dictionaries are unordered*, changeable (mutable), and indexed
collections of data. Each dictionary is a set of key: value pairs. You can
create it by using the following syntax:

my_dictionary = { key1: value1, key2: value2, key3: value3,}

2. If you want to access a dictionary item, you can do so by making a


reference to its key inside a pair of square brackets (ex. 1) or by using the
get() method (ex. 2):
Continued
3. If you want to change the value associated with a specific key, you can do
so by referring to the item's key name in the following way:
pol_eng_dictionary["zamek"] = "lock"
4. To add or remove a key (and the associated value), use the following
syntax:

You can also insert an item into a dictionary by using the update()
method, and remove the last element by using the popitem() method
Continued
5. You can use the for loop to loop through a dictionary,

6. If you want to loop through a dictionary's keys and values, you can use
the items() method

7. To check if a given key exists in a dictionary, you can use the in keyword
Continued
8. You can use the del keyword to remove a specific item, or delete a
dictionary. To remove all the dictionary's items, you need to use the
clear() method

9. To copy a dictionary, use the copy() method

copy_dictionary = pol_eng_dictionary.copy()

You might also like