WorksheetsUnit 1: Python Basics
Total questions: 46
Worksheet time: 24mins
To display something on the screen, use this command:
print()
Print()
screen()
write()
display()
name = 'Dave'
print (name)
Which operator checks if a value is equal to another value?
+
=
==
!=
Which of the following is correct?
Comments are for programmers for better understanding of the program.
Python Interpreter ignores comment.
You can write multi-line comments in Python using triple quotes, either ''' or """.
All of the above
In the following code, n is a/an _______?
n = '5'
integer
string
tuple
operator
What is used to take input from the user in Python?
cin
scanf()
input()
<>
name = 'Dave'
greeting = "Good morning" + name
print (greeting)
people = ["John", "Rob", "Bob"]
print (people[-1])
what would this result be?
Which of the following is a correctly-formatted comment?
*Comment
"Comment"
#comment
(comment)
A line of text that Python won't try to run as code
Integer
Comment
Strings
Input
// means:
two times division
integer division
exact division with floating number
Put a slash
What does "\t " make?
Goes to next line
Horizontal space between two strings
Give a title
Add a slash
What is a list in python?
A collection of variables
A collection of items assigned to a single variable
A statement that cant be changed
A Christmas list
Which of these codes is correct for creating a list of numbers?
num = ('10','29','37','109')
num = ['10','29','37','109']
num == ('10','29','37','109')
num = ['10 29 37 109']
How do i remove something from a list?
variable.append()
variable.delete()
variable.remove()
variable=(new variable)
Which code would i use to add an item to a list?
variable.add()
variable.append()
append.variable()
add.variable()
What does the '#' allow you to do?
Repeat code
Comment on code
Print code
End code
Which of these is the correct code for creating a list of names?
nameList = John, Harry, Jesse, John, Harry, Harry
nameList = ("John", "Harry", "Jesse", "John", "Harry", "Harry")
nameList = ["John", "Harry", "Jesse", "John", "Harry", "Harry"]
nameList = [John, Harry, Jesse, John, Harry, Harry]
List items have an index number. In the following list, which item has the index number of 3?
["John", "Harry", "Jesse", "John", "Harry", "Harry"]
"John"
"Harry"
"Jesse"
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"]
nameList.append(Felipe)
append(nameList,"Felipe")
nameList.append["Felipe",7]
nameList.append("Felipe")
Tuples in Python are mutable
False
True
Result of the program above is
('1', '2', '3', '4', '5')
(1, 2, 3, 4, 5)
12345
"12345"
What is the output of the program above
<class 'tuple'>
<class 'None'>
<class 'list'>
<class 'int'>
Which of the following is an incorrect way to create a tuple?
T=(3)*2
T=(1,2,)
T=('A','B')
T=tuple([1,2,3])
Which of the following are true of Python dictionaries:
Dictionaries can be nested to any depth.
Dictionaries are accessed by key.
All the keys in a dictionary must be of the same type.
Dictionaries are mutable.
A dictionary can contain any object type except another dictionary.
Which of the following is not a valid way to define this dictionary in Python:
d = dict([
('foo', 100),
('bar', 200),
('baz', 300)
])
d = {}
d['foo'] = 100
d['bar'] = 200
d['baz'] = 300
d = dict(foo=100, bar=200, baz=300)
d = {'foo': 100, 'bar': 200, 'baz': 300}
d = { ('foo', 100), ('bar', 200), ('baz', 300) }
You have the following dictionary definition:
d = {'foo': 100, 'bar': 200, 'baz': 300}
What method call will delete the entry whose value is 200?
delete d('bar')
d.pop("bar")
d.remove("bar")
None of the above
Consider the following statement:
If you just need to work with the keys of a dictionary, then you can use .keys(), which is a method that returns a new view object containing the dictionary’s keys.
Is this statement True or False?
False
True
The _________ function removes the first element of a set and the last element of a list.
remove
pop
discard
dispose
Which of these about a set is not true?
Mutable data type
Allows duplicate values
Data type with unordered values
Immutable data type
If x={5,6,7,8}, which of the following statements is false?
print(len(x))
print(min(x))
x.remove(5)
x[2]=45
Python is _____ sensitive which means you must use the same indentation every time.
space
colour
code
word
print(3*4+5)
What will be the output of the following Python code?
nums = set([1,1,2,3,3,3,4,4])
print(len(nums))
7
Error, invalid syntax for formation of set
4
8
