telusko python 3
telusko python 3
#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.
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.
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
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
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
#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
>>>