wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

w3schools python

Total questions: 115

Worksheet time: 58mins

Name
Class
Date
1.

What is the correct file extension for Python files?


a)

.pp

b)

.pt

c)

.py

2.

What is a correct command line syntax for checking if python is installed on your computer? (And also to check the Python version)

a)

python --version

b)

python ##version

c)

python version

3.

What is a correct syntax to exit the Python command line interface?

a)

exit()

b)

stop()

c)

end()

4.


True or False: Indentation in Python is for readability only.

a)

True

b)

False

5.


Which character is used to define a Python comment:

a)

'

b)

//

c)

#

d)

/*

6.

What is a correct way to declare a Python variable?

a)

var x = 5

b)

$x = 5

c)

x = 5

d)

#x = 5

7.


True or False:
You can declare string variables with single or double quotes.
x = "John"
# is the same as
x = 'John'

a)

True

b)

False

8.

True or False:
Variable names are not case-sensitive.
a = 5
# is the same as
A = 5

a)

True

b)

False

9.

Which is NOT a legal variable name?

a)

my-var = 20

b)

my_var = 20

c)

Myvar = 20

d)

_myvar = 20

10.

What is a correct syntax to add the value 'Hello World', to 3 variables in one statement?

a)

x, y, z = 'Hello World'

b)

x = y = z = 'Hello World'

c)

x|y|z = 'Hello World'

11.

Consider the following code:
fruits = ['apple', 'banana', 'cherry']
a, b, c = fruits
print(a)
What will be the result of a

a)

apple

b)

cherry

c)

banana

12.

Consider the following code:
print('Hello', 'World')
What will be the printed result?

a)

Hello, World

b)

Hello World

c)

HelloWorld

13.

Consider the following code:
a = 'Hello'
b = 'World'
print(a + b)
What will be the printed result?

a)

a + b

b)

Hello World

c)

HelloWorld

14.

Consider the following code:
a = 4
b = 5
print(a + b)
What will be the printed result?

a)

45

b)

9

c)

4+5

15.


Consider the following code:
x = 'awesome'
def myfunc():
  x = 'fantastic'
myfunc()
print('Python is ' + x)
What will be the printed result?

a)

Python is awesome

b)

Python is fantastic

16.


Consider the following code:
x = 'awesome'
def myfunc():
  global x
  x = 'fantastic'
myfunc()
print('Python is ' + x)
What will be the printed result?

a)

Python is awesome

b)

Python is fantastic

17.

If x = 5, what is a correct syntax for printing the data type of the variable x?

a)

print(dtype(x))

b)

print(type(x))

c)

print(x.dtype())

18.

Which is NOT a legal numeric data type in Python:

a)

int

b)

long

c)

float

19.

What will be the result of the following code:
print(int(35.88))

a)

35

b)

35.88

c)

36

20.

What will be the result of the following code:
print(float(35))

a)

35

b)

35.0

c)

0.35

21.

What will be the result of the following code:
print(str(35.82))

a)

35

b)

35.8

c)

35.82

22.

What will be the result of the following code:
x = 'Welcome'
print(x[3])

a)

Wel

b)

c

c)

Welcome Welcome Welcome

23.

What will be the result of the following code:
x = 'Welcome'
print(x[3:5])

a)

lcome

b)

come

c)

com

d)

co

24.


What will be the result of the following code:
x = 'Welcome'
print(x[3:])

a)

lcome

b)

come

c)

com

d)

co

25.

What is a correct syntax to print a string in upper case letters?

a)

'Welcome'.upper()

b)

'Welcome'.toUpper()

c)

'Welcome'.toUpperCase()

26.

What is a correct syntax to merge variable x and y into variable z?

a)

z = x, y

b)

z = x = y

c)

z = x + y

27.


What will be the result of the following code:
x = 'Welcome'
y = 'Coders'
print(x + y)

a)

Welcome Coders

b)

WelcomeCoders

c)

Welcome
Coders

28.

Consider this code:
a = 'Join'
b = 'the'
c = 'party'
What is a correct syntax to print 'Join the party'?

a)

print(a + b + c)

b)

print(a + ' ' + b + ' ' + c)

c)

print(a b c)

29.

If x = 9, what is a correct syntax to print 'The price is 9.00 dollars'?

a)

print(f'The price is {x:.2f} dollars')

b)

print(f'The price is {x:2} dollars')

c)

print(f'The price is {x:format(2)} dollars')

30.

What will be the result of the following code:
print(f'The price is {2 + 3} dollars')

a)

The price is 23 dollars

b)

The price is 5 dollars

c)

The price is {2 + 3} dollars

d)

The price is 6 dollars

31.

What will be the result of the following syntax:
print(5 > 3)?

a)

True

b)

False

c)

5>3

32.


What will be the result of the following syntax:
x = 5
x += 3
print(x)

a)

3

b)

5

c)

8

33.

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
print(mylist[1])

a)

apple

b)

banana

c)

cherry

34.

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'banana', 'cherry']
print(mylist[2])

a)

apple

b)

banana

c)

cherry

35.

True or False.
List items cannot be removed after the list has been created.

a)

True

b)

False

36.

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
print(mylist[-1])

a)

apple

b)

banana

c)

cherry

37.

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
mylist[0] = 'kiwi'
print(mylist[1])

a)

apple

b)

banana

c)

cherry

d)

kiwi

38.

What will be the result of the following syntax:
mylist = ['apple', 'banana', 'cherry']
mylist.insert(0, 'orange')
print(mylist[1])

a)

apple

b)

banana

c)

cherry

d)

orange

39.


What is a List method for removing list items?

a)

pop()

b)

push()

c)

delete()

40.

What is a correct syntax for looping through the items of a list?

a)

for x in ['apple', 'banana', 'cherry']:
  print(x)

b)

for x in ['apple', 'banana', 'cherry']
  print(x)

c)

foreach x in ['apple', 'banana', 'cherry']
  print(x)

41.

Consider the following code:
fruits = ['apple', 'banana', 'cherry']
newlist = [x for x in fruits if x == 'banana']
What will be the value of newlist?

a)

['apple', 'cherry']

b)

['banana']

c)

True

42.

What is a correct syntax for sorting a list?

a)

mylist.orderby(0)

b)

mylist.order()

c)

mylist.sort()

43.


What is a correct syntax for reversing the current order of a list?

a)

mylist.sort(-1)

b)

mylist.sort(reverse = True)

c)

mylist.reverse()

44.


What is a correct syntax for sorting a list descending?

a)

mylist.sort(-1)

b)

mylist.sort(reverse = True)

c)

mylist.sort('desc')

45.

What is a correct syntax for making a copy of a list?

a)

list2 = list1

b)

list2 = list1.copy()

c)

list2.copy(list1)

46.

What is a correct syntax for making a copy of a list?

a)

list2 = list1

b)

list2 = list1.list()

c)

list2 = list(list1)

47.


What is a correct syntax for making a copy of a list?

a)

list2 = list1[:]

b)

list2 = list1[]

c)

list2 = list1[-1]

48.

What is a correct syntax for joining list1 and list2 into list3?

a)

list3 = join(list1, list2)

b)

list3 = list1 + list2

c)

list3 = [list1, list2]

49.

What is a correct syntax for adding elements from list2 into list1?

a)

list1.extend(list2)

b)

list1.join(list2)

c)

list1.push(list2)

50.

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)

['a', 'b', 'c', 1, 2, 3]

b)

[1, 2, 3]

c)

['a', 1, 'b', 2, 'c', 3]

51.

Which one of these is a tuple?

a)

thistuple = ('apple', 'banana', 'cherry')

b)

thistuple = ['apple', 'banana', 'cherry']

c)

thistuple = {'apple', 'banana', 'cherry'}

52.

True or False.
Tuple items cannot be removed after the tuple has been created.

a)

True

b)

False

53.

You can access tuple items by referring to the index number, but what is the index number of the first item?

a)

-1

b)

0

c)

1

54.

You cannot change the items of a tuple, but there are workarounds. Which of the following suggestion will work?

a)

Convert tuple into a list, change item, convert back into a tuple.

b)

Convert tuple into a set, change item, convert back into a tuple.

c)

Convert tuple into a dictionary, change item, convert back into a tuple.

55.

Which is a correct syntax to delete a tuple?

a)

delete mytuple

b)

mytuple.delete()

c)

del mytuple

56.

True or False. You are allowed to add a tuple to an existing tuple.

a)

True

b)

False

57.


Consider the following code:
fruits = ('apple', 'banana', 'cherry')
(x, y, z) = fruits
print(y)
What will be the value of y?

a)

apple

b)

banana

c)

cherry

58.

Consider the following code:
fruits = ('apple', 'banana', 'cherry')
(x, *y) = fruits
print(y)
What will be the value of y?

a)

banana

b)

['banana', 'cherry']

c)

banana, cherry

59.

Consider the following code:
fruits = ('apple', 'banana', 'cherry', 'mango')
(x, *y, z) = fruits
print(y)
What will be the value of y?

a)

['banana', 'cherry']

b)

['banana', 'cherry', 'mango']

c)

['cherry', 'mango']

60.

Whis one of theese is a dictionary?

a)

x = ('apple', 'banana', 'cherry')

b)

x = {'type' : 'fruit', 'name' : 'banana'}

c)

x = ['apple', 'banana', 'cherry']

61.

True or False.
A dictionary cannot have two keys with the same name.

a)

True

b)

False

62.


Which one of these is a set?

a)

myset = ('apple', 'banana', 'cherry')

b)

myset = ['apple', 'banana', 'cherry']

c)

myset = {'apple', 'banana', 'cherry'}

63.

True or False.
Set items cannot be removed after the set has been created.

a)

True

b)

False

64.

What will be the result of the following code:
x = 5
y = 8
if x > y:
  print('Hello')
else:
  print('Welcome')

a)

Hello

b)

Welcome

65.

Which statement is a correct syntax to break out of a loop?

a)

end

b)

return

c)

break

66.

What will be the result of the following code:
for x in range(3):
  print(x)

a)

0

1

2

b)

0

1

2

3

c)

1

2

3

67.

What is the correct keyword for defining functions in Python?

a)

function

b)

func

c)

def

68.

What will be the result of the following code:
x = lambda a, b : a - b
print(x(5, 3))

a)

15

b)

8

c)

3

d)

2

69.

True or False: Lambda functions can take multiple arguments.

a)

True

b)

False

70.

True or False: Lambda functions can have multiple expressions.

a)

True

b)

False

71.

Python Lists can be used as arrays.
What will be the result of the following code:
fruits = ['apple', 'banana', 'cherry']
print(fruits[0])

a)

apple

b)

banana

c)

cherry

72.

What is a correct Python List method used to return the number of elements in a list?

a)

count()

b)

len()

c)

lenght()

73.

What will be the result of the following code:
fruits = ['apple', 'bananan', 'cherry']
print(len(fruits))

a)

1

b)

2

c)

3

d)

4

74.

When the class object is represented as a string, there is a function that controls what should be returned, which one?

a)

init()

b)

str()

c)

return()

75.

What is a correct syntax for deleting an object named person in Python?

person.delete()

delete person

del person

a)

person.delete()

b)

delete person

c)

del person

76.

What is the correct keyword to use inside an empty class, to avoid getting an error?

a)

empty

b)

inherit

c)

pass

77.

There are two methods that you have to implement when you create an iterator, which two?

a)

iter() and next()

b)

next() and prev()

c)

init() and end()

78.

Which statement can be used to stop the iteration?

break

stop

stopIteration

a)

break

b)

stop

c)

stopIteration

79.

True or False. Lists, tuples, dictionaries, and sets are all iterable objects.

a)

True

b)

False

80.

True or False. One object cannot have a method with the same name as another object's method.

a)

True

b)

False

81.

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.

a)

True

b)

False

82.

Which statement is true?

a)

Polymorphism refers to methods/functions/operators with the same name that can be executed on many objects or classes.

b)

Polymorphism refers to classes with the same name that performs different tasks.

83.

Consider the following code:
x = 300
def myfunc():
  x = 200
myfunc()
print(x)
What will be the printed result?

a)

200

b)

300

c)

200300

84.

Consider the following code:
x = 300
def myfunc():
  x = 200
myfunc()
print(x)
What will be the printed result?

a)

200

b)

300

c)

200300

85.

Which statement keyword can be used for variables inside nested function?

a)

local

b)

nonglobal

c)

nonlocal

86.

Consider the following code:
import datetime
x = datetime.datetime
Which syntax will print the current date?

a)

print(x.datetime())

b)

print(x.date())

c)

print(x.now())

87.

Consider the following code:
import datetime
x = datetime.datetime.now()
Which syntax will print the name of the weekday?

a)

print(x.strftime('%A'))

b)

print(x.ftime('%A'))

c)

print(x.fdate('%A'))

88.

When formatting date objects into readable strings, which syntax is used to return the month name, full version?

a)

print(x.strftime('%B'))

b)

print(x.strftime('%M'))

c)

print(x.strftime('%N'))

89.

Consider the following code:
import datetime
x = datetime.datetime(2024, 8, 20)
print(x.strftime('%d'))
What will be the printed result?

a)

19

b)

20

c)

21

90.

Consider the following code:
print(max(5, 10, 25))
What will be the printed result?

a)

5

b)

10

c)

25

91.

Consider the following code:
print(pow(2, 3))
What will be the printed result?

a)

2

b)

4

c)

8

d)

6

92.

When using the built-in math module, what will be the printed result of the following code:
import math
print(math.sqrt(9))

a)

3

b)

9

c)

81

93.

When using the built-in math module, how can you return the number of PI?

a)

math.pi

b)

math.fpi

c)

math.strpi

94.

When you parse code with the json.loads() method, the result is returned as a specific Python data type, which one?

a)

list

b)

set

c)

tuple

d)

dictionary

95.

Which method from the json library can be used to convert a Python object into a JSON string?

a)

json.tojson()

b)

json.dumps()

c)

json.extract()

96.

The json.dumps() method has a keyword parameter used to sort the result, what is it called?

a)

order

b)

sort_keys

c)

arrange

97.

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', 'a']

b)

'The rin in Spin'

c)

2

98.

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?

a)

3

b)

4

c)

5

99.

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?

a)

3

b)

4

c)

5

100.

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

a)

-1

b)

None

c)

0

d)

Null

101.

In the world of Pyhton, what describes PIP best?

a)

PIP is a module used for drawing

b)

PIP is a module used for handling large amounts of data

c)

PIP is a package manager for Python modules

102.

What is a correct way of importing the array module?

a)

import array

b)

include array

c)

install array

103.

In Command Line view, what is a correct statement for listing all the packages installed on your system?

a)

pip dir

b)

pip list

c)

pip read

104.

What is a function used for opening files?

a)

load()

b)

run()

c)

open()

105.

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?

a)

x = bopen('demofile.txt')

b)

x = open('demofile.txt', 'b')

c)

x = open(b'demofile.txt')

106.

True or False.
The default opening mode when opening a file with the open() function is 'r' for 'reading'.

a)

True

b)

False

107.

After opening a file with the open() function, which method can be used to read the content?

list()

read()

show()

a)

list()

b)

read()

c)

show()

108.

To read only one line, we can use another method, which one?

readline()

lread()

readl()

a)

readline()

b)

lread()

c)

readl()

109.

True or False.
If you call the readline() method two times, it will return the two first lines.

a)

True

b)

False

110.

What happens to the original file content if you open a file like this:
f = open('demofile3.txt', 'w')

a)

The original content will be overwritten

b)

Any new content will be added after the original content

111.

If you open a file like this:
f = open('demofile3.txt', 'w')
What happens if the file does not exist?

a)

It will return an error

b)

A file will be created

112.

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?

a)

'x'

b)

'b'

c)

't'

113.

To remove a file you can import the os module, but which function removes the file?

os.delete()

os.drop()

os.remove()

a)

os.delete()

b)

os.drop()

c)

os.remove()

114.

Which os function can be used to delete an entire folder?

a)

os.rmdir()

b)

os.rmfolder()

c)

os.rmcatalog()

115.

True or False. To remove a folder with the os module, it cannot contain any files

a)

True

b)

False