wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python list, if

Total questions: 20

Worksheet time: 11mins

Name
Class
Date
1.

Which of these is the correct code for creating a list of names?

a)

nameList = John, Harry, Jesse, John, Harry, Harry

b)

nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")

c)

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

d)

nameList = [John, Harry, Jesse, John, Harry, Harry]

2.

Which of these pieces of code would return the name "Harry" from the following list?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

nameList()

b)

nameList[1]

c)

NameList(4)

d)

nameList["4"]

3.

The list needs one more name added to the end - "Felipe". Which piece of code below would do this?

nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]

a)

nameList.append(Felipe)

b)

append(nameList,"Felipe")

c)

nameList.append["Felipe",7]

d)

nameList.append("Felipe")

4.

nameList = ["John", "Harry", "Jesse", "John", "Marry", "Larry"]

The list will be slice and show Harry and Jesse only. Which piece of code below would do this?

a)

print(nameList[1:3])

b)

print(nameList[0:1])

c)

print(nameList[1:2])

d)

print(nameList["Harry":"Jesse"])

5.

To insert an the string "Pedro" in the first position of a the nameList we use

a)

nameList.append("Pedro, 1")

b)

nameList.insert("Pedro",0)

c)

nameList.insert(1, "Pedro")

d)

nameList.insert(0, "Pedro")

6.

variable.sort() will do what?

a)

Sort the list into alphabetical order

b)

Sort the list into reverse order

c)

Create a new list

d)

Error

7.

nameList = ["John", "Harry", "Jesse", "John", "Marry", "Larry"]

print(nameList.count("John"))

What would be the output

a)

1

b)

2

c)

3

d)

Error

8.

nameList = ["John", "Harry", "Jesse", "John", "Marry", "Larry"]

nameList[6] ="Susie"

print(nameList)

What would be the output

a)

['John', 'Harry', 'Jesse', 'John', 'Marry', 'Larry', 'Susie']

b)

['Susie', 'John', 'Harry', 'Jesse', 'John', 'Marry', 'Larry']

c)

['Susie']

d)

Error

9.

What does the following code do? myAge = int (myAge)

a)

Converts the var (variable) myAge to a string

b)

Converts the var (variable) myAge to a integer

c)

Converts the var (variable) myAge from a integer to a string

d)

Converts the var (variable) myAge to if statement

10.
What will the output be from the following code?
print(Hello world!)
a)
Hello world!
b)
SyntaxError
c)
Hello world
d)
print(Hello world!)
11.
Decides if a string only contains numbers
a)
isalpha()
b)
isnumeric()
c)
int()
d)
string()
12.
Python identifies blocks of code by
a)
BEGIN and END keywords
b)
{ and }
c)
aligning up the starts of lines (indentation)
d)
guessing
13.

people = ["John", "Rob", "Bob"]

print (people[1])

what would this result be?

a)
John
b)
Rob
c)
Bob
14.

The ‘else’ statement is optional in Python

a)

TRUE

b)

FALSE

15.

There can be an else statement without an if statement

a)

FALSE

b)

TRUE

16.

The program above

a)

prints all the odd numbers between 1 and 20

b)

prints all even numbers between 1 and 20

c)

print all the odd numbers between 1 and 21

d)

prints all the even numbers between 1 and 21

17.

Write the symbol for the blank space, if an adult's age is 18 or above.

if age (a)   18: print("You are an adult")

elif age<18: print("You are a child")

18.
This operator means that one value is not equal to another value
a)
==
b)
<=
c)
>=
d)
!=
19.

The result of this program:

Friday = False

if Friday:

print "Jeans day!"

else:

print "Uniform day"

a)

Jeans day

b)

Today is Friday

c)

Uniform day

d)

Not Friday

20.

What is the output?

a)

True

b)

NIL

c)

True

NIL

d)

True

NIL

NIL