1 of 14

Computer Programming I

Fundamentals Session 6

Dr. Daniel Precioso

dprecioso@faculty.ie

2 of 14

The Python Scope

3 of 14

Python Scope

Python has a set of rules for variable scope.

Variables can be inside…

4 of 14

Local Scope

The local scope is the innermost scope, typically within a function or a code block (e.g., within a loop).

Variables defined within this scope are accessible only within that specific function or block.

5 of 14

Enclosing Scope

It includes variables defined in a higher-level function or a surrounding code block.

These variables are accessible within the local scope of the nested function or block.

6 of 14

Global Scope

The global scope is the outermost scope, and it encompasses the entire Python script or module.

Variables defined at this level are accessible from anywhere in the code, including within functions and blocks.

7 of 14

Built-in Scope

This is the highest level of scope and includes all the names and objects that are always available when you run a Python program.

This scope contains the built-in functions, exceptions, and objects that are part of the Python language itself.

You don't need to import any modules or define anything special to access these built-in names; they are automatically available.

We have already learned some build-in functions, such as print() and len().

8 of 14

Python Scope

LEGB Rule

In Python, variable lookup follows the LEGB rule, which stands for Local, Enclosing, Global, and Built-in scopes.

When you reference a variable, Python first looks in the local scope, then the enclosing scope, followed by the global scope, and finally the built-in scope (which includes Python's built-in functions and objects).

9 of 14

Docstrings

& Type Hints

10 of 14

Docstrings

A docstring is a string that provides documentation for a function or module.

It helps developers understand how to use the code.

Docstrings are enclosed in triple quotes.

def greet(name):

"""

This function greets a person.

Parameters

----------

name : str

The name of the person to greet.

Returns

-------

str

A greeting message.

"""

return f"Hello, {name}!"

11 of 14

Type Hints

Type hints are a way to specify the types of function arguments and return values in your code.

While Python does not enforce these types at runtime, they serve as valuable documentation for understanding code.

To add a type hint for function arguments, simply place a colon : after the parameter name. To hint the output type, use a right arrow ->

def add(a: int, b: int) -> int:

return a + b

def check_pass(name: str, grade: int) -> str:

if grade >= 5:

return f"{name} has passed!"

else:

return f"{name} has failed"

12 of 14

Why Use Them?

Both docstrings and type hints can be seen on Google Colaboratory when we hover our cursor on the function name.

Try it now!

def check_pass(name: str, grade: int) -> str:

"""

This function returns a message saying if the student

has passed or not.

Parameters

----------

name : str

The student's name

grade : int

The student's score, between 0 and 10

Returns

-------

str

Message saying whether the student passed or not

"""

if grade >= 5:

return f"{name} has passed!"

else:

return f"{name} has failed"

13 of 14

From now on, I expect you to add docstrings and type hints to all the functions you write.

14 of 14

Project Euler

Now that we have learned about variables, flow control, loops, and functions, you have the perfect toolkit to dive into some of the most intriguing mathematical problems. Head over to Project Euler and challenge yourself with their extensive collection of exercises. You might even come across problems we have already explored together in class!

https://projecteuler.net/archives

Happy coding!