Code Icon

PYTHON CONDITIONAL STATEMENTS

Links


    # IF 
    is_male = False
    is_tall = False

    if is_male and is_tall:
        print("You are a tall male")
    elif is_male and not(is_tall):
        print("Male not tall")
    elif not(is_male) and is_tall:
        print("Not male but tall")
    else:
        print("Not a male and not tall")



    # COMPARISON OPERATORS
    def max_num(num1, num2, num3):
        if num1 >= num2 and num1 >= num3:
            return num1
        elif num2 >= num1 and num2 >= num3:
            return num2
        else:
            return num3

    print(max_num(3, 40, 5))
    # other operators: == , !=, 

    # BETTER CALCULATOR

    num1 = float(input("Enter first number: "))
    op = input("Enter operator : "))
    num2 = float(input("Enter second number: "))

    if op == "+":
        print(num1 + num2)
    elif op == "-":
        print(num1 - num2)
    elif op == "*":
        print(num1 * num2)
    elif op == "/":
        print(num1 / num2)
    else
        print("Operator not valid")
    result = int(num1) + int(num2) # problem is that it won't allow to use decimals
    # result = float(num1) + float(num2)
    
    print(result)


    # WHILE
    i = 1
    while i >= 10:
        print(i)
        i += 1


    # SECRET WORK GAME
    secret_word = "giraffe"
    guess = ""
    guess_limit = 3
    guess_count = 0
    out_of_guesses = False

    while guess != secret_word and not(out_of_guesses):
        if guess_count < guess_limit:
            guess = input("Enter guess:")
            guess_count += 1
        else:
            out_of_guesses = True

    if out_of_guesses:
        print("You lose")
    else:
        print("You win!")


    # FOR LOOP
    for letter in "Giraffe Academy":
        print(letter)

    friends = ["Kim", "Joanna", "Leslie"]
    
    for index in range(10) #also in range (3, 10)
        print(index)
    for index in range(len(friends))
        print(friends[index])


    # EXPONENT FUNCTION
    def raise_to_power(base_num, pow_num):
        result = 1
        for index in range(pow_num):
            result = result * base_num

    print(raise_to_power(2, 3))


    # 2D LISTS

    number_grid = [
        [1,2,3],
        [4,5,6],
        [7,8,9],
        [0]
    ]

    print(number_grid[0][0])

    # nested for LOOP
    for row in number_grid:
        for col in row:
            print(col)