Multiway Conditional Check, Nested If, Bitwise Ops and Membership Operator

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Multiway Conditional Check, Nested if, Bitwise Ops and

Membership Operator

August 9, 2022

1 Multiway - Conditional Check (Using elif)


• When you have more than 2 possible outcomes
• elif always preceded by an if statement
• Unlike else, elif takes condition
• An if statement can be followed by n number of elif statements but by only 1 else statement

[5]: # Write a program to check profit or loss


cp = int(input())
sp = int(input())
if cp > sp:
print('Loss')
elif cp < sp:
print('Profit')
else:
print('No Profit No Loss')

100
100
No Profit No Loss

[10]: # When you have to say whether the given number is +ve or -ve
n = int(input())
if n > 0:
print('+ve')
elif n < 0:
print('-ve')
else:
print('Zero')

-12
-ve

1.1 Finding out the largest of three distinct integers


abc
10 20 30 –> C is the largest

1
20 30 10 –> B is the largest
30 10 20 –> A is the largest

[13]: a, b, c = map(int, input().split())


if a > b and a > c:
print(f'a is largest: {a}')
elif b > a and b > c:
print(f'b is largest: {b}')
else:
print(f'c is largest: {c}')

30 20 10
a is largest: 30

1.2 Printing week name when week day is given


• Day 1 —> Monday
• Day 2 —> Tuesday
• Day 3 —> Wednesday
• ……
• …..
• Day 7 –> Sunday
1 –> Monday
6 –> Saturday

[22]: day = int(input()) # 2


if day == 1: # 2 == 1
print('Monday')
elif day == 2: # 2 == 2
print('Tuesday')
elif day == 3:
print('Wednesday')
elif day == 4:
print('Thursday')
elif day == 5:
print('Friday')
elif day == 6:
print('Saturday')
elif day == 7:
print('Sunday')
else:
print('Invalid')

-15
Invalid

2
2 Nested if statements
• Writing if statements inside other if statements is called nested if statements

2.1 Finding out the largest of three distinct integers using nested if statements
(This time using nested if)
[26]: a, b, c = map(int, input().split()) # 20 10 30
if a > b: # 20 > 10
if a > c: # 20 > 30
print(f'a is largest: {a}')
else:
print(f'c is largest: {c}')
else:
if b > c: # 20 > 30
print(f'b is largest: {b}')
else:
print(f'c is largest: {c}')

10 30 20
b is largest: 30

3 Bitwise Operators
• Operates on bit level.
• Need to have the knowledge on binary number system.
• & –> Bitwise AND
• –> Bitwise OR
• ^ –> Bitwise XOR
• « –> Bitwise left shift
• » –> Bitwise right shift

[27]: # Bitwise AND


11 & 12

[27]: 8

[28]: # Bitwise OR
11 | 12

[28]: 15

[29]: # Bitwise XOR


11 ^ 12

3
[29]: 7

3.1 Decimal Number System (Base 10)


• 100, 5465456946566 –> Decimal Number System
• Deci –> Ten (10)
• Digits –> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 –> 10

3.2 Binary Number System (Base 2)


• There are only two digits –> 0, 1
• 2 (and it’s powers) a significant role in binary

3.2.1 Decimal to Binary Conversion


Decimal Number: 23
Binary Represe: 10111
• Find out the nearest power of 2 which is <= given number
• Put a 1 below the numbers that you can use to add upto the given number
• Put a 0 below the numbers that you can’t use
16 8 4 2 1
1 0 1 1 1 –> This the binary representation of 23.
(10111)
Decimal: 49
Binary: 110001
32 16 8 4 2 1
1 1 0 0 0 1 –> This the binary of 49.
(110001)
Decimal: 75
Binary: 1001011
64 32 16 8 4 2 1
1 0 0 1 0 1 1 –> This is the binary of 75
(1001011)
Halving
Decimal: 23
Binary: ?
23 –> 1
11 –> 1
5 –> 1
2 –> 0
1 –> 1
- The number is even write a 0 - The number is odd write a 1
Write the results bottom up
(10111)

4
Decimal: 75
Binary: 1001011
75 –> 1
37 –> 1
18 –> 0
9 –> 1
4 –> 0
2 –> 0
1 –> 1
Bottom up (1001011)

3.2.2 Binary to Decimal Conversion


Example 1:
Binary: 10111
Decimal: 23
To convert any binary number into decimal form do the following
• Seperate the bits as follows and

• Write the powers of 2 (starting from 20 ) from right to left under each bit

1 0 1 1 1
16 8 4 2 1

And add all the numbers under 1 to get the decimal representation
16 + 4 + 2 + 1 = 23
Some more examples
Example 2:
Binary: 1101101
Decimal: 109
Seperating bits

1 1 0 1 1 0 1
64 32 16 8 4 2 1

Adding all the numbers that are under 1 to get the decimal representation
64 + 32 + 8 + 4 + 1 = 109
Example 3:

5
Binary: 1111011
Decimal:
Seperating bits

1 1 1 1 0 1 1
64 32 16 8 4 2 1

Adding all the numbers that are under 1 to get the decimal representation
64 + 32 + 16 + 8 + 2 + 1 = 123

3.3 Bitwise AND


• Bitwise AND (&) compares each bit of the first operand with corresponding bit of the second
operand and sets the result bit to 1 if both bits are 1, otherwise to 0.

[32]: # Bitwise AND


print(11 & 12)
# Write the binary representation of the given two operands
# 1011
# 1100
# ----
# 1000 --> 8
# 1 0 0 0
# 8 4 2 1

3.4 Bitwise OR (|)


• Bitwise OR (|) Compares each bit of the first operand with corresponding bit of the second
operand and sets the resultant bit to 1 even if one of the bits is 1 otherwise to 0.

[ ]: ### Bitwise OR
print(11 | 12)
# 1011
# 1100
# ----
# 1111 --> 15

3.5 Bitwise XOR


• Bitwise XOR (^) Compares each bit of the first operand with corresponding bit of the second
opeand and sets the resultant bit to 1 if and only if one bit is 1 and the other is 0 or vice
versa (Alternate bits), otherwise 0.

[35]: # Bitwise XOR


print(11 ^ 12)

6
# 1011
# 1100
# ----
# 0111 --> 7

3.6 Bit shortage


• In the case of bit shortage we are allowed to add as many 0s as we want on the left end of
binary representation.

[37]: # 5 & 11
print(5 & 11)
# 0101
# 1011
# ----
# 0001 --> 1

3.7 Bitwise left shift


• a«b
• Shifts the bits in a to their left. b states how many bits to shift
• a « b == a * 2 ** b

[38]: print(11 << 1)


# 11 * 2

# 1 0 1 1 0
# 16 8 4 2 1 --> 22

22

3.8 Bitwise right shift


• a»b
• Shifts the bits in a to their right. b states how many bits to shift
• a » b == a // 2 ** b

[39]: print(11 >> 1)


# 11 // 2

[40]: print(14 << 3)


# 14 * 8

112

7
[41]: print(14 >> 3)
# 14 // 8

4 Membership Operators
• Check if the given value is a member of community
• MLA –> Member of something
• MP –> Member of
• in
• not in
• It checks if an element is a member of an iterable - string, list, set, range, tuple

[42]: print('h' in 'hello')

True

[43]: print('H' in 'hello')

False

[44]: print(10 in [10, 20, 30])

True

[45]: print(100 in [10, 20, 30])

False

[46]: print('apple' in ('apple', 'orange', 'mango'))

True

[47]: print('apple' in ('banana', 'orange', 'mango'))

False

[48]: print(1 in 100)

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [48], in <cell line: 1>()
----> 1 print(1 in 100)

TypeError: argument of type 'int' is not iterable

[49]: print('1' in '100')

True

8
[50]: print('h' not in 'hello')

False

[51]: print(100 not in [10, 20, 30])

True

[52]: print('hel' in 'hello')

True

[53]: print('helo' in 'hello')

False

4.1 Finding out if the given character is vowel or consonant


[55]: ch = input("Enter a character: ")
if ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u':
print('Vowel')
else:
print('Consonant')

Enter a character: z
Consonant

[57]: ch = input("Enter a character: ") # a


vowels = ['a', 'e', 'i', 'o', 'u'] # a
if ch in vowels:
print('Vowel')
else:
print('Consonant')

Enter a character: z
Consonant

[62]: ch = input("Enter a character: ") # a


vowels = 'aeiouAEIOU'
if ch in vowels:
print('Vowel')
else:
print('Consonant')

Enter a character: Z
Consonant

[64]: lst = [10, 11, -1, 14, 17, 100, 241]


se = int(input())
if se in lst:
print('True')

9
else:
print('False')

1456
False

[ ]: # You will be given a character and you have to say if it's


# a digit or 0-9
# a alphabet abcd
# a special character #$^&*()-_

[67]: digits = '0123456789'


alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
ch = input("Enter a character: ")
if ch in alpha:
print('Alphabet')
elif ch in digits:
print('Digit')
else:
print('Special character')

Enter a character: _
Special character

10

You might also like