0% found this document useful (0 votes)
9 views10 pages

Python Basic Operators Explained

This document provides an overview of basic operators in Python, including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It outlines objectives for understanding these operators and includes learning activities such as reading, journaling, and group discussions. Additionally, it lists various resources and revision questions to reinforce the concepts discussed.

Uploaded by

Ianbrown Wekesa
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)
9 views10 pages

Python Basic Operators Explained

This document provides an overview of basic operators in Python, including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It outlines objectives for understanding these operators and includes learning activities such as reading, journaling, and group discussions. Additionally, it lists various resources and revision questions to reinforce the concepts discussed.

Uploaded by

Ianbrown Wekesa
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

TOPIC 5 : Basic Operators in Python

Introduction.
Operators are the constructs which can manipulate the value of operands. Consider the expression
4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator. Operators are used to perform
operations on variables and values. Operators are used to perform operations on values and
variables. Operators can manipulate individual items and returns a result. The data items are
referred as operands or arguments. Operators are either represented by keywords or special
characters.
Objectives
Objectives by the end of this topic you should be able to describe:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators

Learning activities
Learning Activity 5.1: Reading
Read further on Bitwise operators.
Learning Activity 5.2: Journal
Summarize the usage of operators from python documentation.
Learning Activity 5.3: Discussion
In groups of three write programs to demonstrate usage od python operators.
Assessment

Topic resources
1. The Python Tutorial¶. (n.d.). Retrieved from [Link]
2. Mueller, J. P. (n.d.). Beginning Programming with Python For Dummies. S.l.: For
Dummies.
3. (n.d.). Python 3.7.4 documentation. Retrieved from [Link]
4. (n.d.). Git Handbook. Retrieved from [Link]
handbook/
5. Shaw, Z. (2017). Learn Python 3 the hard way: a very simple introduction to the
terrifyingly beautiful world of computers and code. Boston: Addison-Wesley.
6. Bader, D. (2018). Python tricks: the book. Vancouver, BC: Dan Bader.
7. Downey, A. B. (2015). Think Python. Sebastopol: OReilly.
8. Ramalho, L. (2016). Fluent Python:Beijing: OReilly.

URL Links
[Link]
[Link]
[Link]

TOPIC 5 NOTES
1. Arithmetic operators: Arithmetic operators are used to perform mathematical operations
like addition, subtraction, multiplication and division.
Output:
13
5
36
2.25
2
1

2. Relational Operators: Relational operators compares the values. It either


returns True or False according to the condition.
Output:
False
True
False
True
False
True
3. Logical operators: Logical operators perform Logical AND, Logical OR and Logical
NOT operations.

Output:
False
True
False
Bitwise operators: Bitwise operators acts on bits and performs bit by bit operation.
Output:
0
14
-11
14
2
40
4. Assignment operators: Assignment operators are used to assign values to the variables.
5. Special operators: There are some special type of operators like-
• Identity operators-
is and is not are the identity operators both are used to check if two values are located on
the same part of the memory. Two variables that are equal does not imply that they are
identical.

is True if the operands are identical


is not True if the operands are not identical
Output:
False
True
False
• Membership operators-
in and not in are the membership operators; used to test whether a value or variable is in a
sequence.

in True if value is found in the sequence


not in True if value is not found in the sequence

Output:
True
True
False
True
False
Revision questions
1. What is the output of the following code : print(9//2)
2. What is the answer of this expression, 22 % 3 is?
3. (T/F) x = 8, print(x > 3 or x < 20)
4. Which function overloads the >> operator?
(A) more()
(B) gt()
(C) ge()
(D) None of the above

5. Which operator is overloaded by the or() function?


(A) ||
(B) |
(C) //
(D) /
6. What is the output of the following program :
i=0
while i < 3:
print i
i++
print i+1
(A) 0 2 1 3 2 4
(B) 0 1 2 3 4 5
(C) Error
(D) 1 0 2 4 3 5

Common questions

Powered by AI

Python's special operators, such as identity (is, is not) and membership (in, not in), facilitate complex data manipulation by providing efficient means to test object identities and check membership within sequences respectively . These operators enable developers to perform advanced manipulations like de-duplication of data and validation of object references more intuitively and efficiently, improving both performance and readability of the code in data-intensive applications .

Assignment operators in Python not only assign values to variables but also facilitate expressions like incremental addition (e.g., +=, -=). This combines assignment with a mathematical operation, offering a concise and readable way to perform common value transformations directly on the variable . This approach contrasts with languages that require separate statements for calculation and assignment, effectively reducing verbosity and potential for errors in reassignment logic .

Membership operators in Python allow for direct testing of whether an element is present within a collection, such as a list, tuple, or dictionary, which can greatly streamline operations . Instead of using loops to check element presence, a process that can be computationally expensive, membership operators (in, not in) provide an optimized, built-in way to perform these checks, improving both code clarity and performance .

Logical operators (AND, OR, NOT) are used to combine multiple conditions, whereas relational operators (such as <, >, <=, >=, ==, !=) are used to compare two values or expressions . In decision-making constructs like if statements, relational operators define the conditions to be evaluated. Logical operators allow complex condition chains by combining these conditions, thereby enabling more nuanced control flow based on multiple criteria being satisfied .

Understanding Python operators is crucial for beginner programmers as they form the building blocks of programming logic and operations. Mastery of operators, including arithmetic, relational, logical, and bitwise, is essential for performing calculations, making decisions, and manipulating data efficiently. This foundation aids in learning more advanced programming concepts and writing effective, optimized code, thereby promoting better problem-solving skills and programming fluency .

Bitwise operators can make code less readable and more challenging to maintain because they operate at a lower level than most other operations, manipulating binary bits directly. While they can offer performance benefits and are necessary in certain applications like encryption and hardware interfacing, they can obscure the logic for developers who are not familiar with bit-level operations . Thus, they must be documented and used judiciously, often paired with comments that describe their function and purpose within the code .

Bitwise operators are favored in cryptography and graphics programming due to their ability to perform operations on the binary level directly . This allows for high performance and precision, essential for encryption algorithms that rely on manipulating bits for security and encoding tasks. In graphics, bitwise operations efficiently handle pixel data and color transformations on the binary level, ensuring faster processing speeds essential for rendering and real-time graphics applications .

Comparison operators (such as ==, !=, <, >, <=, >=) form the foundational checks within conditional statements, providing the criteria that determine the logical branch execution in decision flow. They enable the formulation of expressions that resolve to boolean values, thus controlling the execution of specific branches of code based on defined logical conditions. This capability is integral to the creation of dynamic programs that respond appropriately to varying inputs and states, directly influencing the decision-making process .

Identity operators (is, is not) are used to check if two variables point to the same object in memory, which can optimize certain comparisons and ensure object identity over object equivalence . Membership operators (in, not in) allow quick checks of sequence membership, which is critical in efficiently handling large datasets by preventing the need to manually iterate through elements . These operators streamline data handling processes in Python by providing syntactically intuitive and efficient operations for common data checks.

Arithmetic operators are used for performing basic mathematical operations such as addition, subtraction, multiplication, and division on numbers. In contrast, bitwise operators perform operations on the binary representations of integers directly, manipulating bits within the integer . Due to their low-level nature, bitwise operations can be more computationally efficient for certain calculations, especially when working directly with binary data or performing operations related to computer graphics and low-level data processing .

You might also like