# Write a conditional to check if a is greater than or equal to b; if it is, print out
a = 15
b = 6
if (a >= b):
print("a is greater than b")
Run to view results
# Write a conditional to check if a is equal to b; if it is, print this out
a = 15
b = 6
if (a == b):
print('a equals b') # Now is the time for all good men to come to the aid of their country. Now is the time for all
Run to view results
# Write a conditional to check if a is NOT equal to b; if it is, print a out;
a = 6
b = 15
if a != b:
print("Not Equal")
else:
print(a)
Run to view results
# Write a conditional to check if a is not equal to b; if True print out the statement
# "a is not equal to b"; if false print out the statement that "a is equal to b"
# Check your code by choosing values for a & b which are equal and which are not equal
a = 14
b = 16
if a != b:
print("a is not equal to b")
else:
print("a is equal to b")
Run to view results
#Write a conditional to check the length of the given string. Remember to do this we
# use the function len() with the string (or list) name in parenthesis
#
# If string has <= 10 characters print this fact out;
# otherwise print out that the string has more than 10 characters.
# Test your code on strings which satisfy each condition
s = "Florida Seminoles"
if len(s) >= 10:
print("string has more than 10 chars")
else:
print("string has 10 chars or less")
Run to view results
x = 1101
if x < 0:
print("x is negative")
elif x < 100:
print("x is a positive number less than 100")
else:
print("x is a positive number greater than or equal to 100")
Run to view results
sales = 230000; target = 210000
percentage = sales/target
if percentage < 0.90:
print(f"commission rate = 2%, sales are {percentage} of the target sales")
elif percentage < 0.99:
print(f"commission rate = 3%, sales are {percentage} of the target sales")
elif percentage < 1.1:
print(f"commission rate = 3.5%, sales are {percentage} of the target sales")
elif percentage >= 1.1:
print(f"commission rate = 4%, sales are {percentage} of the target sales")
Run to view results
# write a for loop to cube the variable x and print out x and its cube where x
# takes on values from the given list
my_list = [1, 2, 3, 4, 5]
for x in my_list:
print(f"{x} cubed is {x**3}")
Run to view results
# write a for loop using range(m,n) to print out x^3 where x ranges from 1 to 5
for x in range(1, 6):
print(f"{x} cubed is {x**3}")
Run to view results
# write statements to print out each letter in the string "Go Noles" except the space (use
# a conditional to avoid printing out space)
for char in "Go Noles":
if char != " ":
print(char)
Run to view results
# write a for loop using range to print out x^3 where x is an ODD number
# from 1 to 5
for x in range(1, 6):
if x % 2 != 0:
print(f"{x} cubed is {x**3}")
Run to view results
# write a while loop to print out x^3 while x <= 5; initialize x to 1
# remember to increment x in the loop or else the while condition will never be satisfied
# If this happens go up to Kernel menu and click "interrupt"
x = 1
while(x <= 5):
print(x**3)
x += 1
Run to view results
# write a while loop to print out x^3 for all ODD numbers from 1 to 5
# remember to increment x in the loop or else the while condition will never be satisfied
# If this happens go up to Kernel menu and click "interrupt"
# Hint: To cube only the odd numbers you can either (1) start with 1 and increment
# x by 2 instead of 1 OR (2) use the modulo function to check if x is odd; if true compute
# cube of x
x = 1
while(x <= 5):
if x % 2 != 0:
print(x ** 3)
x += 1
Run to view results
#1. List comprehensions by iterating over a Python list
fib = [1, 2, 3, 5, 8, 13, 21] # list of first 7 Fibonacci numbers
# create a list of squares of first 7 Fibonacci numbers using a for loop
for i, num in enumerate(fib):
fib[i] = num ** 2
print(fib)
# create a list of squares of first 7 Fibonacci numbers using list comprehension
fib2 = [num for num in fib]
print(fib2)
Run to view results
#2. List comprehension for iterating over a Python list where we add a conditional
# Use for to create a list of squares of all Fibonacci number where
# the square is less than 100
list1 = []
for num in fib:
if num < 100:
list1.append(num)
print(list1)
# Use list comprehension to create a list of squares of all Fibonacci number where
# the square is less than 100
fib3 = [num for num in fib if num < 100]
print(fib3)
Run to view results
#3. List comprehensions by using a for loop with a range
fib = [1, 2, 3, 5, 8, 13, 21] # list of first 7 Fibonacci numbers
# create a list of squares of first 7 Fibonacci numbers using a for loop
list22 = []
for num in fib:
list22.append(num**2)
print(list22)
# create a list of squares of first 7 Fibonacci numbers using list comprehension
list33 = [num**2 for num in fib]
print(list33)
Run to view results
#This nested loop is for reference to understand how nested loops work
for i in range (0,2) : # i takes on the values 0 and 1 in this loop
for j in range (3,5) : # j takes on the values of 3 and 4
print (f"the value of i is {i} and the value of j is {j}")
# Note that i is not incremented until the j loop is complete
Run to view results
# 1. Using a nested for loop take sum of each number in the first list with each number in second list.
# Print out the numbers being summed and their sum.
list1 = [1, 2, 3, 4]
list2 = [10, 20]
for num in list1:
for num2 in list2:
print(f"{num} + {num2} = {num + num2}")
Run to view results
# 2.Write a nested __for__ loop to check if each number in a given list
# is divisible by 2 or by 3. Print out results.
my_list = [3, 8, 12]
for num in my_list:
if num % 2 == 0 or num % 3 == 0:
print(num)
Run to view results