Unit4 Part01
Unit4 Part01
Unit IV – Part 01
Functions
SS2024
Introduction to Python Programming (CPL103)
Objectives
Functions
def greet():
print('Hello World!')
A centre of excellence in science and technology enriched with GNH values
College of Science and Technology
CST Royal University of Bhutan
Cont.
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
Output:
The youngest child is Linus
Scope
1. Local Variables
def greet():
# local variable
message = 'Hello’ Output:
print('Local', message) Local Hello
greet() NameError: name
#outside the greet() function 'message' is not defined
print(message)
A centre of excellence in science and technology enriched with GNH values
College of Science and Technology
CST Royal University of Bhutan
Cont.
Exercise:
Output?
Global Variable?
Local Variable?
3. Nonlocal Variables
Python Docstrings
• The closing quotes are on the same line as the opening quotes.
• There's no blank line either before or after the docstring.
• They should not be descriptive, rather they must follow "Do this, return
that" structure ending with a period.
Comments
Comments in Python start with a hash mark (#) and are intended
to explain the code to developers. They are ignored by the
Python interpreter.
Docstrings
It provide a description of the function, method, class, or module.
Unlike comments, they are not ignored by the interpreter and can
be accessed at runtime using the .__doc__ attribute.
Function Recursion
Class Activity
Home Assignment
Group Activity
Reference
Python tutorial. (n.d.). https://www.w3schools.com/python/
Automate the boring stuff with python. Automate the Boring Stuff with
Python book cover thumbnail. (n.d.). https://automatetheboringstuff.com
Programiz. (n.d.). Python Operators: Arithmetic, Comparison, Logical and
more. Www.programiz.com. https://www.programiz.com/python-
programming/operators
GfG. (2024). C Operators. GeeksforGeeks; GeeksforGeeks.
https://www.geeksforgeeks.org/quizzes/operators-gq/
Thank You
SS2024
Introduction to Python Programming (CPL103)