Domain 3 - Recap Flashcards
- Using curly braces, produce a print statement that uses string formatting to display the message “Hi I’m Bryce, I am 25 years old.
- What benefit do the curly braces provide for string formatting?
- The curly braces as you to mix strings and ints together without the need to cast values.
- Using the new format string method ‘f’, display the message “Hi I’m Bryce, I am 25 years old.
- In Python 2, strings were formatted using the following
%s
%d
Explain what types of variables can be used with each one.
- %s was used as a placeholder for strings.
- %d means ‘decimal’ but it can be a placeholder for numbers
Refer to the floating point variable below
cost = 1000.1212
Using a print statement, how can this be formatted to two decimal places?
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)
- This means the output number will be ten digits long.
What will be the output of the following print statement?
print(“Hello \tWorld \nUniverse”)
- The escape character \t will insert a tab
- The escape character \n will place the word “Universe” on a new line.
Using the same example again, how would we print everything in the string including the escape characters?
print(“Hello \tWorld \nUniverse”)
- Use ‘r’ to print a raw string.
print(r”Hello \tWorld \nUniverse”)
What is the following code an example of?
s = ‘’’\
Hello
World
Universe’’’
print(s)
- This is a multi line string
- The backslash is used to escape the new line.
Will a python script run if it encounters a syntax error?
No
Numbers can be formatted using different systems. What do each of these letters represent?
- b
- c
- d
- o
- x
- X
- b = binary
- c = character
- d = decimal
- o = octal
- x = hexadecimal lowercase
- X = hexadecimal uppercase
What will be the output of the following code sample?
num = 7000
formatted = “”
print(formatted.format(num))
1B58
What will be the output of the following code sample?
num = 7000
formatted = “”
print(formatted.format(num))
- 7,000
Note the inclusion of the comma in this line formatted = “”
What symbols can be used to format text to do the following?
- centered
- right
- left
- ^ = centered
- > = right aligned
- < = left aligned
What will be the output of the following formatted text?
num = 5000
num2 = 200
formatted = {1: *^15, d}
print(formatted.format(num, num2))
******200******
Note, 1 is equal to the index of num2
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))
- input is 5, is type of
2.