程序代写代做代考 python PowerPoint Presentation

PowerPoint Presentation

Basics in programming

Lectures 9.
Variable-length arguments in functions
Recursion in functions
Scope of variables
Mutable and Immutable data

Functions in Python, recap

Functions in Python, recap

Functions in Python, recap

>> What error is produced?

Variable length of arguments

Recursion

Recursion

Recursion

Recursion

Recursion

Recursion

Recursion in Python

Recursion in Python

Recursion in Python
Reversing string with recursion:
– Base case:

Recursion in Python
Reversing string with recursion:
– Recursive call:

Recursion in Python
Reversing string with recursion:
– Recursive call:

Recursion in Python
Reversing string with recursion:
– Recursive function:

Scope of Variables
When writing functions it is important to understand scope.

Scope refers to the area in a program where a variable is defined. Normally, a variable is defined after it has been assigned a value.

You cannot reference a variable until it has been assigned a value.

Scope of Variables
Local Scope
Enclosing Scope
Global Scope
Built-in Scope.

Scope of Variables
Local Scope
Local scope extends for the body of a function and refers to anything indented in the function definition.

Variables, including the parameter, that are defined in the body of a function are local to that function and cannot be accessed outside the function.

They are local variables.

Scope of Variables
Global Scope
A variable which is defined in the main body of a file is called a global variable.
It will be visible throughout the file, and also inside any file which imports that file.
Global variables can have unintended consequences because of their wide-ranging effects – that is why we should almost never use them. Only objects which are intended to be used globally, like functions and classes, should be put in the global namespace.

Scope of Variables
Built-in Scop
The final scope rule is the built-in scope. The built-in scope refers to those identifiers
that are built-in to Python. For instance, the len function can be used anywhere in a program
to find the length of a sequence.

Mutable Data and Functions
If function changes data of a variable that is passed to it as argument its called mutating.

Some variables are immutable and some mutable in functions.

Mutable Data and Functions

Mutable Data and Functions
When a function is called that mutates one or more of its parameters, the calling code will see that the data has been mutated.
The mutation is not somehow undone when the function returns. Since strings, ints, floats, and bools are all immutable, this never comesup when passing arguments of these types.

But, again, lists are mutable, and a function may mutate a list

Mutable Data and Functions

Mutable Data and Functions

Try mutating int

Mutable and Immutable objects
https://www.pythonforthelab.com/blog/mutable-and-immutable-objects/#mutable-and-immutable-data-types

Tuple datatype

Tuple datatype
In Python programming, a tuple is similar to a list. The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed.

Tuple datatype
-We generally use tuple for heterogeneous (different) datatypes and list for homogeneous (similar) datatypes.
-Since tuple are immutable, iterating through tuple is faster than with list. So there is a slight performance boost.
-Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.
-If you have data that doesn’t change, implementing it as tuple will guarantee that it remains write-protected.

Tuple datatype

Tuple datatype

Tuple datatype

Tuple datatype

Tuple datatype

Tuple datatype

Tuple datatype

Tuple datatype

Tuple datatype
https://www.programiz.com/python-programming/tuple

Excercises 7.
Create program that uses recursion to calculate sum of digits that are given to recursive function.
For example recursive(12345) would print 15

Create program that uses Tuple datatype as function return value. This function should ask user to enter his/her name and age and return it as Tuple object. In main program call this function in loop 5 times and print result

/docProps/thumbnail.jpeg