lOMoARcPSD|60412687
Global AI Hub – Python syntax essentials
MIS (Boğaziçi Üniversitesi)
Scan to open on Studocu
Studocu is not sponsored or endorsed by any college or university
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
ASSESMENT 1 OF 6
AI7020 Assessment – Python syntax
essentials
March 4, 2022
In order to continue with the course, you need to pass the assessment
with a minimum score of 60%. You have unlimited number of attempts.
What is the correct file extension for Python files?
.p
.pl
.py
.python
Correct
The file extension of the scripts you create with Python is .py. With this
Python-specific extension, your scripts will be detected by your
computer as Python files.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What will be displayed on the console when the inputs to name and age
are “Ali” and “15”?
Ali15
15Ali
15 Ali
SyntaxError
Correct
In Python, we can add multiple string values together. For this, it is sufficient to
use the “+” operator. In the above operations, when the expressions are put
together and pressed to the screen, the 15Ali value will be printed on the screen.
Which one is not a keyword in Python?
return
date
except
print
Correct
After Python 2 in Python 3 and above versions, ‘printʼ has been removed
from Python as a keyword and included as a built-in function.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
How do you create a variable with the numeric value 5?
x=5
x == 5
x: 0
x(5)
Correct
Variables hold values. When assigning a value to a variable in Python,
programmers donʼt have to specify its data type because Python automatically
detects the class of the data type the value belongs to. All you have to do is
provide a variable name and assign a value to it with the equal sign (=). The
Python interpreter caches the value and determines the type of the variable.
What is the correct syntax to output the type of a variable or object in
Python?
print(typeof x)
print(typeOf(x))
print(Type(x))
print(type(x))
Correct
The type() method returns the class type of the argument, which can be a value
or a variable, passed as a parameter. And remember, Python is a case-sensitive
language so “Type” and “type” will not be interpreted as the same thing.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which one is NOT a proper variable identifier?
_myvar
My_var
Myvar
my-var
Correct
In general programming languages do not use the dash (-) when
creating a variable. If we want to use it, the programming language will
perceive it as subtraction. For this reason, we should never use the dash
when creating variables in Python.
What is the data type of “(name*a)”?
bool
str
int
float
Correct
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
When you perform a mathematical operation with a string and a number
in Python you will end up with a string. This is because multiplying
strings by integer numbers creates a repetitive sequence of the string.
Try this for yourself in Colab.
What is the value of x after the following statements?
Correct
We add 1 to the variable y and multiply it with the current value of x which is
2, Then we assign the result of this operation to the value x itself. It is same
as ” x = x * (y + 1) “.
How do you insert comments in Python code?
//This is a comment
#This is a comment
/*This is a comment */
%This is a comment
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Correct
Comments start with a hash sign (#) and Python ignores them while
executing the typed code. For this reason, the comments created do not
cause any errors.
What is the correct type conversion that solves the error below?
print(a + int(b))
print(str(a) + b)
print(int(a) + b)
print(int(a) + str(b))
Correct
Mathematically, a character and a number cannot be added together. If you
want to do this with Python, you will encounter the “TypeError” error. The
data type of variable “a” is a string. After converting this variable to
integer, we will not get any errors in our code when we perform addition.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
ASSESMENT 2 OF 6
AI7030 Assessment – Flow controls
March 4, 2022
In order to continue with the course, you need to pass the assessment
with a minimum score of 60%. You have unlimited number of attempts.
What will be the output of this code?
"54 is found in the list"
"54 is not found in the list"
Index out of Bound Exception.
No Output. Infinite Loop.
Correct
“No Output” is correct. The program is stuck in the infinite loop because
the index variable i is not incremented, or increased, and “mylist[i] ==
54” always checks the first element of mylist with index 0.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which one is the output of the following code?
Zero
Positive Number
Negative Number
Null
Correct
“Positive Number” will be printed on the screen because “3.4 > 0” is
True and the other statements will not be executed.
Which keyword is used to stop a loop?
pass
exit
break
finally
Correct
Keyword “break” is correct. A break statement is used to stop a loop.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
How do you start writing an if statement in Python?
if x > y –
if(x > y)
if x > y:
if (x >y){
Correct
The syntax “if x > y:” is correct because a colon is needed to indicate
the beginning of a block, but we don’t need to put the variables in
parentheses.
How do you start writing a for loop in Python?
for i in mylist:
for x > y:
for each i in y:
for(i =0; i < 5; i++){:
Correct
“for i in mylist:” is correct. A for loop in Python uses the keyword ‘forʼ
followed by a variable, the keyword ‘inʼ and an iterable object to loop
over. In this case, our variable is ‘iʼ and ‘mylistʼ is the iterable object.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which one is the output of the following code?
Correct
For the first if statement, “not x” and “y” are both false, so the next elif
statement is checked. “Not x” is false and “z” is also false making the
statement false. In the third statement, “not y” and “x” is true, which
makes the statement true. So, 3 will be printed. The last else statement
will not be executed.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which one is the output of the following code if we enter 50 for score?
Nice, but you should still work harder.
Congratulations!
Invalid Score
Not good, you should work harder.
Correct
“Not good, you should work harder.” will be printed because “50 <= 50”
is True and the other statements will not be executed.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What is the output of this code?
Ista
Error! NameError
Correct
“Error! NameError” is correct. The statement “x =+ 1” can also be
written as “x = x + 1”. It means that variable x is called before it is
defined. Thus, it will give NameError.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What will be the output of the following code?
Infinite Loop
Correct
Option “1 3” is correct because when the loop starts, we first print the
initial value of “I” which is “1”, then we add 2 to I and make it 3, which
will be printed in the next turn of the loop. But before 5 is printed the
loop is exited.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which one of the following is the output of this code?
Correct
“1” is correct. This program finds the biggest number in the list and in
this case, the biggest number is 5. The program then prints the element
at index 5 which is 1.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
ASSESMENT 3 OF 6
AI7040 Assessment – Functions
March 4, 2022
In order to continue with the course, you need to pass the assessment
with a minimum score of 60%. You have unlimited number of attempts.
Which one of the following is the correct syntax to import the Math
module in Python?
import math
import math from modules
#include math.h
from math include math library
Correct
It is enough to type “import math” to import the Math module.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
You have defined a function named preprocess() in the [Link] file. How
can you use this in your notebook?
[Link]()
#include utils
preprocess() using utils
using utils
[Link]()
import utils
preprocess()
Correct
To use a module, we need to import it first using import and write the file
name. Then we use a dot followed by the function name to call it.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
brackets are used for other things, which weʼll learn later in the course.
Which one of the following functions can be used to convert the string
“7” to an integer 7?
int
integer
atoi
Float
Correct
The function “int” is used to change the datatype to integer.
How can we identify which lines of code belong to a function?
Curly Brackets {}
Square Brackets []
Indentation
All of the above
Correct
Indentation is the correct answer. The Python compiler identifies
different code blocks with indentation. Curly brackets are used in other
programming languages like C++, C# as well as JavaScript, and square
brackets are used for other things, which weʼll learn later in the course.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
In the following code, what is the value of the amount variable?
Error
Correct
We will get an error. The function myproduct only prints the product and does
not return it. Therefore, the type of myproduct(3,2) is None. The equation will
give TypeError because you cannot add an integer to a NoneType.
The Seaborn library is used for which of the following purposes?
Solving mathematics equations
Data visualization
Date and time representation
Converting nautical miles to miles
Correct
The Seaborn library is used for data visualization or generating
statistical graphs in Python. It is build on top of matplotlib and can be
integrated with pandas data structures.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
In the following code, what is the type of the value of the amount
variable?
Error!
Correct
‘7ʼ is correct. The value of myproduct(3,2) is 6. When you add 1, the
amount becomes 7.
What will be printed when the following code is executed?
4
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Correct
The keyword argument d is changed but c remains the same. When c is
compared with the d in the if statement, it is True and variable a is
printed.
Which one of the following is the correct way to define a function in
Python?
function sum(num1, num2)
public int sum(num1, num2)
def sum(num1, num2):
define sum(num1, num2):
Correct
In Python, a function is defined by using the “def” keyword followed by
the function name, then the arguments, and a colon at the end.
What will be printed on the screen when the following code is executed?
1500
1200
Error!
No Output
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Correct
1200 is correct. The price variable is out of the scope of the function
update_price. Therefore, the price variable is not changed even when
the function is called.
What will be the output of the following code?
"This is foo1"
"Hello World"
Error
No output
Correct
The function does not give any output. The function definition is correct,
but it is only defined but never called.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What is the output of the following code?
12
16
Correct
16 is correct. The num1 variable will be defined within the scope of the
function, and global num2 variable will be used to compute the sum.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
ASSESMENT 4 OF 6
AI7050 Assessment – Data structures
March 4, 2022
In order to continue with the course, you need to pass the assessment
with a minimum score of 60%. You have unlimited number of attempts.
What will be the output of the following code?
('Bananas', 'Eggs', 'Bread', 'Jelly')
('Apples', 'Bananas', 'Bread', 'Jelly')
('Apples', 'Eggs', 'Bread', 'Jelly')
Error
Correct
Notice the round brackets. The round brackets indicate the definition of
a tuple, and tuples are immutable. Therefore, replacing the item at the
first index will cause an error.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
We want to extract “AI” from mystr which is defined as mystr = “Hello AI
World”. What will be the correct indexes for slicing mystr?
mystr[6 8]
mystr[6:9]
mystr[7:10]
mystr[7:9]
Correct
The letters A and I are the 7th and 8th characters in the string. Since the
index starts from 0, these letters have indexes 6 and 7. The character at
the starting index will be included in the slice, but the character at the
ending index won’t. Thus, [6:8] is correct.
You assign new_price = 20 and the datatype of new_price is an integer.
How can you change it to float without changing its value?
price = price * 1.0
price = price / 1.0
price = float(price)
All of the above
Correct
Arithmetic operations between integers and floats result in a float type.
Therefore, answer options a. and b. are correct. The float() function
converts the datatype to float, making option c. also correct.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What is the output of the following code?
[1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[1, 4, 9, 16, 25]
[0, 1, 4, 9, 16]
Correct
The range function creates a list of numbers starting from 0. new_series
is a list comprehension definition that iterates over all the elements of
series and takes its square. So, new_series consists of the squared
values of series.
In Python, how do you define an empty dictionary named sample_dict?
sample_dict = {}
sample_dict = dict()
All of the above
None of the above
Correct
In Python, you can use curly braces or the dict() function to define a
dictionary.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which one of the following data structures is immutable?
List
Dictionary
Tuple
String
Correct
You can change the contents of the list and dictionary. You can also edit the
individual characters of the string. However, you cannot change a tuple after
it is defined. Therefore, tuples are immutable.
What will be printed to the screen when the following code is executed?
{'Alex': 11, 'Bella': 12, 'Chris': 12, 'Donna': 10}
{'Alex': 12, 'Bella': 13, 'Chris': 13, 'Donna': 11}
{'Alex': 12, 'Bella': 13, 'Chris': 14, 'Donna': 15}
Error
Correct
Even though age is incremented, the values are not reassigned to the
keys of the dictionary. This means that the value is just read from the
dictionary, not overwritten by the incremented value.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
In the following code, what will be the content of new_list?
[]
[2, 3, 4, 2]
[2, 5, 3, 4, 2]
Error
Correct
In the beginning, the new list is empty. The for loop iterates over all the
elements of list_of_numbers. The numbers that are less than 6 after 1 is
added to them are appended in the new list. Keep in mind that the
values of “number” are added, not the values of “inc_number”.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which dictionary method can we use to access the key/value pairs as a
list of tuples?
.items
.keys
.values
.both
Correct
When we call the .item method on a dictionary, keys and values are
returned as a list of tuples.
If you assign x = “0.2”, what will be the datatype of x?
int
float
string
None of the above
Correct
The single or double quotation marks indicate the datatype string.
Without the quotation marks the datatype of x would have been float.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
ASSESMENT 5 OF 6
AI7060 Assessment – Files and errors
March 4, 2022
In order to continue with the course, you need to pass the assessment
with a minimum score of 60%. You have unlimited number of attempts.
Which one of the following is the correct syntax to open a file [Link]
that is in the current directory?
[Link]
[Link](txt)
open(“[Link]”)
open([Link])
Correct
Python has a built-in function open(). If the file is in the current directory, it
is enough to put the file name as an argument. If the mode is not specified
in the arguments, Python will open the file in read mode by default.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What will be the result of the following program if there is only one file
called [Link] in the current directory?
[Link] will be opened and 0 will be written in it
[Link] will be created and 1 will be written in it
[Link] be will opened and 0 will be written in it
[Link] will be created and 1 will be written in it
Correct
When we open a file without specifying the mode, Python opens the file
in read mode by default. When we try to write something into a file that
is opened in read mode, it throws an exception. Therefore, [Link]
will be created and 1 will be written in it.
Which symbol is used for opening a file in writing mode?
‘rʼ
‘wʼ
‘xʼ
‘+ʼ
Correct
The symbol ‘wʼ is used to open a file in write mode.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Consider the function foo1. What will be the output if foo1(1, 2) is called?
3.0
4.0
“NewException”
“ZeroDivisionError”
Correct
There is no exception in this code because the integers 1 and 2 are valid
arguments for the function. The result of the calculation
num1+num2+0/1 will be returned. Notice that 0/1 is not a division by zero, it
results in 0.0, which is a float. When we add 0.0 to the integer 3 it will return
the float 3.0.
When is the except block executed in a program?
When an exception is thrown anywhere in the program
When an exception is thrown in the try block
Only when there are no exceptions in the program
Always
Correct
The except block is only executed when there is an exception in the try block.
If there is an exception in other parts of the program, the program will still
give an error.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What will be the output of the following code?
t3
e3
e4
No Output
Correct
There will be a TypeError exception when we try to add the integer 1 with
the string ‘3ʼ. Therefore, the program runs the except block. Here, the string
‘3ʼ is converted to an integer, and then the print statement is executed
which outputs e 4.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What will happen if you run the following code and there is no file called
[Link] in the current directory?
FileNotFoundError
It creates a new file “[Link]”
Nothing happens
None of the above
Correct
When we try to open a file that does not exist in write mode, Python creates
the file itself.
Which one of the following functions is used to write the string “Radio Gaga”
in a file that was opened in write mode and assigned to the variable queen?
queen = open(“Radio Gaga”, ‘w’)
write_on_queen(“Radio Gaga”)
queen.w(“Radio Gaga”)
[Link](“Radio Gaga”)
Correct
The file was already opened in write mode and assigned to the variable
queen. The .write() function is used to write in a file.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which error do you expect to encounter when you run the following
code?
NameError
TypeError
IndexError
No Error. ‘b’ will be printed.
Correct
Notice that we defined a variable “my_string” with an underscore, but not
“mystring”. Because of this typo, the code will throw a NameError.
What happens when we exit a code block where a file is opened using the
keyword with?
Next code block related to the file is executed
It loops over the file one more time
The file is closed
It prints files that are used
Correct
If a file is opened using the keyword with, it will be closed once we exit the
with-block. Using with is a safe way to make sure that the files we open in
our code will be closed when we are done using them.
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
ASSESMENT 6 OF 6
AI7000 Final Assessment
March 4, 2022
How do you create a variable with the numeric value 5?
x=5
x == 5
x: 0
x(5)
Which one is NOT a proper variable identifier?
_myvar
My_var
Myvar
my-var
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
When is the except block executed in a program?
When an exception is thrown anywhere in the program
When an exception is thrown in the try block
Only when there are no exceptions in the program
Always
You have defined a function named preprocess() in the [Link] file. How
can you use this in your notebook?
import utils
[Link]()
#include utils
preprocess() using utils
using utils
[Link]()
import utils
preprocess()
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
Which one of the following is the correct way to define a function in
Python?
function sum(num1, num2)
public int sum(num1, num2)
def sum(num1, num2):
define sum(num1, num2):
How do you start writing an if statement in Python?
if x > y –
if(x > y)
if x > y:
if (x >y){
Which symbol is used for opening a file in writing mode?
‘r’
‘xʼ
‘+ʼ
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What is the output of the following code?
[1, 2, 3, 4, 5]
[0, 1, 2, 3, 4]
[1, 4, 9, 16, 25]
[0, 1, 4, 9, 16]
Which one of the following is the correct syntax to open a file [Link]
that is in the current directory?
[Link]
[Link](txt)
open(“[Link]”)
open([Link])
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
How do you start writing a for loop in Python?
for i in mylist:
for x > y:
for each i in y:
for(i =0; i < 5; i++){:
Which one of the following data structures is immutable?
List
Dictionary
Tuple
String
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
What will be the output of the following code?
Infinite Loop
We want to extract “AI” from mystr which is defined as mystr = “Hello AI
World”. What will be the correct indexes for slicing mystr?
mystr[6 8]
mystr[6:9]
mystr[7 10]
mystr[7 9]
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])
lOMoARcPSD|60412687
How can we identify which lines of code belong to a function?
Curly Brackets {}
Square Brackets []
Indentation
All of the above
What happens when we exit a code block where a file is opened using the
keyword with?
Next code block related to the file is executed
It loops over the file one more time
The file is closed
It prints files that are used
Downloaded by Beytullah AYVAT (beytullahayvat00@[Link])