01 . Perform Operations using Data Types and Operations 1 Flashcards

1
Q

Describe what a variable is?

A

A variable can be thought of as a container that can store information that can be used later.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Python has rules for how to declare a variable name.

List the four rules.

A
  1. Variables cannot have spaces in the names
  2. Variables are case-sensitive
  3. Variables cannot start with a number. It must be either a letter or the underscore character.
  4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name the best practice which should be used when declaring variables that contain two words.

A

The best practice is to use camel casing, i.e. firstName

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What type of variable is firstName in the given example?

firstName = ‘Bryce’

A

String variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A whole number is known as what type of variable in python?

A

An integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What punctuation symbols should we use to combine strings and numbers in a print statement?

A

A comma

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How many values does a Boolean variable have?

A

Two values - either True or False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will be the output in the print statement for the following code example?

north = 200

south = 300

northwins = north > south

southwins = south > north

print(“northwins = “, northwins, “\nSouthwins = “, southwins)

A

Note: the newline escape character on the previous slide. The output would be as follows.

northwins = False

southwins = True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Are the two Boolean values case sensitive?

A

Yes - the values must be written either True or False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The following code example results in a TypeError when executed.

What function is required to cast the variable “widgets” to a string?

price = 3.95

widgets = 5

print(“The price of the widgets is “, price)

print(“We have “ + widgets + “ in stock.”)

A

Surround the variable widgets with the str function to cast it to a string.

print(“We have “ + str(widgets) + “ in stock.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What type will the answer be as per the print statement calculation?

price = 3.95

widgets = 5

print(price * widgets)

A

It will be a float, i.e. 19.75

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What type of data structure is the following piece of code?

regions = [“North”, “South”, “East”, “West”]

A

Due to the square brackets, the sample code is a list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What method is required to add a new employee name to the end of the following list?

employee = [“Bob”, “Dave”, “John”]

A

The append() method.

employee.append(“Ken”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What method is required to remove an employee name from the following list?

employee = [“Bob”, “Dave”, “John”]

A

The remove() method

employee.remove(“Bob”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What method is required to arrange the names of the employees in alphabetical order?

employee = [“Bob”, “Dave”, “John”]

A

The sort() method

employee.sort()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What index is “south” in the following list?

region = [“North”, “South”, “East”, “West”]

A

Index 1

17
Q

What will be the output from the following print statement?

region = [“North”, “South”, “East”, “West”]

print(“Region: “, region[-1])

A

Region: West

-1 can be used to access the last item in a list.

18
Q

What will be the print statement output in the following sample?

employees = [“Bob”, “Dave”, “John”]

employees[2] = “Michael”

for employee in employees:
print(employee)

A

Bob
Dave
Michael

The index value of 2 represents the last item in the list. Therefore, the item for John will be overwritten with the name Michael.

19
Q

What data type is the variable in the following code example?

serialNumber = “535353”

A

A string variable.

20
Q

What data type is the variable in the following code example?

itemsReceived = False

A

A Boolean variable