False
Comparison operators are used to compare two values or expressions. They return a Boolean value: True or False.
The common comparison operators in Python include:
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal toEqual to and Not Equal to
Greater Than and Less Than
try
and except
Error handling allows us to deal with errors during code execution without crashing the program.
It’s essential to make programs more robust and user-friendly.
Why Use try and except?
try
to test a block of code for errors.handle
the error if one occurs.try
and except
This is the basic syntax:
Example:
10.0
try
and except
This is the basic syntax:
Another Example:
An error occurred: division by zero
try
and except
try
fails.You can have multiple except
blocks to handle different types of errors.
We will learn how to store more than one value in a variable.
Lists are collections of items that are stored in a variable
The following is a list:
We can access items in a list in the following way:
We can access the second item in the following way:
Thus, notice that item enumeration starts from 0.
We can get the last item on a list in the following way:
We can get the second the last item from a list
Store the values ‘python’, ‘c’, and ‘java’ in a list. Print each value using its position in the list.
Store the values ‘python’, ‘c’, and ‘java’ in a list. Print each value using its position in the list.
Store the values ‘John’, ‘Anna’, and ‘Emma’ in a list. Using their position in the list, print a statement about each value.
Your statement could simply be, ‘“Value” is one of the students taking the class.’
Store the values ‘John’, ‘Anna’, and ‘Emma’ in a list. Using their position in the list, print a statement about each value.
Your statement could simply be, ‘“Value” is one of the students taking the class.’
A loop executes a given code section more than once.
A for
loop is composed of:
for
keywordin
keywordThis is the syntax in a for
loop.
Here is another example with strings:
for
loop to iterate through the vector and print each element.The output should look like the following:
1 1
2 4
3 9
4 16
5 25
for
loop to iterate through the vector and print each element.The output should look like the following:
1 1.0
2 1.4142135623730951
3 1.7320508075688772
4 2.0
5 2.23606797749979
list_students = ["John", "Anna", "Emma"]
for
loop to iterate through the vector and write:John is one of the students taking the class.
Anna is one of the students taking the class.
Emma is one of the students taking the class.
We can use the function break
to interrupt a loop.
When a condition is met, the loop will stop.
The break
function could also make a piece of code more efficient: our code runs quicker.
The use of break is even more apparent in the following case:
In this case, we were trying to find a number that could be divided by 3 and 7.
Rather than going through the whole range from 1 to 100, the loop stops when it encounters a number divisible by both 3 and 7.
When looping through a list, identifying the index of a current item may often be necessary.
The enumerate()
function takes the index, and then it loops through the list.
For example:
Python
Place: 0 Student: John
Place: 1 Student: Anna
Place: 2 Student: Emma
the index variable holds the index, which is an integer
if you want to print it in a string, the integer needs to be turned into a string
as before, the integer always starts at 0.
zip()
functionThe zip()
function takes multiple iterables (such as lists, tuples, or strings) and aggregates them into tuples.
A tuple - an immutable (unable to be changed), ordered collection of elements that can hold multiple items.
It creates an iterator of tuples, where the i-th tuple contains the i-th element from each iterable.
The first item in each passed iterator is paired together.
The second item in each passed iterator is paired together, etc.
zip()
functionHere is an example:
zip()
functionWe can also use the plus-equals operator.
The plus-equals operator +=
provides a convenient way to add a value to an existing variable.
This assigns the new value back to the same variable.
Python
Student 1: John obtained 89 in the exam.
Student 2: Anna obtained 51 in the exam.
Student 3: Emma obtained 70 in the exam.
zip()
function ExerciseSuppose you want to increase the grade for John, Anna, and Emma by 5%.
Use the previous code to update the grades by 5%.
zip()
function Exercise solutionSuppose you want to increase the grade for John, Anna, and Emma by 5%.
Use the previous code to update the grades by 5%.
Python
Student 1: John obtained 93.45 in the exam.
Student 2: Anna obtained 53.55 in the exam.
Student 3: Emma obtained 73.5 in the exam.
Comparison operators and conditional statements enable decision-making in programs.
Lists store multiple items, and loops process them efficiently.
try
and except
handle errors gracefully for robust code.
Tools like break
, enumerate
, and zip
enhance loop functionality.
Popescu (JCU): Lecture 4