Python Practice Questions
What are escape characters?
"\" followed by string characters that would otherwise be difficult or impossible to type into code.
What does the | character signify in regular expressions?
"either, or" between two groups
What does ? signify in regular expressions?
"match zero or one of the preceding group" or signifies non-greedy matching.
How do you use the join() and split() functions? Use - as the seperator.
'-'.join('text') ('text').split('-')
'Hello, world!'[:5]
'Hello'
'-'.join('There can be only one.'.split())
'There-can-be-only-one.'
How would you check to see if 'cat' exists as a key in the dictionary?
'cat' in spam or 'cat' in spam.keys()
How would you check to see if 'cat' exists as a value in the dictionary?
'cat' in spam.values()
What does inputStr(limit=3, default='hello') do if blank input is entered three times?
'hello'
What are the three "mode" arguments that can be passed to the open() function?
'r' for read 'w' for write 'a' for append
How do you type the tuple value that has just the integer value 42 in it?
(42,)
What do + and * mean in regular expressions?
+ matches one or more. * matches zero or more.
What five functions and methods do you have to call to create a bar chart?
1. GET DATA openpyxl.chart.Reference(), 2. CREATE LEGEND openpyxl.chart.Series(), 3. CREATE CHART openpyxl.chart.BarChart(), 4. COMBINE LEGEND and CHART chartObj.append(seriesObj), 5. ADD CHART add_chart()
What are the steps to using regular expressions?
1. import re 2. var = re.compile(r'PATTERN) 3. match = var.search('text') or var.findall('text') 4. Call the match strings with print(match) or match.group()
What are the six comparison operators?
==, !=, <, >, <=, >=
Explain what a condition is and where you would use one.
A condition is an expression used in a flow control statement that evaluates to a Boolean value.
What data structure does a shelf value resemble?
A shelf value resembles a dictionary value; it has keys and values, along with keys() and values() methods that work similarly to the dictionary methods of the same names.
What is an expression made up of? What do all expressions do?
An expression is a combination of values and operators. All expressions evaluate to a single value.
Name a few ways that list values are similar to string values.
Both lists and strings can use... len( ), indexes and slices, for loops, be concatenated or replicated, in and not in operators.
In C:\bacon\eggs\spam.txt, which part is the dir name, and which part is the base name?
C:\bacon\eggs is the dir name, while spam.txt is the base name.
Why are functions advantageous to have in your programs?
Functions reduce the need for duplicate code. This makes programs shorter, easier to read, and easier to update.
In the regex created from r'(\d\d\d)-(\d\d\d-\d\d\d\d)', what does group 0 cover?
Group 0 is the entire match
When does findall() returns a list? a tuple?
If the regex has no groups = list. If there are groups = tuple.
What does it mean for a regular expression to be greedy?
In ambiguous situations Python will match the longest string possible. ex. a search for HaHa in HaHaHaHa will return HaHaHaHa
What is the difference between lists and tuples?
Lists are mutable and use [ ]. Tuples are immutable and use ( ).
What does the search() method return?
Match objects
What do the \D, \W, and \S mean in regular expressions?
NOT a single digit, NOT word, or NOT space character, respectively.
If a function does not have a return statement, what is the return value of a call to that function?
None
What is the data type of None?
NoneType
What is an absolute path?
Paths that start with the root folder like / or C:\.
Why are raw strings often used when creating Regex objects?
Raw strings are used so that backslashes do not have to be escaped.
Variables that "contain" list values don't actually contain lists directly. What do they contain instead?
References to list values.
What is a relative path relative to?
Relative paths are relative to the current working directory.
What does inputStr(limit=3) do if blank input is entered three times?
RetryLimitException
What is break?
The break statement will move the execution outside and just after a loop.
What is continue?
The continue statement will move the execution to the start of the loop.
What is the main difference between a dictionary and a list?
The items stored in a dictionary are unordered, while the items in a list are ordered.
What happens to variables in a local scope when the function call returns?
The local scope is destroyed, all the variables forgotten.
What is the difference between the read() and readlines() methods?
The read() method returns the file's entire contents as a single string value. The readlines() method returns a list of strings, where each string is a line from the file's contents.
How many global scopes are there in a Python program? How many local scopes?
There is one global scope, and a local scope is created whenever a function is called.
What are the two values of the Boolean data type?
True and False
If you want to retrieve the result of a cell's formula instead of the cell's formula itself, what must you do first?
When calling load_workbook(), pass True for the data_only keyword argument.
Where spam contains the list ['a', 'b', 'c', 'd']: What does spam[:2] evaluate to?
['a', 'b']
What is syntax to match all numbers and lowercase letters?
[0-9a-z] or [a-z0-9]
Where bacon contains the list [3.14, 'cat', 11, 'cat', True]: What does bacon.remove('cat') make the list value look like?
[3.14, 11, 'cat', True]
What does the openpyxl.load_workbook() function return?
a Workbook object
What does the wb.sheetnames workbook attribute contain?
a Worksheet object
What do the \d, \w, and \s mean in regular expressions?
a single digit, word, or space character, respectively.
What are the three Boolean operators?
and, or, and not
How would you retrieve the cell's row and column?
cell.row and cell.column
What is the difference between copy.copy() and copy.deepcopy()?
copy of a list vs. copies of list and lists inside the list
What statement creates a function?
def spam( ):
How can you prevent a program from crashing when it gets an error?
def spam(divideBy): try: return 42 / divideBy except ZeroDivisionError: print('Error: Invalid argument.')
What are two ways to remove values from a list?
del and remove( )
What is [ ]?
empty list value
Write a short program that prints the numbers 1 to 10 using a for loop.
for i in range(1, 11): print(i)
How can you force a variable in a function to refer to the global variable?
global
Python's regular expressions are greedy or non-greedy by default?
greedy
Write a short program that prints the numbers 1 to 10 using a while loop.
i = 1 while i <= 10: print(i) i = i + 1
How do you import the module that helps with input validation?
import pyinputplus as pyip
What are the pyinputplus functions?
inputStr() inputNum(), inputInt(), inputFloat() inputChoice() inputMenu() inputDateTime() inputYesNo() inputBool() inputEmail() inputFilepath() inputPassword()
What three functions can be used to get the integer, floating-point number, or string version of a value?
int(), float(), and str()
What do the sheet.max_column and sheet.max_row sheet attributes hold?
integer values for the max column and row
Name three data types.
integers, floating-point numbers, and strings
How can you trim whitespace characters from a string?
lstrip() and rstrip()
If you needed to get the integer index for column 'M', what function would you need to call?
openpyxl.cell.column_index_from_string('M')
If you needed to get the string name for column 14, what function would you need to call?
openpyxl.cell.get_column_letter(14)
How can you create Excel bar, line, scatter, and pie charts?
openpyxl.chart. BarChart() LineChart() ScatterChart() PieChart()
How do you get an Excel Workbook?
openpyxl.load_workbook()
What are the functions to get and change the current working directory?
os.getcwd() os.chdir()
What module and function can be used to "pretty print" dictionary values?
pprint.pprint()
How can you ensure that the user enters a whole number between 0 and 99 using PyInputPlus?
pyip.inputint(min=0, max=99)
What are three ways to call the numbers 0-9?
range(10), range(0, 10), and range(0, 10, 1)
How do you make a regular expression case-insensitive?
re.I or re.IGNORECASE as the second argument to re.compile()
What is the function that creates Regex objects?
re.compile()
What string methods can you use to right-justify, left-justify, and center a string?
rjust(), ljust(), and center()
How would you hide column C?
sheet.column_dimensions['C'].hidden = True
How would you set the height of row 5 to 100?
sheet.row_dimensions[5].height = 100
How can you retrieve a tuple of all the Cell objects from A1 to F1?
sheet['A1':'F1']
How would you set the value in the cell C5 to "Hello"
sheet['C5'] = 'Hello' or sheet.cell(row=5, column=3).value = 'Hello'
How would you retrieve the value in the cell C5?
sheet['C5'].value or sheet.cell(row=5, column=3).value
What do these escape characters represent? \' \" \t \n \\
single quote double quote tab newline backslash
What is the difference between the append() and insert() list methods?
spam.append('moose') spam.insert(1, 'chicken')
If you had a function named bacon() inside a module named spam, how would you call it after importing spam?
spam.bacon()
What is a shortcut for the following code? if 'color' not in spam: spam['color'] = 'black'
spam.setdefault('color', 'black')
Assign the value 'hello' as the third value in a list stored in a variable named spam?
spam[2] = 'hello'
How do you create multiline strings withOUT \n?
triple quotes: print('''Dear Alice, Hello. Sincerely, Bob''')
How would you retrieve the active Excel sheet?
wb.active
How would you save the workbook to the filename example.xlsx?
wb.save('example.xlsx')
How would you retrieve an Excel sheet named 'Sheet1'?
wb['Sheet1']
What does a dictionary value with a key 'foo' and a value 42 look like?
{'foo': 42}