0% found this document useful (0 votes)
25 views5 pages

Python Operators and Number Systems

This document covers various types of operators in Python, including arithmetic, assignment, relational, logical, and unary operators, along with their uses. It also discusses number system conversions between decimal, binary, octal, and hexadecimal, detailing methods for converting and using built-in Python functions for these conversions. Additionally, it explains bitwise operators, their types, and how they operate on binary representations of integers.

Uploaded by

ajk952626
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)
25 views5 pages

Python Operators and Number Systems

This document covers various types of operators in Python, including arithmetic, assignment, relational, logical, and unary operators, along with their uses. It also discusses number system conversions between decimal, binary, octal, and hexadecimal, detailing methods for converting and using built-in Python functions for these conversions. Additionally, it explains bitwise operators, their types, and how they operate on binary representations of integers.

Uploaded by

ajk952626
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

operators

In this lecture we will learn:


- Different types of operators in Python
- Use of different operators
- Operation performed between different operators
- Logical operators and their uses
#1
- Types of operators in Python:
1. Arithmetic operators
2. Assignment operators
3. Relational operators
4. Logical operators
5. Unary operators
- Arithmetic operators are used to perform mathematical operations like addition, subtraction,
multiplication, division, etc.
- Assignment operation is performed with the help of equal to(=), in which we can assign a value to the
variable.
- We can also do the assignment by short-hand which means performing an arithmetic operation and
then assigning the value both at the same time.
- We can also assign the values in one line for two variables.
a,b = 5,6
- Unary operator takes a single operand in an expression or a statement.
- Comparison can be performed with the help of relational operators.
- Comparison operators return a boolean value or True or False.

#2
- If you want to combine two or more conditions and then check the relation between them, then
logical operators are used.
- Logical operators include And, Or and Not.
- Logical operators follow the truth table for And, Or, and Not operators.
- And the operator returns True when both conditions are true otherwise returns False.
Or the operator returns True when any of the conditions or both the conditions are True otherwise it
returns False.
Not the operator reverses the value of the output.

telusko python 2 Page 1


Number System Conversion
In this lecture we are discussing about Number System and its conversion:

In programming, the number system is a way of representing numbers using different bases.
The most commonly used number systems in programming are the decimal (base 10),
binary (base 2), octal (base 8), and hexadecimal (base 16) systems.

a)Decimal system: This is the number system we use in our everyday lives. It uses 10 digits (0-9)
to represent numbers.

b)Binary system: This system uses only two digits (0 and 1) to represent numbers. It is commonly
used in computer systems because digital devices work with binary signals.

c)Octal system: This system uses 8 digits (0-7) to represent numbers. It's often used in computer programming
because it's a compact way to represent binary numbers.

d)Hexadecimal system: This system uses 16 digits (0-9 and A-F) to represent numbers. It's commonly used in
computer programming
and web development to represent colors and memory addresses.

Converting different number systems:

Decimal to binary: Divide the decimal number by 2 and write down the remainder. Keep dividing by 2 and writing
down the remainders until
you get to 0 at quotient. Then, read the remainders in reverse order to get the binary number.
e.g 51
2|51 |1
2|25 |1
2|12 |0
2|6 |0
2|3 |1
2|1 |1
|0 |1
reverse order: 110011
Binary to decimal: Multiply each digit in the binary number by the corresponding power of 2
(starting with 2^0 on the right). Add up the results to get the decimal number.

e.g 11011
2^4 2^3 2^2 2^1 2^0
1*16 1*8 0*4 1*2 1*1
16+8+0+2+1=27
It is Same for octal and hexadecimal(Not in this lecture but for your knowledge)

Decimal to octal: Divide the decimal number by 8 and write down the remainder. Keep dividing by 8 and
writing down the remainders until you get to 0. Then, read the remainders in reverse order to get the
octal number.
e.g
51
8|51 |3
8|6 |6
8|0 |0
reverse order: 360
Octal to decimal: Multiply each digit in the octal number by the corresponding power of 8 (starting with 8^0 on
the right).
Add up the results to get the decimal number.
e.g

telusko python 2 Page 2


e.g
360
8^2 8^1 8^0
3*64 6*8 0*1
192+48+0=240

Decimal to hexadecimal: Divide the decimal number by 16 and write down the remainder. If the remainder is
greater than 9, replace it with the corresponding letter (A-F). Keep dividing by 16 and writing down the
remainders
until you get to 0. Then, read the remainders in reverse order to get the hexadecimal number.
e.g
51
16|51 |3
16|3 |3
16|0 |0
reverse order: 33

Hexadecimal to decimal: Multiply each digit in the hexadecimal number by the corresponding power of 16
(starting with 16^0 on the right).
If a digit is a letter, replace it with the corresponding value (A=10, B=11, etc.). Add up the results to get the
decimal number.
e.g
33
16^1 16^0
3*16 3*1
48+3=51

Only for Interest (Not in this lecture)


Converting from octal to binary and binary to octal can be done using a simple method.
Conversion from Octal to Binary:

To convert an octal number to binary, we can simply replace each digit in the octal number with its equivalent
three-digit binary number.
Here are the octal-to-binary conversions for each octal digit:
0 = 000
1 = 001
2 = 010
3 = 011
4 = 100
5 = 101
6 = 110
7 = 111
For example, to convert the octal number 725 to binary:
7 = 111
2 = 010
5 = 101
So, 725 in octal is equivalent to 111010101 in binary.
Conversion from Binary to Octal:
To convert a binary number to octal, you can group the binary digits into sets of three (starting from the right)
and replace each set with its equivalent octal digit. Here are the binary-to-octal conversions for each set of three
binary digits:
000 = 0
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
110 = 6

telusko python 2 Page 3


110 = 6
111 = 7
For example, to convert the binary number 101011011 to octal:
101 011 011
|- - - ||- - - | |- - - |
5 3 3
we get 533 in octal.

Use of method in python to direclty convert number system:


Decimal to binary: bin()
e.g
bin(51)
'0b110011' # 0b represent binary formate
binary to decimal: int()
e.g
int('0b110011',2)
51

Decimal to octal: oct()


e.g
oct(51)
'0o63' # 0o represent octal formate
octal to decimal: int()
e.g
int('0o63',8)
51

Decimal to hexadecimal: hex()


e.g
hex(51)
'0x33' # 0x represent hexadecimal formate
hexadecimal to decimal: int()
e.g
int('0x33',16)
51

telusko python 2 Page 4


BitWise Operators
In this lecture we will learn:
- What are Bitwise operators in Python?
- Different types of bitwise operators
- How operations performed on bits
- Implementation of bitwise operations

#1
- In bitwise operations, the integers are first converted into binary and then operations are
performed by bitwise operators on each bit or corresponding pair of bits.
- The result is returned in decimal format by the bitwise operators.
- There are six types of binary operators:-
1. Complement (~)
2. And (&)
3. Or (|)
4. XOR (^)
5. Left Shift
6. Right Shift

#2
- Complement operator is also known as tilde(~) operator.
- Complement simply do the reverse of binary form. If a bit is 0 then it makes it 1 and vice-
versa.
- Reverse of each bit of a number returns the 1's complement.
- We can store only positive numbers in our system and that's why we find the 2's
complement of each negative number.
2's complement = 1's complement +1

#3
-AND operator will return true if both the conditions are true while the OR operator will
return true if at least one condition is true.

#4
- XOR operator will return 1 or true when there is an odd number of 1 bit present in
operations and if there is an even number of 1, then it will return 0.

#5
- Leftshift operator shift bits on the left-hand side. The right-shift operator shifts bits on the
right-hand side.
- Leftshift add the bits while the right-side removes the bits.

>>> ~12
-13
>>> 12 & 13
12
>>> 12 | 13
13
>>> 25 & 30
24
>>> 12 ^ 1
13
>>> 12 ^ 13
1
>>> 25 ^ 30
7
>>>

telusko python 2 Page 5

Common questions

Powered by AI

The hexadecimal number system is significant in programming because it provides a more human-readable way to express binary numbers. It uses 16 symbols (0-9 and A-F) to represent values, which condenses long binary numbers, thus simplifying representation. In web development, hexadecimal is often used to represent colors, where a six-digit code specifies the intensity of red, green, and blue components. Additionally, hexadecimal is employed in memory addresses where memory locations are often vast, and the compact form of hexadecimal offers ease in handling .

The Unary operator in Python differs from other operators in that it operates on a single operand. It typically changes the sign of a numeric value (as in the case of the unary minus for negation) or complements the bits of a binary number (as with the bitwise NOT '~'). Other operators, such as binary, arithmetic, or logical operators, require two operands. The Unary operator is unique because it directly manipulates a single variable or value's sign or bit state without comparing or performing arithmetic between two separate values .

Assignment operators in Python are particularly useful when both assigning and updating the value of a variable. They allow for concise code writing by combining arithmetic operations with assignment within a single statement, such as '+=' for adding and assigning in one step. This reduces redundancy and can improve readability of the code, especially when performing repetitive tasks sequentially, such as summing elements in a loop or accumulating values in a variable over iterations .

Manually converting a decimal number to octal involves repeatedly dividing the number by 8, recording the remainder at each step, and then reading these remainders in reverse order. For example, converting 51 to octal involves dividing by 8 to get remainders of 3, 6, until the quotient is 0, resulting in the octal number 63. Python simplifies this with the 'oct()' function, which returns a string prefixed with '0o' to denote the octal number, e.g., 'oct(51)' results in '0o63', thus automating the conversion process and minimizing errors .

To convert a binary number to octal manually, you group the binary digits into sets of three starting from the right. Replace each group with the corresponding octal digit. For instance, converting the binary number 101011011 involves grouping it into 101, 011, 011, which translates to the octal number 533. Python automates this process using sequence operations where binary can be converted to an integer using the int() function with base 2, and that integer can be formatted into an octal string with the 'oct()' function. For example, 'oct(int("101011011", 2))' directly yields the octal equivalent .

Manually converting a decimal number to binary involves dividing the number by 2 and recording the remainder for each division until the quotient is zero. The binary number is then obtained by reading the remainders in reverse order. For example, to convert 51 to binary: 51 divided by 2 gives a remainder of 1, then 25 divided by 2 gives 1, and so forth, resulting in the binary number 110011. In Python, this conversion can be performed using the built-in function 'bin()', which returns the binary string directly, for example, 'bin(51)' results in '0b110011'. Python offers a more efficient and error-free method compared to manual conversion .

Logical operators in Python are used to combine multiple conditions and assess their relationships. These include 'and', 'or', and 'not'. The 'and' operator returns True if both conditions are true, otherwise, it returns False. The 'or' operator returns True if at least one condition is true. The 'not' operator reverses the truth value of a condition. Therefore, logical operators help in decision-making processes by operating based on the logical truth table .

Bitwise operations are significant in low-level programming because they directly manipulate bits, offering high efficiency and speed compared to operations on larger data types. They are pivotal in operations requiring precision control over data, such as in embedded systems, algorithms involving encryption, or error detection. By operating at the binary level, they reduce the need for complex computations, leading to faster execution times and optimal use of computing resources. Thus, they are a fundamental tool for performance-critical applications .

Bitwise operators in Python perform operations on the binary representations of integers at the bit level. They include operators such as Complement (~), And (&), Or (|), XOR (^), Left Shift (<<), and Right Shift (>>). Unlike logical operators, which evaluate entire expressions and return True or False based on the evaluation of the conditions, bitwise operators manipulate bits within the number itself. For example, the And (&) operator returns a number with bits set to 1 only if corresponding bits of both operands are 1, whereas the logical 'and' is concerned with boolean values of expressions .

Arithmetic operators in Python are used to perform mathematical operations like addition, subtraction, multiplication, and division. These operators work on numerical values and yield numeric results. Relational operators, on the other hand, compare values or expressions and return a boolean (True or False) indicating the result of the comparison—examples include '==', '!=', '<', and '>'. Thus, while arithmetic operators manipulate numeric data, relational operators evaluate relationships between data .

You might also like