wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Unit 02 - Libraries and Variables

Total questions: 31

Worksheet time: 16mins

Name
Class
Date
1.

You've been asked to write a program that asks a user for a value. The value must be used as a whole number in a calculation even if the user enters a decimal value. To help complete the code for this requirement, which code segment should you use?

a)

total_items = float(input("How many items would you like?"))

b)

total_items = input("How many items would you like?")

c)

total_items = str(input("How many items would you like?"))

d)

total_items = int(input("How many items would you like?"))

2.

Match the data type to the code segment containing it.


Which of the segment below would represent a type bool

a)

age = 2

b)

tiger = False

c)

name = "Albert"

d)

grade = 91.7

e)

zip = "32054"

3.

Match the data type to the code segment containing it.


Which of the segment below would represent a type int

a)

age = 2

b)

tiger = False

c)

name = "Albert"

d)

grade = 91.7

e)

zip = "32054"

4.

Match the data type to the code segment containing it.


Which of the segment below would represent a type str

a)

age = 2

b)

tiger = False

c)

name = "Albert"

d)

grade = 91.7

5.

Match the data type to the code segment containing it.


Which of the segment below would represent a type float

a)

age = 2

b)

tiger = False

c)

name = "Albert"

d)

grade = 91.7

e)

zip = "32054"

6.

After running code segment 1 shown below, what will the data type of variable a be?


# Code segment 1

x1 = "20"

y1 = 3

a = x1 * y1

a)

str

b)

int

c)

float

d)

bool

e)

none of these, code will produce an error instead.

7.

After running code segment 2 shown below, what will the data type of variable b be?


# Code segment 2

x2 = 6

y2 = 4

b = x2 / y2

a)

str

b)

int

c)

float

d)

bool

e)

none of these, code will produce an error instead.

8.

After running code segment 3 shown below, what will the data type of variable c be?


# Code segment 3

x3 = 2.5

y3 = 1

c = x3 / y3

a)

str

b)

int

c)

float

d)

bool

e)

none of these, code will produce an error instead.

9.

Evaluate the following Python arithmetic expression and identify the correct result.


(3*(1+2)**2 - (2**2)*3)

a)

3

b)

13

c)

15

d)

69

10.

You are writing code that generates a random integer with a minimum value of 5 and a maximum value of 11. Which TWO functions should you use. Each correct answer presents a complete solution. (CHOOSE TWO)

a)

random.randint(5, 11)

b)

random.randint(5, 12)

c)

random.randrange(5, 12, 1)

d)

random.randrange(5, 11, 1)

11.

You need to accept input from the user and print that information to the user screen.


01 print("What is your name?")

02

03 print(name)


You have started the following code, but haven't finished it yet. What should go on line 02?

a)

name = input

b)

input("name")

c)

input(name)

d)

name = input()

12.

You develop a Python application for your company. You want to add notes to your code so that other team members will understand it.


What should you do?

a)

Place the notes after the # sign on any line.

b)

Place the notes after the last line of code separated by a blank line.

c)

Place the notes before the last line of code separated by a blank line.

d)

Place the notes inside of the parentheses on any line.

13.

Identify the correct data type to the type operation shown below:


type(+1E10)

a)

int

b)

float

c)

str

d)

bool

14.

Identify the correct data type to the type operation shown below:


type(5.0)

a)

int

b)

float

c)

str

d)

bool

15.

Identify the correct data type to the type operation shown below:


type("True")

a)

int

b)

float

c)

str

d)

bool

16.

Identify the correct data type to the type operation shown below:


type(False)

a)

int

b)

float

c)

str

d)

bool

17.

You have written the following line of code in your Python program:


value = a+b*c-d


Which part of the expression will be evaluated FIRST?

a)

a+b

b)

b*c

c)

c-d

18.

You have written the following line of code in your Python program:


value = a+b*c-d


Which part of the expression will be evaluated SECOND?

a)

addition

b)

subtraction

c)

multiplication

19.

You have written the following line of code in your Python program:


value = a+b*c-d


Which expression below is equivalent to this expression?

a)

(a+b)*(c-d)

b)

(a+(b*c))-d

c)

a+((b*c)-d)

20.

What will the output be from the following Python code?


print(9/3)

a)

3

b)

3.0

c)

9/3

d)

SyntaxError

21.

What is the Python built-in function to converts a number to a string?

a)

str()

b)

int()

c)

parseInt()

d)

convert

22.

Two symbols are missing from the codes (see picture).

Choose the correct symbols to ensure the input function works.

a)

1st blank: "

2nd blank: "

b)

1st blank: #

2nd blank: #

c)

1st blank: !

2nd blank: !

d)

1st blank: (

2nd blank: )

23.

Two symbols are missing from the codes (see picture).

Choose the correct symbols to ensure the concatenation works.

a)

1st blank: +

2nd blank: +

b)

1st blank: "

2nd blank: "

c)

1st blank: (

2nd blank: )

d)

1st blank: #

2nd blank: #

24.

x = 5

y = 6

print("x*y")


The code above displays the following:

a)

11

b)

x*y

c)

Syntax Error

d)

30

25.
In python the ' STRING data type' can be defined as...?
a)
holds alphanumeric text
b)
hold whole numbers
c)
holds numbers with a decimal point
d)
holds either a true or false value
26.
In python the ' INTEGER data type' can be defined as...?
a)
holds alphanumeric data as text
b)
holds whole numbers
c)
holds numbers with a decimal point
d)
holds either ‘true’ or ‘false’
27.

In python the ' FLOAT data type' can be defined as...?

a)

holds alphanumerical data

b)

holds whole numbers

c)

holds a number with a decimal part

d)

hold either true or false

28.

In Python the ' BOOLEAN data type' can be defined as...?

a)

holds alphanumerical data

b)

holds whole numbers

c)

holds a number with a decimal point

d)

holds either true or false

29.

If we needed to store information such as a ZIP CODE in PYTHON, what data type would we use?

a)

String (alphanumerical)

b)

Float (decimal point)

c)

Boolean (true or false)

30.

What is keeping the following math code from successfully executing?


number_one = input("Enter a number: ")

number_two = input("Enter a second number: ")

print(number_one + number_two)

a)

Because variables cannot be longer than 1 word

b)

Because number_one and number_two are strings

c)

Because input is an invalid function

d)

Python will always think that it has to add 1+2

e)

Program works as is.

31.

What will the code below print if -

num1 = 5 and num2 = 8?


num1 = input("Number 1")

num2 = input("Number 2")

print(num1+num2)

a)

85

b)

58

c)

5+8

d)

None of the above

e)

13