0% found this document useful (0 votes)
4 views4 pages

Function

This document provides an overview of functions in Python, detailing their definition, parameters, docstrings, indentation, and return values. It includes an example function and a series of exercises designed to practice creating various types of functions. The exercises cover tasks such as summing numbers, finding maximum values, checking for prime numbers, and working with lists and strings.
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)
4 views4 pages

Function

This document provides an overview of functions in Python, detailing their definition, parameters, docstrings, indentation, and return values. It includes an example function and a series of exercises designed to practice creating various types of functions. The exercises cover tasks such as summing numbers, finding maximum values, checking for prime numbers, and working with lists and strings.
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

Function

Course BASIC PYTHON

Idx 6

Last edited @July 23, 2025 10:42 AM

Status Complete

Type Lesson

Notes
A function in Python is a named, reusable block of code designed to perform a
specific task. Functions promote code organization, reusability, and modularity,
making programs easier to understand, debug, and maintain.
Key characteristics of Python functions:

Definition:
Functions are defined using the def keyword, followed by the function name,
parentheses () , and a colon : .

Parameters:
Optional input values, called parameters, can be specified within the
parentheses during function definition. These act as placeholders for
arguments passed when the function is called.

Docstrings:
The first statement within a function can be an optional documentation string
(docstring), enclosed in triple quotes, which describes the function's purpose.

Indentation:
The code block belonging to a function must be indented, typically by four
spaces, following the colon.

Return Value:

Function 1
Functions can optionally return a value using the return statement. If
no return statement is present, the function implicitly returns None .

Calling a Function:
To execute the code within a function, it must be called by its name followed
by parentheses () , enclosing any necessary arguments.

Example of a Python function

def greet(name):
"""
This function takes a name as input and prints a greeting.
"""
print(f"Hello, {name}!")

# Calling the function


greet("Alice")

Exercises
Exercise 1
Write a Python function to sum/multiply all the numbers in a list.

Exercise 2
Write a Python function to find the maximum number in list.

Exercise 3
Writing a function calculate how many cans of paint you need in order to paint a
wall, given the height and width of the wall. Know that we must use 1 can per 5 m2 
of wall.

Exercise 4

Function 2
Write a Python function that takes a number as a parameter and checks whether
the number is prime or not.

Exercise 5
Write a Python function to reverse a string.

Exercise 6
Write a Python function that takes a list and returns a new list with distinct
elements from the first list.

Exercise 7
Write a Python program to create a lambda function that adds 15 to a given
number passed in as an argument, also create a lambda function that multiplies
argument x with argument y and prints the result.

r = lambda a: a + 15
print(r(10))

r = lambda x, y: x * y
print(r(12, 4))

Exercise 8
Write a Python function that takes a list of words and returns the longest word and
its length.
Example:

Input list: ["Hanoi", "Haiphong", "Nghean"]

The output should be:

Longest word: Haiphong


Length of the longest word: 8

Function 3
Exercise 9
Write a function that takes a sentence and returns a dictionary with the frequency
of each word.

Exercise 10
Create a function which takes a Gregorian year and return the corresponding
Vietnamese Lunar Year name (Can Chi). Then, find the next Gregorian year that
has the same Lunar Year name.

The Can-Chi (Heavenly Stem and Earthly Branch) cycles are shown in the wheel
diagram below:

Heavenly Stems (Can): Giáp, Ất, Bính, Đinh, Mậu, Kỷ, Canh, Tân, Nhâm, Quý
Earthly Branches (Chi): Tý, Sửu, Dần, Mão, Thìn, Tỵ, Ngọ, Mùi, Thân, Dậu, Tuất,
Hợi

Each combination of Can and Chi forms a unique year name, and the cycle
repeats every 60 years.

The reference point (base year) is the year 0, which is Canh Thân.

Function 4

You might also like