0% found this document useful (0 votes)
5 views17 pages

Chapter 2 Part II

The document provides an overview of Python programming language operators, including arithmetic, comparison, assignment, logical, and bitwise operators. It explains their functions, examples, and operator precedence. Additionally, it includes exercises to practice using these operators in Python.

Uploaded by

mametalha
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)
5 views17 pages

Chapter 2 Part II

The document provides an overview of Python programming language operators, including arithmetic, comparison, assignment, logical, and bitwise operators. It explains their functions, examples, and operator precedence. Additionally, it includes exercises to practice using these operators in Python.

Uploaded by

mametalha
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

COMPUTER PROGRAMMING

PYTHON PROGRAMMING LANGUAGE

Compiled By: Kahsay N.


Contents
• Type of Python Operators
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Python Operator Precedence
Python Operator
• Python operators are the constructs which can manipulate the value of operands.
• These are symbols used for the purpose of logical, arithmetic and various other
operations.
• Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called
operator.
Python Arithmetic Operators
• Python arithmetic operators are used to perform mathematical operations on
numerical values. These operations are Addition, Subtraction, Multiplication,
Division, Modulus, Exponents and Floor Division.
Operator Name Example
+ Addition 10 + 20 = 30
- Subtraction 20 – 10 = 10
* Multiplication 10 * 20 = 200
/ Division 20 / 10 = 2
% Modulus 22 % 10 = 2
** Exponent 4**2 = 16
// Floor Division 9//2 = 4
Python Arithmetic Operators
• Floor division (//) is an operator that returns the largest integer less than or equal to
the quotient of two operands. For example, 7 // 2 evaluates to 3, because 2 can go into
7 three times with a remainder of 1.

• Ceiling division, on the other hand, returns the smallest integer greater than or equal
to the quotient of two operands. It is denoted by the symbol "⌈ ⌉". For example, ⌈7/2⌉
evaluates to 4, because the quotient of 7 divided by 2 is 3.5, and the smallest integer
greater than or equal to 3.5 is 4.

• In Python, you can use the math module to perform ceiling division using the ceil()
function. For example, [Link] (7/2) evaluates to 4.
Python Comparison Operators
• Python comparison operators compare the values on either sides of them and decide
the relation among them. They are also called relational operators. These operators are
equal, not equal, greater than, less than, greater than or equal to and less than or equal
to. Operator Name Example
== Equal 4 == 5 is not true.
!= Not Equal 4 != 5 is true.
> Greater Than 4 > 5 is not true.
< Less Than 4 < 5 is true.
>= Greater than or Equal to 4 >= 5 is not true.
<= Less than or Equal to 4 <= 5 is true.
Python Assignment Operators
• Python assignment operators are used to assign values to variables.
Operator Name Example
= Assignment Operator a = 10
+= Addition Assignment a += 5 (Same as a = a + 5)

-= Subtraction Assignment a -= 5 (Same as a = a - 5)

*= Multiplication Assignment a *= 5 (Same as a = a * 5)

/= Division Assignment a /= 5 (Same as a = a / 5)

%= Remainder Assignment a %= 5 (Same as a = a % 5)

**= Exponent Assignment a **= 2 (Same as a = a ** 2)

//= Floor Division Assignment a //= 3 (Same as a = a // 3)


Python Bitwise Operators
• Bitwise operator works on bits and performs bit by bit operation.
Operator Name Example
& BinaryAND Sets each bit to 1 if both bits are 1

| Binary OR Sets each bit to 1 if one of two bits is 1


^ Binary XOR Sets each bit to 1 if only one of two bits is 1

~ Binary Ones Inverts all the bits


Complement

<< Binary Left Shift Shift left by pushing zeros in from the right and let the
leftmost bits fall off
>> Binary Right Shift Shift right by pushing copies of the leftmost bit in from the
left, and let the rightmost bits fall off
Python Bitwise Operators cont..
• The one's complement of a binary number is a bitwise operation that flips all the bits
of the number, i.e., it changes every 0 to 1 and every 1 to 0.

• For example, the one's complement of the binary number 010110 is 101001.
• To compute the one's complement of a binary number, you can use the bitwise NOT
operator (~) in most programming languages.
x = 0b010110 # binary literal for 010110
ones_comp = ~x # bitwise NOT of x
print(bin(ones_comp)) # prints -0b101001
(two's complement representation)
Python Bitwise Operators cont..
• the left shift operator (<<) is a bitwise operation that shifts the binary representation
of a number to the left by a specified number of bits.

• The syntax for the left shift operator is as follows: result = value << n_bits

• For example, shifting the binary number 0b1010 (which is equivalent to the decimal
number 10) to the left by 2 bits gives the binary number 0b101000 (which is

equivalent to the decimal number 40).


x = 0b1010 # binary literal for 1010
n_bits = 2
result = x << n_bits # left shift x by 2 bits
print(bin(result)) # prints 0b101000
Python Bitwise Operators cont..
• the right shift operator (>>) is a bitwise operation that shifts the binary representation
of a number to the right by a specified number of bits.

• The syntax for the left shift operator is as follows: result = value >> n_bits

• Note that right shifting a binary number by n_bits is equivalent to dividing the
decimal value of the number by 2 to the power of n_bits and rounding down to the
nearest integer.

• For example, right shifting the binary number 0b101000 by 2 bits is equivalent to
dividing the decimal value of the number (40) by 2 to the power of 2, which gives 10.
Python Logical Operators
• There are following logical operators supported by Python language.
Operator Description Example

and Logical AND If both the operands are true then condition (a and b) is true.
becomes true.

or Logical OR If any of the two operands are non-zero (a or b) is true.


then condition becomes true.

not Logical NOT Used to reverse the logical state of its Not(a and b) is false.
operand.
Python Operators Precedence
• The following table lists all operators from highest precedence to lowest.
[Link]. Operator & Description 6 &
1 ** Bitwise 'AND'
Exponentiation (raise to the power)
7 ^|
2 ~+- Bitwise exclusive `OR' and regular `OR'
Complement, unary plus and minus (method names for
the last two are +@ and -@)
8 <= < > >=
3 * / % // Comparison operators
Multiply, divide, modulo and floor division
9 <> == !=
4 +- Equality operators
Addition and subtraction
10 = %= /= //= -= += *= **=
5 >> <<
Right and left bitwise shift Assignment operators
Python operator Exercise
1. Modify your temperature conversion program to convert temperature from Celsius
to Fahrenheit and vice versa based on the user's preference.
2. Write a program to add, subtract, multiply, divide, and modulus two numbers
entered by the user.
3. Find the square and cube of a number using the exponent operator (**).
4. Swap two numbers using arithmetic operators (no third variable).
5. Take two numbers and display the quotient and remainder.
6. Evaluate the expression a + b * c - d / e using input values and explain operator
precedence.
Python operator Exercise
1. Swap two numbers using arithmetic operators (no third and with third var).
2. Take input from user, multiply it by 2 using assignment operator and print result.
3. Print the binary representation of two numbers using bin() and apply &, |, and ^
operators.
4. Left shift a number by 2 bits and right shift it by 2 bits, print both results.
5. Use bitwise NOT (~) operator on a number and explain the output.
6. Apply ^ (XOR) on two numbers and explain its behavior with examples.
7. Count number of set bits in a number using bitwise operations.
8. Start with x = 10. Increment x by 5, then decrement by 3, and print the final value
using += and -=.

You might also like