An Introduction to Python

Conditionals

This notebook contains the programming exercises for An Introduction to Python: Conditionals.

Exercises

Do the following problems.

Jupyter Notebook Shortcuts:

There are two modes when a cell is highlighted.

Command Mode: Press ESC to activate. The cell has a blue border if this mode is active. In this mode, you can add, delete, create, copy and paste cells.

  • create a new cell above the current cell: a
  • create a new cell below the current cell: b
  • delete the current cell: dd
  • change the current cell’s type to “Code”: y
  • change the current cell’s type to “Markdown”: m

Edit Mode: Press ENTER to activate. The cell has a green border if this mode is active. In this mode, you can edit and type text into the cell.

  • execute the current cell and create a new cell: SHIFT + ENTER

Write a segment of code that uses conditionals to print out the messages "Gold", "Silver" or "Bronze" according to the value of the variable x which represents the standing of an athlete at an Olympics event.

In [ ]:
 

What is logically wrong with the following code? Use markdown to answer the question. You can also run the code to check your answer.

if grade >= 90:
    print("You got an A!")
if grade >= 80:
    print("You got a B!")
if grade >= 70:
    print("You got a C!")
if grade >= 60:
    print("You got a D!")
if grade >= 50:
    print("You got an F!")
if  i < j:
    if j < k:
        i = j 
    else:
        j = k 
else:
    if j > k: 
        j = i
    else: 
        i = k
print("i =", i, " j =", j, " k =", k)

What will the code print if the variables i, j, and k have the following values?

(a) i = 3,j = 5,and k = 7

(b) i = 3,j = 7,and k = 5

(c) i = 5,j = 3,and k = 7

If a boolean expression uses all of and , or and not. The not operator has the highest precedence and should be evaluated first. Then evaluate and before or.

In the following, note that evaluating or before and in the following gives a different and incorrect answer. What is the value of b?

x = 2
y = 4
z = 5
b = z > 2 or x > 3 and y < 3

Write a segment of code that ask the user to input an integer and prints out the message "Fizz" if the number is a multiple of 3, "Buzz" if the number is a multiple of 5 and "FizzBuzz" if it is a multiple of both 3 and 5. If the number satisfies none of the above conditions, print the number.

Test your code!:

  • x = 4 should print 4.
  • x = 12 should print "Fizz".
  • x = 10 should print "Buzz".
  • x = 45 should print "FizzBuzz".

The first line of code has been given to you to receive user input.

In [ ]:
x = int(input("Enter a positive integer. "))
# This is a comment. fill out your code below.

What is the value of y in the following? Try it first then run the code to check your answer.

1)

x = 30
y = "a" if x > 15 else "b"

2)

x = 15
y = "a" if x > 15 else "b"

3)

x = 10
y = "a" if x > 15 else "b" if x >=11 else "c"

4)

x = 12
y = "a" if x > 15 else "b" if x >=11 else "c"

5)

x = 35
y = "a" if x > 15 else "b" if x >=11 else "c"

Let y represents a year. A leap year is a year that is divisible by 4 and not by 100 unless it is divisible by 400. For example, 4, 2008, and 1600 are leap years but not 1700. Write a segment of code that determines whether y is a leap year. Do it in two ways: 1) Use if/elif/else statements. 2) Use only boolean logical operators and, or and not.

Test your code with various values for y.

In [4]:
# method 1, use if/elif/else
y = 1324
is leap
In [10]:
# method 2, use boolean operators and, or and not 
# fill in the boolean expression for isLeap below
y = 2400
isLeap = 
print(isLeap)
True
In [ ]: