Chapters 1-20 Flashcards
What is the command to create a directory in Powershell?
mkdir
How would you output the text below in a Python print statement.
That’s a ‘piece of cake’ as they say
print(“That’s a ‘piece of cake’ as they say”)
What is the command to run Python from Powershell?
python
How do you write a single line comment in Python?
#
What data type is 4.0
It’s a float
If we execute the statement
x = 4/2
then what is the datatype of x
It’s a float
what data type is 4
It’s an int
what is the % operator called and what does it do
what is the result of 5 % 2
It’s the modulo operator and displays the remainder portion of a division
5 % 2 is 1
What is the order of calculation of the following operators
+ - % / * ( ) **
P
E
(M&D) left to right
(A&S) left to right
(MD)(AS)The actual order is that you do the multiplication and division in one step, from left to right, then you do the addition and subtraction in one step from left to right.
What is the exponent operator in Python
**
How do you round a floating point number in Python (e.g. 1.73)
round()
round(1.73) gives the answer 2
How can we output a variable in the middle of some text in a print statement
e.g.
my_height = 180
print the statement
my height is (my_height) cms
print(f”some text {variable_name} more text”)
print(f”my height is {my_height} cms”)
How can we create a string with a variable in it?
f-string can be used to do this
e.g text_to_output = f”my height is {my_height} cms”
The text is substituted in when the command is executed - if the variable subsequently updates to a new value, the f-string is not implicitly updated with the new value
How can we create a string with a variable in it that overcomes f string limitation to dynamically update.
Every object has a .format method e.g.
hilarious = False
joke_evaluation=”Is that funny? {}”
joke_evaluation.format(hilarious)
=> ‘Is that funny? False’
If hilarious is updated to True then you get
joke_evaluation.format(hilarious)
=> ‘Is that funny? True’
What does the following code do
print(“my name is “,end=’’)
print(“Hans Solo”)
The end = ‘’ argument will not create an auto LF/CR at the end of the print statement.
The second print statement is on the same line as the first.
e.g.
my name is Hans Solo
is output
Using end = ‘ ‘ or end = ‘,’ would place a space or , at the end of the first print statement or any specified delimiter text
What is output?
welcome_text = “Welcome to {} in the {}”
print(welcome_text.format(“LA”,”USA”))
print(welcome_text.format(“London”,”UK”))
Welcome to LA in the USA
Welcome to London in the UK
What is the code for the following:
- define x = 43 and y = 51
- define a variable z to holds the text “You are x degrees of longitude and y degrees of latitude” where x and y are themselves variables
- print the value of z
x=43
y=45
z=”You are {} degrees longitude and {} degrees of latitude”
print(z.format(x,y))
What is the escape sequence that prints a new line e.g. how do we split
print(“line 1 line 2”)
line 1 line 2
so that there is a separate line between them when printed
The escape sequence for new line is \n (backslash n) so
print(“line 1 \nline 2”) gives
line 1
line 2
as output
How do we print text across multiple lines in a print statement (not using \n)
E.g so we get :
line 1 followed by line 2
in turn followed by line
line 3
finishing at line 4
Use “”” in the print statement so
print(“"”line 1 followed by line 2
in turn followed by line
line 3
finishing at line 4”””)
What is an escape sequence? and how do you put one into a string?
Escape sequences are used in strings for handling difficult-to-type characters.
There are different ways of starting an escape sequence - they can start with single quote - ‘ - double quote - “ - or backslash - .
How you would you use an escape sequence to print the sentence with a double quote “ escape character
I am 6’2” tall.
print(“I am 6’2" tall.”)
escape second double quote inside string
I am 6’2” tall.
The escape character tells Python the second double quote is part of the string
How you would you use an escape sequence to print the sentence with a single quote ‘ escape character
I am 6’2” tall.
print(‘I am 6'2” tall.’)
escape second quote inside string
I am 6’2” tall.
The escape character tells Python the second single quote is part of the string
How can you put a tab into a line of text with an escape sequence
\t
e.g.
“\tI’m tabbed in.”
What is the escape character to allow for text to be accepted as a series of lines
””” or ‘’’
e.g.
fat_cat = “””
I’ll do a list:
\t* Cat Food
\t* Fishies
\t* Catnip\n\t* Grass
“””