Final Exam - Python Coding Study Guide
What character(s) is used at the start of a line in Python to indicate that what follows should be treated as a comment?
#
What will be the result of the following Python 3 statement? temperature = "3" + "5"
' 35 '
What results from "3" + "5"? Why?
' 35'. Because they are surrounded by quotation marks, the interpreter treats the values as letters and concatenates them.
What is if-elif?
- if statement can be used together with one or more elif statements to set multiple tests. - The code block related to the test that is satisfied will be executed. - If none of the conditions is met, then either the else code block will be executed, or if there is no else code block, none of the conditional code blocks will be executed.
What is the difference between .append and . extend?
.append adds the string value "arthropod" to the end of the list, while .extend treats the string "bird" as a sequence of values and adds each character of the string as a separate value.
What is the output of the following code in Python 3? print(1 and 0)
0
What will be the output of the following code in Python 3? number = 6 number = number + 4 lucky_number = 10 * number print(lucky_number/number)
10
What is 6 + 10 / 2? What is it called when calculation is correct but not meant for this result?
11. Semantically Incorrect.
What is the output of the following code in Python 3? print(17%5)
2
If we have just 1 + 1 what are the outcome in command prompt (Shell Mode) and script?
2 and *blank*
What is the output of the following code? def factorial(n): if n == 1: return 1 else: return n * factorial(n-1) print(factorial(3))
6
What is the purpose for the command, return?
A function can return a value.
Python interpreter always treats the values the user inputs at the keyboard as?
A string.
Consider the following: lucky_number = 42 print(lucky_number) 42 What is lucky_number in this case?
A variable: gives a name to a value.
Which of the following is a type in Python? int bool list All of the above
All of the above
Consider the following: lucky_number = lucky_number * 2 print(lucky_number) 84 What happened?
Assigned value of lucky_number * 2 to variable lucky_number. This changes the value of the variable lucky_number. Variable names are case sensitive.
Behaves much like a list, but permits non-integer types to be used as the indices. Can be thought of as a set of non-sequential key-value pairs.
Dictionary
What is the basic syntax of a Method?
Dot notation: <object>.<method-name>(<parameter1>, <parameter2>, ...) Object is typically a variable name
Define for loop and its structure:
Execute a code block for a specified number of times. Structure: for <variable> in <iterable>: <code block> The <variable> is used as a counter that controls how many times the code block is executed. The <iterable> is a sequence of values such as a list or a string. The <variable> takes each successive value of the <iterable>.
Define if statement and its structure:
Executes a statement block based on results of a test expressed by a conditional statement Structure: if <test>: <code block> elif <test>: <code block> else: <code block>
A fragment of code that causes a data type to be evaluated?
Expression
Any fragment of code that causes a data type to be evaluated
Expression
This control structure in Python executes a code block for a specified number of times?
For loop
A sequence of instructions that can be called by name?
Function
This control structure in Python executes a code block based on the results of a test expressed by a conditional statement?
If statement
The value at that point in the sequence
Indexing
What are the different types in Python?
Integer, Floating Point Number, String, Boolean, List
Define while loop and its structure:
Keeps executing a code block while the test is True while <test>: <code block>
Declared by assigning a sequence of values between square brackets to a variable.
List. Values in a list can be of any data type including integers, strings, or a mix of data types including other lists.
Takes an argument and returns a value
Method
A programming technique in which a function calls itself one or more times?
Recursion
Standard language for managing and querying relational databases?
SQL
Function
Sequence of instructions that can be called by a name
Operators
Special symbols that represent operations that can be performed on data. Each data type supports a set of operations. Some are supported by multiple different data types, but the results of the operation may differ.
A single unit of code that the Python interpreter can execute?
Statement
Single unit of code that the Python interpreter can execute
Statement
What is the difference/similarities between string and list
String data types consist of a sequence of characters. They are similar to the list data type. The main difference between the two is that a list can contain any data type, while string can only contain characters.
Data type for text and contained within quotation marks
String; can consist of: Letters (both uppercase and lowercase); numbers; special characters; spaces; or combo of above
Here is an example of a script. What does this script do? def factorial(n): if n == 1: return 1 else: return n * factorial(n‐1) print(factorial(5))
This is an example of recursion.
What is the purpose of indentation?
To define blocks of code and at the same time enforce good coding practice. Each change in indentation is used to determine when a code block starts or stops.
What is the output of the following code in Python 3? print(5>= 5)
True
Scope
Variables used in a function that are local to the function. A variable is local to that function so it cannot be referenced outside of the function. If you want to the function to return a value, you need to use the return command.
Here is an example of a script. What does this script do? def lucky_number(): import random x = random.randrange(10) print("Your lucky number is: ",x) lucky_number()
When a function calls other functions.
This control structure in Python keeps executing a code block while the test is True?
While loop
What is the purpose for the command, def?
You can create your own functions. The same rules apply for function names as for variable names. A function that takes no arguments needs to be defined with empty parentheses.
animal = ["mammal", "reptile", "fish", "bird", "insect"] animal[2:4]
['fish','bird']
What is the output of the following code in Python 3? animals = ["mammal","reptile","insect","fish"] animals.insert(3,"bird") print(animals)
['mammal','reptile','insect','bird','fish']
Python keyword used to create a user function?
def
What is if-else?
if statement can be used together with else
greeting = "hello world" greeting[2:4]
ll
Which of the following Python 3 instructions will produce the output Hello World? Print("Hello World") print "Hello World" print "Hello World" print("Hello World")
print("Hello World")
What is another type of <iterator>? What does this do?
range() function Will iterate over a sequence of numbers range(n) This function will generate an iterator starting with 0 and ending at n‐1. You can also specify a starting position and an increment for each iteration: range(<start>, n,<increment>)
vegetable = ["tree", "flower"] for i in vegetable: print(i)
tree flower Iteration addresses the values in the list sequentially
Items in a dictionary are...
unordered so they cannot be referenced using their ordinal position...the actual order of the items is unpredictable.
What is the output of the following code in Python 3? print("Ozymandius"[2:4])
ym