3. Function in python
In Python, a function is a block of organized, reusable code that
performs a specific task. It takes one or more input parameters,
performs some operations, and returns a value or set of values.
Some benefit of function :
Simplify code. More readable.
Reuse. Once written, use again
9. By default, a function must be called with the correct number of
arguments. Meaning that if your function expects 2 arguments, you have
to call the function with 2 arguments, not more, and not less.
10.
11. What is the difference between
argument and parameter ?
a parameter refers to the variable name that is used in the
function definition, which represents a value that needs to be
passed into the function when it is called.
An argument, on the other hand, refers to the actual value that is
passed to the function when it is called.
12. In this example, name is a parameter of the greet() function.
When the function is called with the argument "Rund", the
value of “Rund" is passed to the parameter name. The function
then prints the greeting "Hello, Rund!".
In summary, parameters are used in function definitions to
represent input values, while arguments are the actual values
that are passed to a function when it is called.
13. In Python, you can assign a default value to a function
parameter. This means that if the function is called without a
corresponding argument for that parameter, the default value
will be used instead.
14. In this code, the info() function takes two parameters name and age.
When the function is called on the last line with age=19 and
name="Ahmed", the arguments are passed to the function in a named
order. This means that the age argument is assigned to the age
parameter, and the name argument is assigned to the name parameter.
The function then returns a string that concatenates the name and age
values, separated by a colon and a newline character. Finally, this
string is printed to the console using the print() function.
15. Task 1
Write a Python function that accepts a string and counts the number of upper and
lower case letters.
Sample String : 'The quick Brow Fox'
Expected Output :
No. of Upper case characters : 3
No. of Lower case Characters : 12
Write a Python program to print the even numbers from a given list.
Sample List : [1, 2, 3, 4, 5, 6, 7, 8, 9]
Expected Result : [2, 4, 6, 8]
16. Task 2
Write a Python function that takes a list and returns a new list with distinct
elements from the first list.
Sample List : [1,2,3,3,3,3,4,5]
Unique List : [1, 2, 3, 4, 5]
17. Task 3
Write a Python program to print the even numbers from a given list.
Sample List : [1, 2, 3, 4, 5, 6, 7, 8, 9]
Expected Result : [2, 4, 6, 8]