0% found this document useful (0 votes)
2 views12 pages

Class 3 Integer Float Data Types

The document outlines various operations that can be performed on integers in Python, including arithmetic, comparison, equality, logical, and bitwise operations. It provides examples of each operation, demonstrating their outputs and behaviors, such as how comparison operators return boolean values and how bitwise operations manipulate binary representations of integers. Additionally, it explains the concept of type casting and the evaluation of truthy and falsy values in Python.

Uploaded by

priyanjali patel
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)
2 views12 pages

Class 3 Integer Float Data Types

The document outlines various operations that can be performed on integers in Python, including arithmetic, comparison, equality, logical, and bitwise operations. It provides examples of each operation, demonstrating their outputs and behaviors, such as how comparison operators return boolean values and how bitwise operations manipulate binary representations of integers. Additionally, it explains the concept of type casting and the evaluation of truthy and falsy values in Python.

Uploaded by

priyanjali patel
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

Types of operation that we can perform

on integers
1. Arithmatic operations + , - , * , /(float division), %(modulo), //(floor division) ,
**(exponenet)

In [1]: print(10+2) # +

print(10-2) # -

print(10*2) # *

print(10/2) # but the answer will be always in the float

print(10/3)

print(10.0//3) #floor divsion

print(10//3.0)

print(10%3) # modulo --> remainder

print(15%4)

print(100**2)

print(pow(100,2))

12
8
20
5.0
3.3333333333333335
3.0
3.0
1
3
10000
10000

2. Comaprison operator , > , >= , <= , <

"""

it always gives the output(return) value inf form True / False (Bool)
int vs int
float vs float
int vs float
str vs str

"""

In [3]: a = 100
b = 20

print(a<b)

False

In [4]: a = 10
b = 20.0

print(a<=b)

True

In [ ]: a = 10.0
b = 20.0

print(a<=b)

In [5]: a = 'abc'
b = 100.0

print(a<b)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 4
1 a = 'abc'
2 b = 100.0
----> 4 print(a<b)

TypeError: '<' not supported between instances of 'str' and 'float'

In [6]: # str vs str

a = 'abc'
b = 'xyz'

# a - 97
# x - 120

print(a>b)

False

[Link]

In [28]: a = 'abc'
b = 'Abc'

# a - 97
# A - 65
print(a>b)

True

In [29]: a = 'a'
b = 'Abc'

# a - 97
# A - 65

print(a>b)

True

In [30]: a = 'abc'
b = 'abz'

# c --> 99
# z ---> 122

print(a>b)

False

In [32]: a = 'abc'
b = 'abcz'

# c --> 0
# z ---> 122

print(a>b)

False

In [ ]: a = 'abc#'
b = 'abc'

print (a<=b)

False

In [ ]: 3. equality , == , !=

"""
- it returns the answer in from of True / False
-
"""

In [36]: a = 100
b = 'abc'

print(a == b)

False

In [37]: a = 'afsaan'
b = 'aFsaan'
print( a == b)

False

In [38]: a = 16+4j
b = None

print(a == b)

False

In [39]: a = 16+4j
b = None

print(a != b)

True

In [4]: # 4. ASCII

# ord() ---> getting the ascci value of the charac

ord('?')

ord(':')

Out[4]: 58

In [6]: # chr() -> getting the charac from the ascii value

chr(47)
chr(40)

Out[6]: '('

In [7]: # question number 1

print( 'A' <= chr(65) )

True

In [8]: print(ord('c')//3)

33

In [10]: print(97 == chr('a'))

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[10], line 1
----> 1 print(97 == chr('a'))

TypeError: 'str' object cannot be interpreted as an integer

In [ ]: 5. logical operators - and , or , not

# and Truth table


True and True --> True
True and False --> False
False and True ---> False
False and False ---> False

# or truth table

True or True --> True


True or False --> True
False or True ---> True
False or False ---> False

# not truth table

not True ---> False


not False ---> True

In [12]: print(1 and 0)

In [13]: print(0 and 0)

In [14]: print(not 1)

False

In [15]: print(True)

True

In [17]: # type casting


int(False)

Out[17]: 0

In [18]: True + True + False

Out[18]: 2

In [60]: True == 1

Out[60]: True

In [61]: # question 3

print(True and False)

False

In [19]: # there is small deviation if A and B are two values


- A and B ---> result will be A if A is False else B
A or B ---> result will be A if A is True else B

Cell In[19], line 3


- A and B ---> result will be A if A is False else B
^
SyntaxError: invalid syntax

In short:

✔️ A and B → returns A if falsy, else B ✔️ A or B → returns A if truthy, else B ⚡


They return values, not always True or False, and stop early (“short-circuit”).

Expression Evaluated result Explanation

0 and 5 0 first falsy → return it

5 and 10 10 first true → return second

0 or 5 5 first falsy → return second

5 or 10 5 first true → return it

"" or "yes" "yes" empty string → falsy

"ok" and "done" "done" both truthy → return last one

In [20]: print( 10 or 100)

10

In [21]: A = bool(10) ---> True


B = bool(100) --- True

Cell In[21], line 1


A = bool(10) ---> True
^
SyntaxError: invalid syntax

In [22]: print(0 and 200)

In [ ]: print(200 or 0)

200

In Python, the bool() function converts a value to a Boolean. Non-zero numbers,


including 0.0001, are evaluated as True. Zero and 0.0 are considered False

In [24]: bool(0.0001)

Out[24]: True

In [27]: bool(0.001)
Out[27]: True

In [29]: bool('A')

Out[29]: True

In [30]: bool(None)

Out[30]: False

In [25]: print(200 and 0)

In [ ]: int - 0 is false and rest all the value are True

float - 0.0 is false and rest all the value are true

str - '' is false and rest all the values are true

complex - 0+0j is false and rest all the value True

None - None is always False

5. Bitwise Operations - &(bitwise and), |(bitwise or) , ^(bitwsie xor) , ~(bitwise


not) , <<(left shift) , >>(right shift)

works on bit
works only on integer data type

In [ ]: # bitwise and

x = 10 # -----> binary ---> 1010


y = 4 # ------> binary --> 0100

10 ---> 1 0 1 0
4 ----> 0 1 0 0
and --> 0 0 0 0

# converting the binary anse ---> int


print(0b0000)

In [31]: print(0b0000)

In [83]: x = 12 # -----> binary ---> 1100


y = 22 # ------> binary --> 10110

10 ---> 0 1 1 0 0
4 ----> 1 0 1 1 0
and --> 0 0 1 0 0
# converting the binary anse ---> int
print(0b00100)

In [ ]: x = 12 # -----> binary ---> 1100


y = 22

print(x & y)

In [82]: # bitwise or

x = 12 # -----> binary ---> 1100


y = 22 # ------> binary --> 10110

10 ---> 0 1 1 0 0
4 ----> 1 0 1 1 0
or --> 1 1 1 1 0

# converting the binary anse ---> int


print(0b00100)

In [2]: print(x | y)

14

In [86]: print(0b11110)

30

In [ ]: # bitwise xor

x = 12 # -----> binary ---> 1100


y = 22 # ------> binary --> 10110

10 ---> 0 1 1 0 0
4 ----> 1 0 1 1 0
xor --> 1 1 0 1 0
# converting the binary anse ---> int
print(0b11010)

In [35]: print(0b11010)

26

In [34]: print(x ^ y)

26

In [ ]:

Perfect question 👏 — let’s go step-by-step and fully demystify bitwise


operations in Python, using your example x = 12 , y = 22 .
We’ll cover 👇 1️⃣ What bitwise operators do 2️⃣ How to find binary equivalents
(e.g. “8421 method”) 3️⃣ Step-by-step examples for & , | , ^ , ~ , << , >>

🧩 Step 1 — Reminder: works on bits, not


digits
Bitwise operators work bit-by-bit on integers. To use them, you need to convert
numbers into binary form (base 2).

🧠 Step 2 — Binary (8421 rule)


The 8421 rule helps you decode/encode binary quickly:

Bit position 8 4 2 1

Binary digit 1 1 0 0

Decimal value

Sum = 12 8+4=12 ✅
So:

12 → 1100

Now for 22:

Bit position 16 8 4 2 1

Binary digit 1 0 1 1 0

Decimal value 16 + 4 + 2 = 22

So:

22 → 10110

🧮 Step 3 — Align bits for both numbers


We align their binary representations (same number of bits):

12 = 01100
22 = 10110
🧩 Step 4 — Bitwise operations in action
Let’s now apply each operator:

✅ & → Bitwise AND


Rule: 1 & 1 = 1 , else 0 .

01100 (12)
& 10110 (22)
= 00100 → 4

✅ Output:
print(x & y) # 4

✅ | → Bitwise OR
Rule: 1 | 1 = 1 , 1 | 0 = 1 , 0 | 1 = 1 , else 0 .

01100
| 10110
= 11110 → 30

✅ Output:
print(x | y) # 30

✅ ^ → Bitwise XOR (exclusive OR)


Rule: 1 ^ 1 = 0 , 0 ^ 0 = 0 , 1 ^ 0 = 1 , 0 ^ 1 = 1 .

01100
^ 10110
= 11010 → 26

✅ Output:
print(x ^ y) # 26

✅ ~ → Bitwise NOT (flips bits)


Rule: turns every 1 to 0 and 0 to 1 . ⚠️ Works in 2’s complement form
(signed integer).

x = 12 → binary 00001100
~x → binary 11110011 (inverts all bits)

To interpret this:

~x = -(x + 1)

✅ Output:
print(~12) # -13

✅ << → Left Shift (multiply by 2ⁿ)


Shifts all bits to the left, adding zeros on the right.

Example:

12 << 1 → 24 (multiply by 2)
12 << 2 → 48 (multiply by 4)

✅ Output:
print(12 << 1) # 24

✅ >> → Right Shift (divide by 2ⁿ)


Shifts bits to the right, dropping off the last bits.

12 >> 1 → 6 (divide by 2)
12 >> 2 → 3 (divide by 4)

✅ Output:
print(12 >> 2) # 3

🧮 Quick Summary Table


Binary Decimal
Operator Meaning Example
Result Result

Bitwise
& 12 & 22 00100 4
AND
Binary Decimal
Operator Meaning Example
Result Result

Bitwise
` ` 12 22 11110 30
OR

^ Bitwise XOR 12 ^ 22 11010 26

~ Bitwise NOT ~12 11110011 -13

<< Left shift 12 << 1 11000 24

>> Right shift 12 >> 2 00011 3

✅ In short:
Bitwise operators directly manipulate the binary digits (bits) of
integers. You can use the 8421 rule to quickly visualize the binary
pattern.

You might also like