Lecture 4 - Exercises

4A - print’s keyword arguments

  1. Which of the following is printed by: print("B", "C", "D", "BR", sep="A")

    1. BACADABR
    2. ABACADABRA
    3. ABACADABR
    4. BACADABRA
  2. What is printed by the following code?

    print("Name: ", end="\n---\n")
    print("Date:", end="\n---\n")
  3. What does the following program print?

    a = 31
    b = a // 4
    c = (5 % b) - 1.0
    print("a", a ** 0, sep=": ", end=";  ")
    print("b", b - 4, sep=": ", end=";  ")
    print("c", c * 2, sep=": ")
  4. Prompt the user for three numbers and print their sum without spaces between the numbers and operators, using what you learned about the print function’s keyword arguments.

    Enter the first number: 4
    Enter the second number: 2.4
    Enter the third number: 8.6
    4.0+2.4+8.6=15.0

4B - Binary Representation

For your convenience, here is a table of the first 8 powers of two:

2^0:   1
2^1:   2
2^2:   4
2^3:   8
2^4:  16
2^5:  32
2^6:  64
2^7: 128

Convert each of the following binary numbers to decimal:

  1. 1
  2. 10
  3. 1010
  4. 1111

Convert each of the following decimal numbers to binary:

  1. 32
  2. 15
  3. 28
  4. 68