Category:Language Basics - Excercises
Language Basics ⌘
Check all syntactically correct statements
Which input statements are correct? ⌘
- a = raw_input()
- a = raw_input(“enter a number”)
- a = raw_input(enter your name)
Which print statements are correct? ⌘
- print “9” + “9”
- print int(“nine”)
- print str(9) + “nine”
- print 9 + 9
- nine = 9 print nine
Which are correct arithmetical operations? ⌘
- a = 1 * 2
- 2 = 1 + 1
- 5 + 6 = y
- seven = 3 * 4
Which are correct variable names? ⌘
- result
- my.result
- result77
Which are correct type conversions? ⌘
- int(7.0+0.1)
- str(1.2 * 3.4)
- float(“77”+”.0”)
- str( 9 / 0 )
Which operations result in 8? ⌘
- 65 // 8
- 17 % 9
- 2 ** 4
- 64 ** 0.5
Which lines are commented? ⌘
- “””This is a comment”””
- # This is a comment
- // this is a comment
- This is a comment
Data Types ⌘
Find the matching pairs of expressions and values
Manipulating Strings ⌘
Write the result of each operation into the fields.
String formatting ⌘
Find the matching pairs of expressions and values.
Regular Expressions ⌘
Find the expressions that fit to each row.
Functions and Modules ⌘
Find the matching pairs.
Manipulating Lists ⌘
Write the result of each operation into the fields.
Functions operating on lists (1) ⌘
Write the correct operations to the arrows.
Functions operating on lists (2) ⌘
Write the correct operations to the arrows
Functions operating on lists (3) ⌘
Write the result of each operation into the fields.
Manipulating Lists ⌘
Check the correct answer.
What does the list b contain? ⌘
a = [8,7,6,5,4] b = a[2:4]
- [7,6,5]
- [7,6]
- [6,5]
- [6,5,4]
Which of the following code pieces results in ⌘
- a == [2,4,6]
- a = [1,2,3] * 2
- a = [int(s) for s in “246”]
- a = [x*2 for x in range(3)]
- a = [2**1]+[2**2]+[2**3]
Working with Tuples ⌘
Check all correct answers
Which are correct tuples? ⌘
- ( 1, 2, 3)
- (“Jack” “Knife”)
- ('blue', [0,0,255])
- [ 1, “word” ]
What are tuples good for? ⌘
- Grouping data.
- Managing values that change.
- Running a for loop over them.
- Sorting.
On what data types does the len() function work on? ⌘
- lists
- dictionaries.
- strings.
- tuples.
Manipulating Dictionaries ⌘
Write the result of each operation into the fields.
Manipulating Dictionaries ⌘
Check the correct answer
What do these commands produce? ⌘
d = {1:'A', 'B':1, 'A':True} print d['A']
- False
- “B”
- True
- 1
What do these commands produce? ⌘
d = {1:'A', 'B':1, 'A':True} print d.has_key('B')
- 1
- True
- 'B'
- False
What do these commands produce? ⌘
d = {1:'A', 'B':1, 'A':True} print d.values()
- True
- ['A',1,True]
- 3
- [1,'B','A']
What do these commands produce? ⌘
d = {1:'A', 'B':1, 'A':True} print d.keys()
- [1,'B','A']
- ['A','B',1]
- [1,'A','B']
- The order may vary.
What do these commands produce? ⌘
d = {1:'A', 'B':1, 'A':True} print d['C']
- None
- 'C'
- an Error
- False
What do these commands produce? ⌘
d = {1:'A', 'B':1, 'A':True} d.setdefault('C',3) print d['C']
- 3
- “C”
- None
- an Error
Loops and conditional statements ⌘
Check the correct statements.
Which of these while commands are correct? ⌘
- while a = 1:
- while a+7:
- while len(c)>10:
- while a and (b-2 == c):
- while b==1
- while s.find('c')>=0:
Which of these statements are correct? ⌘
- 'while' is also called a conditional loop.
- The expression after while may contain function calls.
- It is possible to write endless while loops.
- The colon after while may be omitted.
- The code block after while is executed at least once.
What are possible structures of a conditional statement? ⌘
- if <expression> .. else
- if <expression> .. else if <expression>
- if <expression> .. elif <expression> .. else
- if <expression> .. elif <expression> ..else <expression>
- If <expression>.. else <expression> .. efli
Which of these for commands are correct? ⌘
- for char in “ABCD”:
- for i in range(10):
- for num in (4,6,8):
- for k in 3+7:
- for (i=0; i<10; i+=1):
- for var in seq:
Which of these if statements are syntactically correct? ⌘
- if a and b:
- if len(s) == 23:
- if a but not b<3:
- if a ** 2 >= 49:
- if a != 3:
- if (a and b) or (c and d):
Modules and Packages ⌘
Check the correct answer(s).
Which of these import statements are correct? ⌘
- import re
- import re.sub
- from re import sub
- from re.sub import *
- from .re import *
- from re import *
Where does Python look for modules to import ⌘
- In the sys.path variable.
- In the current working directory.
- In the directory where the current module is.
- In the directory where Python was started.
- In the site-packages folder
- In directories in the PYTHONPATH variable
- In the root directory.
Which statements about packages are true? ⌘
- A package is a directory with modules.
- A package may contain zero modules.
- Packages in site-packages/ are imported automatically.
- A package must contain a __init__.py file.
- A package may contain no code.
- Packages are useless in small programs.
Which packages are installed by default? ⌘
- os – manipulating files and directories.
- psyco – makes Python faster
- time – accessing date and time.
- csv – reads and writes tables.
- numpy – number crunching.
- pdb – Python debugging.
While loops ⌘
Match the expressions for the while loops run the designated number of times
Reading and Writing Files ⌘
Write Python commands into the fields
Error Handling ⌘
Check all correct answers
Which commands result in an Exception? ⌘
- f = open(“:::”)
- char = “abc”[7]
- a = (5+9) / (6-(2*3))
- num = [1,2,3][0]
- l = range(10)[:20]
- num = {1:'a'}['a']
Which are common types of Exceptions? ⌘
- ZeroDivisionError
- IOError
- ValueError
- NullPointerException
- KeyError
- InfiniteLoopError
Which commands for managing Exceptions exist? ⌘
- try: … else: ...
- raise ValueError('text')
- try: … except: … error:
- try: … except: ...
Working with Files ⌘
Check all correct answers
Which are correct commands working with files? ⌘
- for line in open(filename):
- f = open(filename,'w')
- open(filename).writelines(out)
- f.close()
Which statements about the csv module are correct? ⌘
- It can save tables of strings and numbers.
- csv reads tables of strings.
- csv cannot handle the quote character.
- Files need to have the .csv suffix.
Language Basics ⌘
- What is a variable?
- What names may variables have?
- What is an integer?
- What is a string?
- How to assign a variable?
- How to add, substract, and multiply numbers?
- What can be printed with print?
- How can text be read from the keyboard?
- How are comments written in a program?
The math module ⌘
Find the matching pairs of functions and values.
The os module ⌘
Insert the correct functions into the gaps.
Creating plots ⌘
Write the result of each operation into the fields.
Which of these for commands are necessary to create a figure in matplotlib? ⌘
- p = plot(x_data,y_data, 'k-')
- title(Name of the graph')
- xlabel('x axis name')
- axis([0,15,0,10])
- savefig('figure1.png'
- show()
Pages in category 'Language Basics - Excercises'
This category contains only the following page.