0% found this document useful (0 votes)
10 views22 pages

Python Operators and Expressions Guide

This document covers Python operators and expressions, categorizing them into arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It explains the role of operators in performing computations on operands and highlights the importance of understanding error messages encountered during programming. Additionally, it emphasizes the distinction between object identity and equality in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views22 pages

Python Operators and Expressions Guide

This document covers Python operators and expressions, categorizing them into arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It explains the role of operators in performing computations on operands and highlights the importance of understanding error messages encountered during programming. Additionally, it emphasizes the distinction between object identity and equality in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MODULE 2

Python Programming Fundamentals


SUBTOPIC 2
PYTHON OPERATORS
AND EXPRESSION
• Familiarize with operators and expression
• Know error messages
• Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
• Arithmetic operators are
used with numeric values
to perform common
mathematical operations
• Assignment operators
are used to assign values
to variables:
• In Python, operators are special symbols that designate that
some sort of computation should be performed. The values
that an operator acts on are called operands. Operators are
used to perform operations on variables and values.
Example:
In this case, the + operator adds the
operands a and b together. An operand can be
either a literal value or a variable that references
an object.
A sequence of operands and operators, like a + b - 5, is called an
expression. Python supports many operators for combining data
objects into expressions.
Example:
• Comparison operators
are used to compare two
values
• Logical operators are used to combine conditional
statements:
• Example
• Identity operators are used to compare the objects, not
if they are equal, but if they are actually the same
object, with the same memory location:
Python provides two operators, is and is not that determine whether the given
operands have the same identity—that is, refer to the same object. This is not the
same thing as equality, which means the two operands refer to objects that
contain the same data but are not necessarily the same object.
• Membership operators are used to test if a sequence is
presented in an object:
• Example
• Bitwise operators are used to compare (binary)
numbers:
Sample Program:

Output:
• Every programmer encounters errors, both those who are
just beginning, and those who have been programming for
years. Encountering errors and exceptions can be very
frustrating at times, and can make coding feel like a
hopeless endeavor. However, understanding what the
different types of errors are and when you are likely to
encounter them can help a lot. Once you know why you get
certain types of errors, they become much easier to fix.
• Syntax Error. A Python couldn’t figure out how to read your
program
• Irv Kalb (2018). Learn to Program in Python 3. Apress
• Eric Matthes (2016). Python Crash Course. No Starch
Press, [Link] and run Python script
• Fletcher Heisler, David Amos, Dan Bader, Joanna
Jablonski (2012-2020). A Practical Introduction to
Python 3 (4th Edition). Real Python
• [Link]

You might also like