0% found this document useful (0 votes)
2 views

Python Programming unit 3

This document covers Python programming concepts related to operators, expressions, and error handling. It explains the types of operators (unary and binary), various errors (syntax errors, logical errors, exceptions), and how to handle exceptions using try-except statements. Additionally, it discusses specific exceptions like NameError, IndexError, and ZeroDivisionError, along with best practices for improving user experience through effective error handling.

Uploaded by

bittugarg308
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Programming unit 3

This document covers Python programming concepts related to operators, expressions, and error handling. It explains the types of operators (unary and binary), various errors (syntax errors, logical errors, exceptions), and how to handle exceptions using try-except statements. Additionally, it discusses specific exceptions like NameError, IndexError, and ZeroDivisionError, along with best practices for improving user experience through effective error handling.

Uploaded by

bittugarg308
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

PYTHON

PROGRAMMING
Unit 3 Operators and Expressions
Contents
• Arithmetic
• Logical
• Relational Expressions
• Operator precedence and Associativity
• Comment Statements
• different kinds of error(Syntax, Runtime Exceptions, Logic Errors)
What are Operators
• An operator is a symbol, that performs an
operation, or action.
• An operator acts on some variables, called
operands to get a desired result.
• These operands, are usual to the left and right
side of the operator, while the operator is
always in the middle.
• If an operator, acts on a single variable, it is
called unary operator. if it acts on two
variables, it is called binary operator.
What are Operators
• An Example of unary operator is,
negative values.
• When we add a minus operator to a
number, the value of that number is
negated.
• An example of a binary operator is,
the plus sign, which added two values.
• And the result is denoted by another
binary operator, which is the equal to
sign.
Errors
• Errors are problems that occur in the program due to an illegal operation performed
by the user or by the fault of a programmer, which halts the normal flow of the
program. Errors are also termed bugs.
Errors
• There are, mainly two types of errors, in python programming. Let us learn about
both types of python errors.
• Syntax errors
• Logical Errors or Exceptions

• Whenever we do not write the proper syntax of the python programming language,
then the python interpreter, throws an error, known as a Syntax Error.
Errors
• On the other hand, Logical Errors are those errors, that cannot be caught during
compilation time. As we cannot check these errors during compile time, they are
named as “Exceptions”.
• Exceptions can cause some serious issues, so we should handle them effectively
Syntax Errors
• When writing a code, sometimes, you can forget a colon at the end of a line, or,
accidentally add one space too many, when indenting under a “for” statement, or
forget a parenthesis in a function, you will encounter a syntax error.
Syntax Errors
• This means that, the Python interpreter could
not figure out how to read your program.
• A syntax error is one of the most basic types
of error in programming. Whenever we do
not write the proper syntax, then python
throws an error.
• Let’s look at an example, to understand
syntax error better.
• As we can see in this example, the python
interpreter raised a syntax error, saying
“invalid syntax”.
• This is because, we have missed the colon,
after the if statement. so that's why the
python interpreter raised a syntax error.
Syntax Errors
• In the 2nd example, we are
trying to name a function called,
some_function.
• Here, we have not used a colon
at the end of the line, for the
function. Hence, python has
given us an error.
• Here, Python tells us that there is
a Syntax Error on line 1, and
even puts a little arrow in the
place where there is an issue. In
this case, the problem is that the
function definition is missing a
colon at the end.
Syntax Errors
• Also, in this example, the function has another
issue with its syntax.
• Once we fix the problem with the colon, we see
that there is also an Indentation Error, which
means that the lines in the function definition,
do not all have the same indentation.
• Here we see an indentation error, and we can
fix this by aligned the return keyword, with the
print function.
• Both Syntax Errors and Indentation Errors,
indicate a problem with the syntax of a
program.
• But an Indentation Error, is more specific, as it
always means that there is a problem with how
the code is indented.
Exceptions
• Exceptions are raised, when the program encounters an error during its execution.
• The difference between errors and exception is, Errors are the problems in a
program, due to which the program will stop the execution.
• On the other hand, exceptions are raised when some logical issues occur, which
changes the normal flow of the program.
Exceptions
• On the other hand, exceptions are
raised when some logical issues occur,
which changes the normal flow of the
program.
• There are many different types of
exceptions, and they are all raised in
particular situations. Some of the
exceptions that you will most likely
come across are shown on the right.
Exceptions
• You must have noticed, a general pattern
in exception error messages.
• Let's break down their general structure,
piece by piece.
• First, we find this line, called a traceback.
A traceback is basically a list, detailing the
function calls that were made before the
exception was raised.
• The traceback helps us, during the
debugging process, because you can
analyse the sequence of function calls, that
resulted in the exception.
• Then, we see this line, with the path to the
file, and the code line that raised the
exception. In this case, the path was the
Python shell, pie shell 0, since the
example was executed directly on
PyCharm IDLE.
Exceptions
• Finally, we see a descriptive message,
detailing the type of exception, and
providing additional information to
help us debug the code.
• Now, let’s look at some important
exceptions in detail.
Exceptions- Variable Name Error
• Another very common type of error, is
called a ‘Name Error’. A name error,
occurs when you try to use a variable,
that does not exist.
• A very common reasons why you
might have an undefined variable, is
that you meant to use a string, or a
text value, but forgot to put quotes
around it.
Exceptions- Variable Name Error
• Another possibility for the name error is, that you made a typo when you were
writing your code.
Exceptions- Index Errors
• Index errors, are errors that occur with storage containers, like lists and tuples, and
the items within them.
• When we try to access an item in a list, we use their index numbers.
Exceptions- Index Errors
Exceptions- Type Errors
• A type error is raised, when an operation or function is applied to an object, of an
inappropriate type.
• For example, here is code with two variables, containing the tuple data type.
Exceptions – Key Errors
• A key error, is raised when you try to access, the value of a key, that doesn't exist in
a dictionary.
• For example, students = {"Krish": 15, "Jay": 30}
Exception Handling
• By handling exceptions, you can provide an alternative flow of code execution, to
avoid crashing your program unexpectedly.
• Imagine what would happen, if a user who is working with your program, enters an
invalid input.
• This would raise an exception because, an invalid operation was performed during
the process.
• If your program doesn't handle this correctly, it will crash suddenly, and the user
will have a bad experience with your program.
Exception Handling
• But if you do handle the exception, you
will be able to provide an alternative, to
improve the experience of the user.
• Perhaps you can display a descriptive
message, asking the user to enter a valid
input, or you could provide a default value
for the input.
• Depending on the program, you can
choose what to do when this happens, and
this is the benefit of error handling.
• When we handle an exception, we are
telling the program what to do if the
exception is raised. In that case, the
"alternative" flow of execution will come
to the rescue.
Exception Handling
• If no exceptions are raised, the code will run as expected.
• The try ... except Statement
• Now that you know what exceptions are, and why you should we handle them, let’s
start diving into the built-in tools, that the Python languages offers for this purpose.
• First, we have the most basic statement. “try” and “except”.
Exception Handling
• Here we have a small program, that
asks the user to enter the name of a
student, to display his or her age.
• Notice here, how we are not validating
the users input, by giving them
instructions or comments to enter a
valid input data.
Exception Handling
• In this case, the user might also
enter invalid values, such as, the
names that are not in the
dictionary. This would cause an
exception error, and the program
would crash with a Key Error.
Here, we have entered a key, that
does not exist in the dictionary.
Hence the key error.
Exception Handling
• We can handle this exception
error, using the “try and
except” statement.
• In our example, lets add the
“try and except” statement,
within the function,
print_student_age.
• When the function is called,
the “try” clause will run. If no
exceptions are raised, the
program will run as expected.
• But if an exception is raised in
the “try” clause, the flow of
execution will immediately
jump to the “except” clause, to
handle the exception.
Catching Specific Exceptions
• Since not all types of exceptions are
handled in the same way, we can
specify which exceptions we would
like to handle with this syntax.
Catching Specific Exceptions
• In this example, we are dividing two
integers, using the numerator and
denominator, as the user input.
• Here we get the “Zero Division Error”
exception. This is caused because, we
have entered “zero”, as the denominator.
• A zero-division error occurs in python,
because In mathematics, division by 0 is
undefined.
• Now, let’s handle this code, by specifying
the type of error to handle.
• So here, we have mentioned the zero-
division error, in the except statement.
Catching Specific Exceptions
Catching Specific Exceptions
Multiple Except Clauses
• In python, a try statement may have more than one except clause, to specify
handlers for different exceptions.
• In this example, we have two except clauses. One of them handles Zero Division
Error, and the other one handles a Value Error.
• These two types of exceptions can be raised in this “try” block of code.
Multiple Except Clauses
Multiple Except Clauses
• Here a value error occurs because, we have 1st defined the input values are integers.
• And since we have entered a float value in the output, python will give us a value
error.
• To handle this, we have added a second except statement, to handle the value error.
Multiple Except Clauses
Errors- Practice Questions
• What are exceptions in Python?
• What is the try-except statement in Python?
• What is the syntax of try-except statement in Python?
• What is the purpose of finally block in Python?
• How do you raise an exception in Python?
• What is the purpose of else block in a try-except statement?
• What is the difference between a SyntaxError and a NameError in Python?
• How do you handle multiple exceptions in try-except statement?
• What is the purpose of traceback in Python?
• What is the difference between a try-except and try-finally statement in Python?

You might also like