PowerPoint Presentation
Basics in programming
Lectures 5.
Order of operations
The reference type
Debugging
Exception handling
Loops
Python interpreter doesn’t read, or process, operators from left to right, like we do.
Instead, the Python interpreter ranks operators by importance and processes them in a specific sequence.
Basically rules that apply in mathematics apply in Python
Order of operations
Example:
1 + 2 * 3
And
(1 + 2 ) * 3
Order of operations
Order of operations
http://www.informit.com/articles/article.aspx?p=459269&seqNum=11
Order of operations
Use parentheses to make code more readable and to be sure that operations are performed like you plan them
Example
If true or false and true
Order of operations
Earlier we found that bytes in memory can be interpreted in different ways.
And that
Each value in Python is called an object. Each object is of a particular type.
There is one type in Python that is typically not seen, but nevertheless is important to understand. It is called the reference type.
The reference type
What’s a reference?
A reference is a pointer that points to an object. A pointer is the address of an object. Each object in memory is stored at a unique address and a reference is a pointer that points to an object.
The reference type
An assignment statement makes a reference point to an object. The general form of an assignment statement is:
The reference type
An identifier is any letters, digits, or underscores written without spaces between them. The identifier must begin with a letter or underscore. It cannot start with a digit.
The expression is any expression that when evaluated results in one of the datatypes in Python
The reference type
the variable R1_width (orange in the figure) is a reference that points at the integer object 10 colored green in the figure. This is what happens in memory in response to the assignment statement:
The reference type
The 0x264 is the reference value, written in hexadecimal, which is a pointer (i.e. the address) that points at the integer object 10
Typically you don’t see reference values in Python.
Instead, you see what a reference points to.
So if you type R1_width in the Python shell after executing the statement above, you won’t see 0x264 printed to the screen, you’ll see 10, the value that R1_width refers to.
The reference type
Python automatically garbage collects memory that is unused, and has no pointer pointing into it
For example
The reference type
The reference type
In Python its possible to debug your code.
In debug mode you can pause execution of the code on breakpoint.
Breakpoint can be set in IDLE
Debugging
Once breakpoint is reached excecution is paused and you are able check contents of variables etc…
Execution continues once you select Go or Step
Debugging
Sometimes things go wrong in a program and it is out of your control.
For instance, if the user does not enter the proper input an error may occur in your program.
Python includes exception handling so programmers can handle errors like this.
Exception handling
Use exception handling alway when there is possiblity that thing could go wrong.
Exception handling
Exception handling
Exception handling
the Exception is optional. That’s what the square brackets (i.e. [ ]) mean. They mean the exception is optional in this case. If the exception is omitted then any exception is caught.
A try-except block may monitor for any exception or just a certain exception.
There are many possible exceptions that might be caught.
For instance, a ValueError exception occurs when you try to convert an invalid value to an integer. A ZeroDivisionError exception occurs when you try to divide by zero.
Exception handling
Exception handling
A try-except block may monitor for any exception or just a certain exception.
There are many possible exceptions that might be caught.
For instance, a ValueError exception occurs when you try to convert an invalid value to an integer. A ZeroDivisionError exception occurs when you try to divide by zero.
Exception handling
Repeating a task in a programming language is often called iteration or a loop.
Much of what computers do are repetitive tasks, it is important to know how to repeat code and how to manipulate strings and lists.
As usually when doing repetative task, its data is either in a list or in a string
Repetitive Tasks a.k.a loops
For loop
To iterate over each element of a sequence you may write a for loop. A for loop looks like this:
In this code the is any variable name you choose. The variable will be assigned to the first element of the sequence and then the statements in the body of the for loop will be executed.
Then, the variable is assigned to the second element of the sequence and the body of the for loop is repeated. This continues until no elements are left in the sequence.
For loop
For loops are useful when you need to do something for every element of a sequence.
Example
myName = ”My name”
for c in myName:
print ”Char is: ” + c
Try to run this code, also try this in debugger
For loop
Example:
Make program that calculate sum of all numbers within given range
List
A list in Python is any sequence of values surrounded by square brackets (i.e. [ ]).
So for instance [0, 1, 2, 3] is a list.
So is [‘a’, 1, ‘b’, 4.2].
Lists are any sequence of values inside square brackets.
The items of the list can be of different types, although it is quite common for all values in a list to be of the same type.
A list is a sequence too. A list can be iterated over using a for loop just like a string.
List
Some operations, like:
-len
-concatenation
-slicing
These apply to lists too.
List and strings
You can use split() function to split string into list of words
List and strings
You can use in to see if item is in list
Mutable lists
In Python its possible to change on object in the list
Example, print list and change one object in it
The Accumulator Pattern
Sometimes we need to know how many times loop as iterated.
For this we can use accumulator pattern.
This pattern works by initializing a variable that keeps track of how much we have counted so far. Then we can write a for loop to go through a list of elements in a sequence and add each element’s value to the accumulator. The pattern looks like this:
The Accumulator Pattern
Example, program that calculates number of integers
Excercises 5.
Make program that is able to catch exception when from data type is used
Make a program prints all multiplications where number1 goes from 0 to 10 and number2 goes from 0 to 10
Make program that converts given name to ALL CAPITALS (with loop) and that replaces all A (or a) letters with # character
Deadline: 7.1.19
Memory management & garbage collection
https://realpython.com/python-memory-management/
http://patshaughnessy.net/2013/10/24/visualizing-garbage-collection-in-ruby-and-python
/docProps/thumbnail.jpeg