Computer Science Principles
Course Progress
0/0
Objectives in LxD
3.4 Strings
3.4 Strings
3.3 Mathematical Expressions
3.3 Math Expressions
3.2 Data Abstractions
3.2 Data Abstractions
3.1 Variables & Assignments
3.1 Variables and Assignments
3.1 Variables and Assignments (Sample)
Intro to Python
Intro to Javascript
Variables and HTML DOM (Sample)
3.5 Boolean Expressions (PY)
3.5 Boolean Expressions (JS)
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.13 Developing Procedures
3.12 Calling Procedures
3.10 Lists
3.13 Developing Procedures
3.10 Lists
3.9 Developing Algorithms
3.17 Algorithmic Efficiency
3.9 Algorithms
3.17 Algorithmic Efficiency
3.15 Random Numbers (pseudocode)
3.15 Random Numbers (js)
3.15 Random Numbers (py)
BI 3 Review
Sprint View
Week 5
3.5 Boolean Expressions (PY)
3.5 Boolean Expressions (PY)
1 min read
🔑 Boolean Hacks in Python: A Few New Codes!
This notebook has short Boolean challenges. Edit the code where it says TODO to make it work.
Challenge 1: Positive Number
Fix the condition so it prints num is positive if the number is greater than 0.
num = 3 # Try changing this number!
# TODO: change the condition
if num < 0: # <-- wrong, fix this
print(num, "is positive")
else:
print(num, "is NOT positive")
3 is NOT positive
Challenge 2: Is Even?
Change the condition so it prints num is even when the number is divisible by 2.
num = 7
# TODO: check if num is even
if num % 2 == 0: # <-- not correct
print(num, "is even")
else:
print(num, "is odd")
7 is odd
Challenge 3: Teenager Check
Print Teenager if age is between 13 and 19 (inclusive). Otherwise print Not Teenager. Fix the condition.
age = 20
# TODO: fix the condition
if age > 12 and age < 20: # <-- wrong
print("Teenager")
else:
print("Not Teenager")
Not Teenager
Challenge 4: Lamp Logic
We have two switches: a and b. The lamp should turn ON if at least one is True.
Fix the condition.
a, b = True, False
# TODO: fix condition for at least one True
if a or b: # <-- wrong
print("Lamp is ON")
else:
print("Lamp is OFF")
Lamp is ON