Domain 3 - Recap Flashcards

1
Q
  1. Using curly braces, produce a print statement that uses string formatting to display the message “Hi I’m Bryce, I am 25 years old.
  2. What benefit do the curly braces provide for string formatting?
A
  1. The curly braces as you to mix strings and ints together without the need to cast values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. Using the new format string method ‘f’, display the message “Hi I’m Bryce, I am 25 years old.
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. In Python 2, strings were formatted using the following

%s

%d

Explain what types of variables can be used with each one.

A
  1. %s was used as a placeholder for strings.
  2. %d means ‘decimal’ but it can be a placeholder for numbers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Refer to the floating point variable below

cost = 1000.1212

Using a print statement, how can this be formatted to two decimal places?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Using the same variable again, explain what the output will be from this print statement.

cost = 1000.1212

print(“The cost of the product is %10f” %cost)

A
  1. This means the output number will be ten digits long.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will be the output of the following print statement?

print(“Hello \tWorld \nUniverse”)

A
  1. The escape character \t will insert a tab
  2. The escape character \n will place the word “Universe” on a new line.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Using the same example again, how would we print everything in the string including the escape characters?

print(“Hello \tWorld \nUniverse”)

A
  1. Use ‘r’ to print a raw string.

print(r”Hello \tWorld \nUniverse”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the following code an example of?

s = ‘’’\
Hello
World
Universe’’’

print(s)

A
  1. This is a multi line string
  2. The backslash is used to escape the new line.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Will a python script run if it encounters a syntax error?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Numbers can be formatted using different systems. What do each of these letters represent?

  1. b
  2. c
  3. d
  4. o
  5. x
  6. X
A
  1. b = binary
  2. c = character
  3. d = decimal
  4. o = octal
  5. x = hexadecimal lowercase
  6. X = hexadecimal uppercase
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will be the output of the following code sample?

num = 7000

formatted = “”
print(formatted.format(num))

A

1B58

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will be the output of the following code sample?

num = 7000

formatted = “”
print(formatted.format(num))

A
  1. 7,000

Note the inclusion of the comma in this line formatted = “”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What symbols can be used to format text to do the following?

  1. centered
  2. right
  3. left
A
  1. ^ = centered
  2. > = right aligned
  3. < = left aligned
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What will be the output of the following formatted text?

num = 5000

num2 = 200

formatted = {1: *^15, d}

print(formatted.format(num, num2))

A

******200******

Note, 1 is equal to the index of num2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If the user enters a value of 5 into this script, what will the print statement and the eval function display?

a = input(“Type some input “)

print (“input is {}, is type of {} “.format(a, type(a) ))

b = eval(a)

print(type (b))

A
  1. input is 5, is type of
    2.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A