01 . Perform Operations using Data Types and Operations 1 Flashcards
Describe what a variable is?
A variable can be thought of as a container that can store information that can be used later.
Python has rules for how to declare a variable name.
List the four rules.
- Variables cannot have spaces in the names
- Variables are case-sensitive
- Variables cannot start with a number. It must be either a letter or the underscore character.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Name the best practice which should be used when declaring variables that contain two words.
The best practice is to use camel casing, i.e. firstName
What type of variable is firstName in the given example?
firstName = ‘Bryce’
String variable
A whole number is known as what type of variable in python?
An integer
What punctuation symbols should we use to combine strings and numbers in a print statement?
A comma
How many values does a Boolean variable have?
Two values - either True or False
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)
Note: the newline escape character on the previous slide. The output would be as follows.
northwins = False
southwins = True
Are the two Boolean values case sensitive?
Yes - the values must be written either True or False.
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.”)
Surround the variable widgets with the str function to cast it to a string.
print(“We have “ + str(widgets) + “ in stock.”)
What type will the answer be as per the print statement calculation?
price = 3.95
widgets = 5
print(price * widgets)
It will be a float, i.e. 19.75
What type of data structure is the following piece of code?
regions = [“North”, “South”, “East”, “West”]
Due to the square brackets, the sample code is a list.
What method is required to add a new employee name to the end of the following list?
employee = [“Bob”, “Dave”, “John”]
The append() method.
employee.append(“Ken”)
What method is required to remove an employee name from the following list?
employee = [“Bob”, “Dave”, “John”]
The remove() method
employee.remove(“Bob”)
What method is required to arrange the names of the employees in alphabetical order?
employee = [“Bob”, “Dave”, “John”]
The sort() method
employee.sort()