Skip to the content.
3.1 and 3.4 3.2 3.3 and 3.5 3.8 3.10

3.3 and 3.5 Hacks

hacks

DevOps

Popcorn Hacks 3.3

Popcorn Hack 1

def output(x):
    print(3 * (x + 9) - 5)

output(2)
28

Popcorn Hack 2

number1 = 11
number2 = 5
number3 = number1 % number2
number4 = number3 * number1 + 70
print(number3)
print(number4)
1
81

Popcorn Hack 3

numbers = [17, 10, 20, 43]

for num in numbers:
    remainder = num % 3
    if remainder == 0:
        print(num, "is divisible by 3")
    else:
        print("The remainder when", num, "is divided by 3 is", remainder)
The remainder when 17 is divided by 3 is 2
The remainder when 10 is divided by 3 is 1
The remainder when 20 is divided by 3 is 2
The remainder when 43 is divided by 3 is 1

Homework Hacks 3.3

Hack 1

n = 10

def fibonacci(n):
    if n <= 0:
        return "Input must be a positive integer."
    elif n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        a, b = 0, 1
        for i in range(2, n):
            a, b = b, a + b
        return b

print(f"The {n}-th Fibonacci number is: {fibonacci(n)}")
The 10-th Fibonacci number is: 34

Hack 2

import math

def calculate_area():
    shape = input("Enter the shape (circle, square, rectangle): ").lower()
    
    if shape == "circle":
        radius = float(input("Enter the radius of the circle: "))
        area = math.pi * (radius ** 2)
        print(f"The area of the circle is: {area:.2f}")
    
    elif shape == "square":
        side = float(input("Enter the side length of the square: "))
        area = side ** 2
        print(f"The area of the square is: {area:.2f}")
    
    elif shape == "rectangle":
        length = float(input("Enter the length of the rectangle: "))
        width = float(input("Enter the width of the rectangle: "))
        area = length * width
        print(f"The area of the rectangle is: {area:.2f}")
    
    else:
        print("Invalid shape.")

calculate_area()
The area of the square is: 625.00

Popcorn Hacks 3.5

Popcorn Hack 1

variableOne = False
variableTwo = True

if variableOne:
    print("VariableOne is true!")
else:
    print("VariableOne is false!")

if variableOne and not variableTwo:
    print("VariableA is true and VariableTwo is false")

if variableOne or variableTwo:
    print("Either variableA or variableB is true")
VariableOne is false!
Either variableA or variableB is true

Popcorn Hack 2

number = 11
number2 = 10

if number > number2:
    print("number is greater than number2 (10)!")
else:
    print("number is not greater than number2 (10)!")
number is greater than number2 (10)!

Popcorn Hack 3

number = 11

if number >= 100 and number <= 999:
    print("number is three digits!")
else:
    print("number is not three digits!")
number is not three digits!

Homework Hacks 3.5

def deMorganLaw():
    print("A      B        Result")
    values = [False, True]
    
    for A in values:
        for B in values:
            result = not (A and B) == (not A or not B)
            print(f"{A}  {B}  {result}")

deMorganLaw()
A      B        Result
False  False  True
False  True  True
True  False  True
True  True  True