Font size
Worksheetsw3schools python
Total questions: 115
Worksheet time: 58mins
What is the correct file extension for Python files?
.pp
.pt
.py
What is a correct command line syntax for checking if python is installed on your computer? (And also to check the Python version)
python --version
python ##version
python version
What is a correct syntax to exit the Python command line interface?
exit()
stop()
end()
True or False: Indentation in Python is for readability only.
True
False
Which character is used to define a Python comment:
'
//
#
/*
What is a correct way to declare a Python variable?
var x = 5
$x = 5
x = 5
#x = 5
True or False:
You can declare string variables with single or double quotes.
x = "John"
# is the same as
x = 'John'
True
False
True or False:
Variable names are not case-sensitive.
a = 5
# is the same as
A = 5
True
False
Which is NOT a legal variable name?
my-var = 20
my_var = 20
Myvar = 20
_myvar = 20
What is a correct syntax to add the value 'Hello World', to 3 variables in one statement?
x, y, z = 'Hello World'
x = y = z = 'Hello World'
x|y|z = 'Hello World'
Consider the following code:
fruits = ['apple', 'banana', 'cherry']
a, b, c = fruits
print(a)
What will be the result of a
apple
cherry
banana
Consider the following code:
print('Hello', 'World')
What will be the printed result?
Hello, World
Hello World
HelloWorld
Consider the following code:
a = 'Hello'
b = 'World'
print(a + b)
What will be the printed result?
a + b
Hello World
HelloWorld
Consider the following code:
a = 4
b = 5
print(a + b)
What will be the printed result?
45
9
4+5
Consider the following code:
x = 'awesome'
def myfunc():
x = 'fantastic'
myfunc()
print('Python is ' + x)
What will be the printed result?
Python is awesome
Python is fantastic
Consider the following code:
x = 'awesome'
def myfunc():
global x
x = 'fantastic'
myfunc()
print('Python is ' + x)
What will be the printed result?
Python is awesome
Python is fantastic
If x = 5, what is a correct syntax for printing the data type of the variable x?
print(dtype(x))
print(type(x))
print(x.dtype())
Which is NOT a legal numeric data type in Python:
int
long
float
What will be the result of the following code:
print(int(35.88))
35
35.88
36
What will be the result of the following code:
print(float(35))
35
35.0
0.35
What will be the result of the following code:
print(str(35.82))
35
35.8
35.82
What will be the result of the following code:
x = 'Welcome'
print(x[3])
Wel
c
Welcome Welcome Welcome
What will be the result of the following code:
x = 'Welcome'
print(x[3:5])
lcome
come
com
co
What will be the result of the following code:
x = 'Welcome'
print(x[3:])
lcome
come
com
co
What is a correct syntax to print a string in upper case letters?
'Welcome'.upper()
'Welcome'.toUpper()
'Welcome'.toUpperCase()
What is a correct syntax to merge variable x and y into variable z?
z = x, y
z = x = y
z = x + y
What will be the result of the following code:
x = 'Welcome'
y = 'Coders'
print(x + y)
Welcome Coders
WelcomeCoders
Welcome
Coders
Consider this code:
a = 'Join'
b = 'the'
c = 'party'
What is a correct syntax to print 'Join the party'?
print(a + b + c)
print(a + ' ' + b + ' ' + c)
print(a b c)
If x = 9, what is a correct syntax to print 'The price is 9.00 dollars'?
print(f'The price is {x:.2f} dollars')
print(f'The price is {x:2} dollars')
print(f'The price is {x:format(2)} dollars')
What will be the result of the following code:
print(f'The price is {2 + 3} dollars')
The price is 23 dollars
The price is 5 dollars
The price is {2 + 3} dollars
The price is 6 dollars
What will be the result of the following syntax:
print(5 > 3)?
True
False
5>3
What will be the result of the following syntax:
x = 5
x += 3
print(x)
3
5
8
What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
print(mylist[1])
apple
banana
cherry
What will be the result of the following syntax:
mylist = ['apple', 'banana', 'banana', 'cherry']
print(mylist[2])
apple
banana
cherry
True or False.
List items cannot be removed after the list has been created.
True
False
What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
print(mylist[-1])
apple
banana
cherry
What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
mylist[0] = 'kiwi'
print(mylist[1])
apple
banana
cherry
kiwi
What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
mylist.insert(0, 'orange')
print(mylist[1])
apple
banana
cherry
orange
What is a List method for removing list items?
pop()
push()
delete()
What is a correct syntax for looping through the items of a list?
for x in ['apple', 'banana', 'cherry']:
print(x)
for x in ['apple', 'banana', 'cherry']
print(x)
foreach x in ['apple', 'banana', 'cherry']
print(x)
Consider the following code:
fruits = ['apple', 'banana', 'cherry']
newlist = [x for x in fruits if x == 'banana']
What will be the value of newlist?
['apple', 'cherry']
['banana']
True
What is a correct syntax for sorting a list?
mylist.orderby(0)
mylist.order()
mylist.sort()
What is a correct syntax for reversing the current order of a list?
mylist.sort(-1)
mylist.sort(reverse = True)
mylist.reverse()
What is a correct syntax for sorting a list descending?
mylist.sort(-1)
mylist.sort(reverse = True)
mylist.sort('desc')
What is a correct syntax for making a copy of a list?
list2 = list1
list2 = list1.copy()
list2.copy(list1)
What is a correct syntax for making a copy of a list?
list2 = list1
list2 = list1.list()
list2 = list(list1)
What is a correct syntax for making a copy of a list?
list2 = list1[:]
list2 = list1[]
list2 = list1[-1]
What is a correct syntax for joining list1 and list2 into list3?
list3 = join(list1, list2)
list3 = list1 + list2
list3 = [list1, list2]
What is a correct syntax for adding elements from list2 into list1?
list1.extend(list2)
list1.join(list2)
list1.push(list2)
Consider the following code:
list1 = ['a', 'b' , 'c']
list2 = [1, 2, 3]
for x in list2:
list1.append(x)
What will be the value of list1?
['a', 'b', 'c', 1, 2, 3]
[1, 2, 3]
['a', 1, 'b', 2, 'c', 3]
Which one of these is a tuple?
thistuple = ('apple', 'banana', 'cherry')
thistuple = ['apple', 'banana', 'cherry']
thistuple = {'apple', 'banana', 'cherry'}
True or False.
Tuple items cannot be removed after the tuple has been created.
True
False
You can access tuple items by referring to the index number, but what is the index number of the first item?
-1
0
1
You cannot change the items of a tuple, but there are workarounds. Which of the following suggestion will work?
Convert tuple into a list, change item, convert back into a tuple.
Convert tuple into a set, change item, convert back into a tuple.
Convert tuple into a dictionary, change item, convert back into a tuple.
Which is a correct syntax to delete a tuple?
delete mytuple
mytuple.delete()
del mytuple
True or False. You are allowed to add a tuple to an existing tuple.
True
False
Consider the following code:
fruits = ('apple', 'banana', 'cherry')
(x, y, z) = fruits
print(y)
What will be the value of y?
apple
banana
cherry
Consider the following code:
fruits = ('apple', 'banana', 'cherry')
(x, *y) = fruits
print(y)
What will be the value of y?
banana
['banana', 'cherry']
banana, cherry
Consider the following code:
fruits = ('apple', 'banana', 'cherry', 'mango')
(x, *y, z) = fruits
print(y)
What will be the value of y?
['banana', 'cherry']
['banana', 'cherry', 'mango']
['cherry', 'mango']
Whis one of theese is a dictionary?
x = ('apple', 'banana', 'cherry')
x = {'type' : 'fruit', 'name' : 'banana'}
x = ['apple', 'banana', 'cherry']
True or False.
A dictionary cannot have two keys with the same name.
True
False
Which one of these is a set?
myset = ('apple', 'banana', 'cherry')
myset = ['apple', 'banana', 'cherry']
myset = {'apple', 'banana', 'cherry'}
True or False.
Set items cannot be removed after the set has been created.
True
False
What will be the result of the following code:
x = 5
y = 8
if x > y:
print('Hello')
else:
print('Welcome')
Hello
Welcome
Which statement is a correct syntax to break out of a loop?
end
return
break
What will be the result of the following code:
for x in range(3):
print(x)
0
1
2
0
1
2
3
1
2
3
What is the correct keyword for defining functions in Python?
function
func
def
What will be the result of the following code:
x = lambda a, b : a - b
print(x(5, 3))
15
8
3
2
True or False: Lambda functions can take multiple arguments.
True
False
True or False: Lambda functions can have multiple expressions.
True
False
Python Lists can be used as arrays.
What will be the result of the following code:
fruits = ['apple', 'banana', 'cherry']
print(fruits[0])
apple
banana
cherry
What is a correct Python List method used to return the number of elements in a list?
count()
len()
lenght()
What will be the result of the following code:
fruits = ['apple', 'bananan', 'cherry']
print(len(fruits))
1
2
3
4
When the class object is represented as a string, there is a function that controls what should be returned, which one?
init()
str()
return()
What is a correct syntax for deleting an object named person in Python?
person.delete()
delete person
del person
person.delete()
delete person
del person
What is the correct keyword to use inside an empty class, to avoid getting an error?
empty
inherit
pass
There are two methods that you have to implement when you create an iterator, which two?
iter() and next()
next() and prev()
init() and end()
Which statement can be used to stop the iteration?
break
stop
stopIteration
break
stop
stopIteration
True or False. Lists, tuples, dictionaries, and sets are all iterable objects.
True
False
True or False. One object cannot have a method with the same name as another object's method.
True
False
True or False. Methods with the same name, but for different objects, can have different content, but the return value has to be of same data type.
True
False
Which statement is true?
Polymorphism refers to methods/functions/operators with the same name that can be executed on many objects or classes.
Polymorphism refers to classes with the same name that performs different tasks.
Consider the following code:
x = 300
def myfunc():
x = 200
myfunc()
print(x)
What will be the printed result?
200
300
200300
Consider the following code:
x = 300
def myfunc():
x = 200
myfunc()
print(x)
What will be the printed result?
200
300
200300
Which statement keyword can be used for variables inside nested function?
local
nonglobal
nonlocal
Consider the following code:
import datetime
x = datetime.datetime.now()
Which syntax will print the name of the weekday?
print(x.strftime('%A'))
print(x.ftime('%A'))
print(x.fdate('%A'))
When formatting date objects into readable strings, which syntax is used to return the month name, full version?
print(x.strftime('%B'))
print(x.strftime('%M'))
print(x.strftime('%N'))
Consider the following code:
import datetime
x = datetime.datetime(2024, 8, 20)
print(x.strftime('%d'))
What will be the printed result?
19
20
21
Consider the following code:
print(max(5, 10, 25))
What will be the printed result?
5
10
25
Consider the following code:
print(pow(2, 3))
What will be the printed result?
2
4
8
6
When using the built-in math module, what will be the printed result of the following code:
import math
print(math.sqrt(9))
3
9
81
When using the built-in math module, how can you return the number of PI?
math.pi
math.fpi
math.strpi
When you parse code with the json.loads() method, the result is returned as a specific Python data type, which one?
list
set
tuple
dictionary
Which method from the json library can be used to convert a Python object into a JSON string?
json.tojson()
json.dumps()
json.extract()
The json.dumps() method has a keyword parameter used to sort the result, what is it called?
order
sort_keys
arrange
Consider the following code:
import re
txt = 'The rain in Spain'
x = re.findall('[a-c]', txt)
print(x)
What will be the printed result?
['a', 'a']
'The rin in Spin'
2
Consider the following code:
import re
txt = 'The rain in Spain'
x = re.search('a', txt)
print(x.start())
What will be the printed result?
3
4
5
Consider the following code:
import re
txt = 'The rain in Spain'
x = re.search('\s', txt)
print(x.start())
What will be the printed result?
3
4
5
When using the re module to find a match, a match will return a Match object, but what is the return value when there is no match?
-1
None
0
Null
-1
None
0
Null
In the world of Pyhton, what describes PIP best?
PIP is a module used for drawing
PIP is a module used for handling large amounts of data
PIP is a package manager for Python modules
What is a correct way of importing the array module?
import array
include array
install array
In Command Line view, what is a correct statement for listing all the packages installed on your system?
pip dir
pip list
pip read
What is a function used for opening files?
load()
run()
open()
By default the file is opened in text mode, but you can also open the file in binary mode. Which one of the following syntaxes opens the file in binary mode?
x = bopen('demofile.txt')
x = open('demofile.txt', 'b')
x = open(b'demofile.txt')
True or False.
The default opening mode when opening a file with the open() function is 'r' for 'reading'.
True
False
After opening a file with the open() function, which method can be used to read the content?
list()
read()
show()
list()
read()
show()
To read only one line, we can use another method, which one?
readline()
lread()
readl()
readline()
lread()
readl()
True or False.
If you call the readline() method two times, it will return the two first lines.
True
False
What happens to the original file content if you open a file like this:
f = open('demofile3.txt', 'w')
The original content will be overwritten
Any new content will be added after the original content
If you open a file like this:
f = open('demofile3.txt', 'w')
What happens if the file does not exist?
It will return an error
A file will be created
Consider this code:
f = open('demofile3.txt', 'w')
What could you replace the 'w' with to instead return an error if the file already exists?
'x'
'b'
't'
To remove a file you can import the os module, but which function removes the file?
os.delete()
os.drop()
os.remove()
os.delete()
os.drop()
os.remove()
Which os function can be used to delete an entire folder?
os.rmdir()
os.rmfolder()
os.rmcatalog()
True or False. To remove a folder with the os module, it cannot contain any files
True
False
