3.5-7 NOTES
booleans have 2 possible values (like binary), but true is 1 and false is 0. can produce boolean value with boolean operator. boolean operators produce booleans after it is used between two values. there are two main types of operators: relationional and logical operators. relationals can work between any two values of the same type (operands)
equal to == operators
==, !=, <,>,>=,<= can also work such as string or list- values at each index compard in order to determine which is greater- ex abg< acd returns true because acd has more letters at the first part of the alphabet.
logical operators produce single boolean result. examples = and, or, not and returns true when both are true. or returns true when at least one is true. not returns true when the boolean after it is false.
relational operators go first. logical operators work in "not, and, or" order. parenthesis take precedence.
conditionals:
algorithm = set of instructions that accomplish a task selection = the process that determines which parts of an algorithm is being executed based on a condition that is true or false.
conditional is a statement that affects the outcome of a program by executing different statements baased on the result of a true or false statement (the true and false statement is a boolean expression) conditionals appear in almost every programming language. they are used by a programs selection process.
if else statements fill in the role of the conditional (some languages call it switch blocks)
they can also just be if statements (more as an interruption)
nested conditionals
nested conditional statements are conditional statements with their own conditional statements.
for example
if condition one = yes, there is a condition 2. if condition 2 is yes there is one statement, if it is no there is another. if the first condition is no, there is no second condition.
mathgrade = 50;
isgoodatmath = False
isbadatmath= True
if mathgrade <= 50:
print("you literally suck at math . what is wrong with you");
if (isgoodatmath):
print("wooow");
if (isbadatmath):
print("boo");
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))
MINE IS BELOW
def decToBinary(n):
binaryNum = [0] * n;
i = 0;
while (n > 0):
binaryNum[i] = n % 2;
n = int(n / 2);
i += 1;
for j in range(i - 1, -1, -1):
print(binaryNum[j], end = "");
n = 20;
decToBinary(n);
def decToBinary(m):
binaryNum = [0] * m;
i = 0;
while (m > 0):
binaryNum[i] = m % 2;
m = int(m / 2);
i += 1;
for j in range(i - 1, -1, -1):
print(binaryNum[j], end = "");
m = n * 20;
decToBinary(m);