Week 7-8:
Functions and Modular Programming
Understanding the Concept of Functions
• Functions are blocks of code that perform a specific task.
• They promote code reusability and maintainability.
• Functions make your code organized and easier to understand.
• a function has a name. The name can be anything that
we want it to be.
• It also (sometimes) has parameters. Information that
goes into the function.
• And also (sometimes) a return value
Defining and Calling Functions
Defining Functions
The def keyword is used to define a function.
greet is the function name.
(name) are the parameters the function accepts.
The colon : indicates the start of the function's body.
Calling Functions
To execute a function, you call it with arguments.
("Alice") is the argument passed to the greet function.
Parameters and Arguments in
Functions
Parameters are placeholders for values that a function expects.
Arguments are the actual values passed when calling the function.
python
Example-
Positional argument
Keyword argument
Example 1
Exercises
1.Write a Python function that takes a list of numbers as input and returns the sum of all the
even numbers in the list.
2.Create a function that converts a temperature in Celsius to Fahrenheit. The function should
take a Celsius temperature as input and return the corresponding temperature in Fahrenheit.
3.Create a function that checks if a given string is a palindrome (reads the same forwards and
backward). The function should return True if it's a palindrome and False if it's not.
4.Implement a function that calculates the factorial of a given integer. The function should
take an integer as input and return its factorial.
5.Write a function that takes a list of words and returns the longest word in the list. If there
are multiple words with the same maximum length, return all of them in a list.
1.Write a Python function that takes a list of numbers as input and returns the sum of all the even numbers
in the list.
2. Create a function that converts a temperature in Celsius to Fahrenheit. The function should
take a Celsius temperature as input and return the corresponding temperature in Fahrenheit.
(Fahrenheit = (Celsius * 9/5) + 32)
slicing notation- slice that starts at the end of the string, and moves backwards. In this particular
example, the slice statement [::-1] means start at the end of the string and end at position 0, move
with the step -1 , negative one, which means one step backwards.
3. Create a function that checks if a given string is a palindrome (reads the same forwards and
backward). The function should return True if it's a palindrome and False if it's not.
4. Implement a function that calculates the factorial of a given integer. The function
should take an integer as input and return its factorial.
5. Write a function that takes a list of words and returns the longest word in the list. If there are multiple
words with the same maximum length, return all of them in a list.
max() function in Python
Syntax:
Parameters:
1.iterable: This parameter is iterable. For example, tuple, list, dictionary, set, and a lot more.
2.*iterables (optional): This is an optional parameter indicating multiple iterables.
3.key (optional): This is also an optional parameter. This parameter works when the iterables are
passed, and the comparison is made on the basis of its return value.
4.default (optional): This is another optional parameter providing the default value to the
function if the specified iterable is void.
Example
Python Lambda
• A lambda function is a small anonymous function.
• A lambda function can take any number of arguments, but can only have one
expression.
Syntax
Using Lambda Expression
list() Function The python list() creates a list in python.
Syntax :
Python List sort() Method
Syntax :
- key (optional): A function to execute to decide the order. Default is None.
- reverse (optional): A Boolean. If True, the list is sorted in descending
order. Default is False.
Example: ascending
Example: descending
Using Lambda Function with filter()
Syntax:
num = [1, 2, 3, 4, 5] num = [1, 2, 3, 4, 5]
my_list = filter(lambda x: x<3, num) my_list = list(filter(lambda x: x<3, num))
print(list(my_list)) print(my_list)
Example
Using Lambda Function with map()
Syntax:
to each item of the numbers list, and the result is a new iterator containing the squared
values of each number.
Example
The main difference between a map and a filter is the return of values. A map will always
have a representation for elements in the list. The filter will filter out the only elements that
will meet the conditions in the function.
List Comprehension
Syntax:
Example
Exercises
Exceptions
• How to handle error in python