Monday, 27 May 2019

Decimal to Binary conversion

Decimal number (Ex: 10) to Binary Conversion:
















1 byte is 8 bits so remaining are filled with 0’s
a=10 à0000 1010

b=11













b=11->0000 1011

So
a=10->0000 1010


b=11->0000 1011

Steps:
Divide this number by 2 and ignore the remainder, keep repeating this process till we get 1

(1.) 10 divide by 2 which gives 5
     5 <--10  <---

(2.) Divide by 2 we get 2, ignore remainder 1
   i.e 2<-- 5 <--10

(3.) 2 divide by 2 we get 1, ignore remainder 0
   i.e 1<-- 2<-- 5<-- 10

(4.) Now we are going to write 1 below any of the odd number
   and 0 below any of the even number.
   i.e write 1 for odd numbers; 0 for even number
          1  2  5  10
   i.e   1  0  1    0
so 10=1010 (In binary)
Unicode & Character Encodings in Python: A Painless Guide

Operators

What is Operator?

An operator is a symbol which is used to perform an operation on operands (values and variables).
Operators are either represented by keywords or special characters. For example, for identity operators we use the keyword "is" and "is not".

What is Operand?
The operand may be variable or value.

Example
total=154.34 # one variable is initialized with value

Here total is variable, 154.34 is the value of the variable (operand), ‘=’ is an operator.

Ex2: c=a+b, the operator ‘+’ is acting on two operands ‘a’ and ‘b’.

Classification of operators:

1. Unary Operators
2. Binary Operators
3. Ternary Operators

1. Unary Operators 

Unary: If an operator acts on a single variable, it is called unary operator.
a=-b

2. Binary Operators
If an operator acts on two variables then it is a binary operator

3. Ternary Operators 
If an operator acts on three variables /operands, then it is called the ternary operator.


Types of operators:
1.  Arithmetic
2.  Assignment
3.  Relational operators
4.  Logical operator
5.  Boolean operator
6.  Bitwise operator
7.  Membership
8.  Identity operators

Youtube channel Related video
Next topic: Arithmetic Operators
Previous topic: bytearray

Bitwise Complement (~)

Bitwise Complement (~)

a=25









Read the remainders from bottom to up
11001



Complement of a
a  = 1 1 0 0 1
~a= 0 0 1 1 0

Now we need to convert from binary to decimal:



0
0
1
1
0



24
23
22
21
20



16
8
4
2
1



0
0
4
2
0

Add 1’s: ~a=4+2=6
Here 16 is leadig with ‘0’ zero are no significance, it is not necessary to take .

This operator gives the complement form of a given number. This operator symbol is~, which is pronounced as tilde . It performs bitwise compliment each bit in the number is complimented, it is Unary operator, it has only one operand.




Next Topic: Bit wise Left shift operator (<<)
Previous Topic: Bitwise OR Operator (|)

Assessment 7

                                                            Assessment 7

1.  What is the difference between = and == operators?
a=10
b=20
=
==
a=b
a==b
i.e: we are assigning the value of variable ‘b’ to the left hand side variable ‘a’
We comparing two variables, which returns Boolean True/False
print(a)
print(a==b) or print(b==a) # compares, we get result True or false
Output: 20
Output: True

2. Find out the output of the following program
a=10
b=10
print(a is b)
x=True
y=True
print(x is y)

output:
True
True

3.  “Python is my favorite Programming language”
How to find the “favorite” is existing or not in the string?
Ans:
>>> a="Python is my favorite Programming language"
>>> print("favorite" in a)
True
>>>

4. If a=20, b=30
(a) Convert from decimal to binary (b) find the a&b (c) a|b

Ans:
(a)  a=10100, b=11110
(b)  a&b=20
(c)  a|b=30

(5.) Find out the a=25, (i) calculate a>>2 (ii) calculate a>>3

Ans:
a=11001
a>>2
6
>>> a>>3
3

(6.) Evaluate the expression: 3/2*4+3+(10//4)**3-2
Ans: 15.0

(7.) What is split() function?
Ans:
The split() method splits a string into a list.
You can specify the separator, the default separator is any whitespace.
Example:
"2 4".split() #We have to call the split() function w.r.t string
['2', '4']
>>> "1, 2 3 Hyd, Vij Kurnul,Kadapa atp".split(',')
['1', ' 2 3 Hyd', ' Vij Kurnul', 'Kadapa atp']
>>> 

(8.) Find out the output
a = 21
b = 10
c = 0
c = a + b
print ("c = a + b is ", c)
c += a
print ("c += a is", c )
c *= a
print ("c *= a is", c )
c /= a
print ("c /= a is ", c )
c  = 2
c %= a
print ("c %= a is", c)
c **= a
print ("c **= a is", c)
c //= a
print ("c //= a is", c)

Ans:
When you execute the above program, it produces the following result −
output:
c = a + b is  31
c += a is 52
c *= a is 1092
c /= a is  52.0
c %= a is 2
c **= a is 2097152
c //= a is 99864
>>>

9.  List out the bit wise operators in python.
Operator
Name
Description
AND
Sets each bit to 1 if both bits are 1
|
OR
Sets each bit to 1 if one of two bits is 1
 ^
XOR
Sets each bit to 1 if only one of two bits is 1
NOT
Inverts all the bits
<< 
Zero fill left shift
Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> 
Signed right shift
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

 10. Convert this string to lowercase? (PYTHON)
Ans:  string='PYTHON'
                         print(string.lower())



Next Topic: Command Line arguments
Previous Topic: Mathematical Functions
Related Video:https://www.youtube.com/watch?v=-AJntqLTi94&feature=youtu.be

Bitwise OR Operator (|)

Bitwise OR Operator (|)

a=10 b=11

Converting from Decimal to Binary:

1 byte is 8 bits so remaining are filled with 0’s
a=10 à0000 1010

b=11


b=11->0000 1011

So
a=10->0 0 0 0 1010
b=11->0 0 0 0 1011
-----------
a|b=   0 0 0 0 1 0 1 1
----------
Binary to decimal:
0
0
0
0
1
0
1
1
27
26
25
24
23
22
21
20
--
--
32
16
8
4
2
1
0
0
0
0
8
0
2
1

a|b=0+0+0+8+0+2+1=11 (Add only 1's, and ignore 0s)



Truth Table:

a
b
a|b
0
0
0
0
1
1
1
0
1
1
1
1



Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...