Python_Lecture_Week7-8
Python_Lecture_Week7-8
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
Syntax
Using Lambda Expression
list() Function The python list() creates a list in python.
Syntax :
Python List sort() Method
Syntax :
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