U3 S5-6
Notes
- In the Boolean value, Ture is 1 and False is 0.
- "==" is an alternative to equal to, since "=" is reserved for value assignment
- The relational operators come first and the logical operators come second, in the order of not, and, or
- In conditional, The true or false statement is a Boolean expression
- Flow Chart of a Nested Conditional Statement: in conditonal 1, "no" goes to statement, "yes" goes to condition 2. Condition 2 is the same
Below is an example of decimal number to binary converter which you can use as a starting template.
def DecimalToBinary(num):
strs = ""
while num:
# if (num & 1) = 1
if (num & 1):
strs += "1"
# if (num & 1) = 0
else:
strs += "0"
# right shift by 1
num >>= 1
return strs
# function to reverse the string
def reverse(strs):
print(strs[::-1])
# Driver Code
num = 67
print("Binary of num 67 is:", end=" ")
reverse(DecimalToBinary(num))
def DecimalToBinary(num):
if num > 0 :
DecimalToBinary(int(num / 2))
print(num % 2, end='')
num = int(input("Enter a decimal number \n"))
DecimalToBinary(num)