wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python basics

Total questions: 67

Worksheet time: 32mins

Name
Class
Date
1.

Select all strings:

a)

greeting = (Hello)

b)

greeting = ("Please enter your name")

c)

greeting = (Welcome to Comp Sci)

d)

greeting = ("Happy Friday!!!")

2.

Please select all floating point numbers:

a)

1.444

b)

34567

c)

777.980

d)

32.0

e)

21

3.

What is the default data type in Python?

a)

Real

b)

String

c)

Integer

d)

Number

4.

What is the Python code needed to display a word on the screen?

a)

print"hello"

b)

"hello"

c)

print("hello")

d)

print="hello"

5.

What is the Python code needed to ask a user for input?

a)

input("what is your name?")

b)

input="what is your name?"

c)

(input)=what is your name?

d)

input=what is your name?

6.

Which is the correct way to store an input as a variable in Python?

a)

answer("what is your name?")

b)

input = "answer"(what is your name?)

c)

answer = input("what is your name?")

d)

input=(what is your name?)

7.

Which character represents a comment in Python code?

a)

*

b)

#

c)

&

d)

$

8.

Which of the following data types can consist of numbers, alphabets and symbols?

a)

String

b)

Integer

c)

Float

d)

Boolean

9.

Which of the following data types is the most appropriate for 23 June 2020?

a)

Float

b)

Boolean

c)

Integer

d)

String

10.

78.0

a)

String

b)

Integer

c)

Float

d)

Number

11.

"89 / 90"

a)

String

b)

Integer

c)

Float

d)

Number

12.

56 + 30

a)

String

b)

Integer

c)

Float

d)

Number

13.

"89"

a)

String

b)

Integer

c)

Float

d)

Number

14.

What is a String?

a)

A data type that contains whole numbers. eg: 3, 45, 124

b)

Numbers with decimal point. eg: 0.5, 2.45, 56.04

c)

Group of characters between quotation marks. eg: "dog"

d)

Data type that can only be True or False.

15.

What is an Integer?

a)

A data type that contains whole numbers. eg: 3, 45, 124

b)

Numbers with decimal point. eg: 0.5, 2.45, 56.04

c)

Group of characters between quotation marks. eg: "dog"

d)

Data type that can only be True or False.

16.

Which value is an Integer?

a)

64

b)

"cat"

c)

True

d)

0.5

17.

Which of the following are true of Python lists?

a)

A list may contain any type of object except another list

b)

A given object may appear in a list more than once

c)

All elements in a list must be of the same type

d)

These represent the same list:

['a', 'b', 'c']

['c', 'a', 'b']

e)

There is no conceptual limit to the size of a list

18.

Assume the following list definition:


>>> a = ['foo', 'bar', 'baz', 'qux', 'quux', 'corge']


Several short REPL sessions are shown below. Which display correct output?

a)

>>> print(a[-6])

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

IndexError: list index out of range

b)

>>> a[:] is a

True

c)

>>> print(a[4::-2])

['quux', 'baz', 'foo']

d)

>>> print(a[-5:-3])

['bar', 'baz']

e)

>>> max(a[2:4] + ['grault'])

'qux'

19.

List a is defined as follows:


a = [1, 2, 3, 4, 5]


Select all of the following statements that remove the middle element 3 from a so that it equals [1, 2, 4, 5]:

a)

a[2] = []

b)

a[2:2] = []

c)

a[2:3] = []

d)

del a[2]

e)

a.remove(3)

20.

Consider the following nested list definition:


x = [10, [3.141, 20, [30, 'baz', 2.718]], 'foo']


A schematic for this list is shown above:


What is the expression that returns the 'z' in 'baz'?

a)

x[1][2][1]

b)

x[1][2][1][2]

c)

x[1][2][2]

d)

x[1][1][2]

21.

List a is defined as follows:


a = [1, 2, 3, 4, 5]


Select all of the following statements that remove the middle element 3 from a so that it equals [1, 2, 4, 5]:

a)

a[2:3] = []

b)

a[2] = []

c)

a.remove(3)

d)

a[2:2] = []

e)

del a[2]

22.

List a is defined as follows:


a = ['a', 'b', 'c']


Which of the following statements adds 'd' and 'e' to the end of a, so that it then equals ['a', 'b', 'c', 'd', 'e']:

a)

a += ['d', 'e']

b)

a[-1:] = ['d', 'e']

c)

a += 'de'

d)

a.append(['d', 'e'])

e)

a[len(a):] = ['d', 'e']

23.

You have a list a defined as follows:


a = [1, 2, 7, 8]


Write a Python statement using slice assignment that will fill in the missing values so that a equals [1, 2, 3, 4, 5, 6, 7, 8].

a)

a[2:2] = [3, 4, 5, 6]

b)

a[1:2] = [3, 4, 5, 6]

c)

a[2:3] = [3, 4, 5, 6]

d)

a[3:5] = [3, 4, 5, 6]

24.

Suppose you have the following tuple definition:


t = ('foo', 'bar', 'baz')


Which of the following statements replaces the second element ('bar') with the string 'qux':

a)

t[1] = 'qux'

b)

t(1) = 'qux'

c)

t[1:1] = 'qux'

d)

It’s a trick question—tuples can’t be modified.

25.

Write Python code to create a tuple with a single element, the string 'foo', and assign it to a variable called t.

(a)  

26.

Consider this assignment statement:


a, b, c = (1, 2, 3, 4, 5, 6, 7, 8, 9)[1::3]


Following execution of this statement, what is the value of b:

a)

5

b)

4

c)

3

d)

6

e)

7

27.

Assume x and y are assigned as follows:


x = 5

y = -5


What is the effect of this statement:


x, y = (y, x)[::-1]

a)

The values of x and y are swapped

b)

The values of x and y are unchanged

c)

Both x and y are -5

d)

Both x and y are 5

28.

What are the three main programming structures?

a)

Sequence, selection, iteration

b)

Structured, object-oriented, procedural

c)

Java, Python, Visual Basic

d)

Machine, assembly, high-level

29.
What is the Output if the user enters:
Yes
a)
Leave umbrella at home
b)
Take an umbrella
30.

In a Python program , a control structure:

a)

directs the order of execution of the statements in the program

b)

dictates what happens before te program starts and after it terminates

c)

defines program-specific data structures

d)

manages the input and output of control characters

31.

What is the output of the above program ?

a)

Am I here?

b)

Or here ?

c)

Am I here ? Or here ?

d)

Or here ? Or over here ?

32.

If the user inputs : 2<Enter> , what does the following code snippet print ?

a)

Yes

b)

No

c)

May be

d)

Nothing printed

e)

Error

33.

Function range(3) is equivalent to "

a)

range(1,3)

b)

range(0,3)

c)

range(0,3,1)

d)

range(1,3,0)

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

What is the output?

a)

condition

b)

True

c)

Program has a syntax error.

d)

3 > 2

36.

What is the output?

a)

more than 7

b)

more than 23

c)

spam

d)

7

37.

What is the output?

a)

3

b)

3

7

c)

3

5

7

d)

7

38.

What is the output?

a)

True

b)

NIL

c)

True

NIL

d)

True

NIL

NIL

39.

What is the output?

a)

level 3

b)

level 3

level 1

c)

level 3

level 1

NIL

d)

level 3

level 2

level 1

NIL

40.

An If statement can be executed multiple times

a)

FALSE

b)

TRUE

41.

The ‘else’ statement is optional in Python

a)

TRUE

b)

FALSE

42.

It is possible to execute both the statements under if and else at the same time.

a)

TRUE

b)

FALSE

43.

There can be an else statement without an if statement

a)

FALSE

b)

TRUE

44.

The program generates an error message if the "else" statement is not defined

a)

FALSE

b)

TRUE

45.

Python supports (a)   types of loops

46.

What is the output?

a)
Hello world!
b)
Hello world
c)
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
......(repeated continuously)
d)
Error
47.

What is the output if the user types "Python"?

a)
Welcome
b)
Try again!
c)
Error
d)
Enter your password
48.

Which symbol is used to indicate comments in Python?

a)

//

b)

#

c)

/

d)

<!--

49.

What is the correct way to define a variable in Python?

a)

variable_name = value

b)

variable_name <- value

c)

value = variable_name

d)

variable_name : value

50.

In Python, variables are:

a)

Declared with a specific data type

b)

Implicitly typed

c)

Declared using the 'var' keyword

d)

Only allowed in functions

51.

Which of the following is NOT a valid variable name in Python?

a)

_myVar

b)

123_var

c)

My-Var

d)

myVar2

52.

What will be the output of this code?

a)

5

b)

10

c)

15

d)

The code will raise an error

53.

What is the data type of the variable in this code?

a)

int

b)

float

c)

str

d)

bool

54.

What is the output of this code?

a)

Greater than 5

b)

Less than or equal to 5

c)

10

d)

The code will raise an error

55.

What is the output of this code?

a)

5

b)

10

c)

15

d)

The code will raise an error

56.

What is Python?

a)

A high-level programming language

b)

low-level programming language

c)

A markup language

d)

A database management system

57.

What is the purpose of indentation in Python?

a)

To make the code look neat and organized

b)

To separate code blocks

c)

To improve the program's performance

d)

To add spaces between characters

58.

Which of the following is not a built-in data type in Python?

a)

int

b)

float

c)

decimal

d)

str

59.

Which data type is used to represent non-integer numeric values in Python?

a)

int

b)

float

c)

str

d)

bool

60.

Which data type is used to represent true or false values in Python?

a)

int

b)

float

c)

bool

d)

str

61.

What is the output of this code snippet?

a)

510

b)

TypeError

c)

15

d)

"510"

62.

What is the output of this expression?

a)

True

b)

False

c)

Error

d)

None

63.

Which operator is used for exponentiation in Python?

a)

^

b)

**

c)

&

d)

//

64.

Which operator is used for floor division in Python?

a)

/

b)

//

c)

%

d)

*

65.

Which operator is used to check for the inequality of two values in Python?

a)

>

b)

<

c)

==

d)

!=

66.

What is the result of this expression?

a)

"Hello World"

b)

"HelloWorld"

c)

Error

d)

None

67.

What is the result of this expression?

a)

3.3333333333333335

b)

3.0

c)

3

d)

4.0