Category:Language Basics - Excercises

From Training Material
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


title
Python Language Basics- Excercises
author
Sam Bashton

Language Basics ⌘

Check all syntactically correct statements

Which input statements are correct? ⌘

  1. a = raw_input()
  2. a = raw_input(“enter a number”)
  3. a = raw_input(enter your name)

Which print statements are correct? ⌘

  1. print “9” + “9”
  2. print int(“nine”)
  3. print str(9) + “nine”
  4. print 9 + 9
  5. nine = 9 print nine

Which are correct arithmetical operations? ⌘

  1. a = 1 * 2
  2. 2 = 1 + 1
  3. 5 + 6 = y
  4. seven = 3 * 4

Which are correct variable names? ⌘

  1. result
  2. my.result
  3. print
  4. result77

Which are correct type conversions? ⌘

  1. int(7.0+0.1)
  2. str(1.2 * 3.4)
  3. float(“77”+”.0”)
  4. str( 9 / 0 )

Which operations result in 8? ⌘

  1. 65 // 8
  2. 17 % 9
  3. 2 ** 4
  4. 64 ** 0.5

Which lines are commented? ⌘

  1. “””This is a comment”””
  2. # This is a comment
  3. // this is a comment
  4. 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]

  1. [7,6,5]
  2. [7,6]
  3. [6,5]
  4. [6,5,4]

Which of the following code pieces results in ⌘

  1. a == [2,4,6]
  2. a = [1,2,3] * 2
  3. a = [int(s) for s in “246”]
  4. a = [x*2 for x in range(3)]
  5. a = [2**1]+[2**2]+[2**3]

Working with Tuples ⌘

Check all correct answers

Which are correct tuples? ⌘

  1. ( 1, 2, 3)
  2. (“Jack” “Knife”)
  3. ('blue', [0,0,255])
  4. [ 1, “word” ]

What are tuples good for? ⌘

  1. Grouping data.
  2. Managing values that change.
  3. Running a for loop over them.
  4. Sorting.

On what data types does the len() function work on? ⌘

  1. lists
  2. dictionaries.
  3. strings.
  4. 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']

  1. False
  2. “B”
  3. True
  4. 1

What do these commands produce? ⌘

d = {1:'A', 'B':1, 'A':True} print d.has_key('B')

  1. 1
  2. True
  3. 'B'
  4. False

What do these commands produce? ⌘

d = {1:'A', 'B':1, 'A':True} print d.values()

  1. True
  2. ['A',1,True]
  3. 3
  4. [1,'B','A']

What do these commands produce? ⌘

d = {1:'A', 'B':1, 'A':True} print d.keys()

  1. [1,'B','A']
  2. ['A','B',1]
  3. [1,'A','B']
  4. The order may vary.

What do these commands produce? ⌘

d = {1:'A', 'B':1, 'A':True} print d['C']

  1. None
  2. 'C'
  3. an Error
  4. False

What do these commands produce? ⌘

d = {1:'A', 'B':1, 'A':True} d.setdefault('C',3) print d['C']

  1. 3
  2. “C”
  3. None
  4. an Error

Loops and conditional statements ⌘

Check the correct statements.

Which of these while commands are correct? ⌘

  1. while a = 1:
  2. while a+7:
  3. while len(c)>10:
  4. while a and (b-2 == c):
  5. while b==1
  6. while s.find('c')>=0:

Which of these statements are correct? ⌘

  1. 'while' is also called a conditional loop.
  2. The expression after while may contain function calls.
  3. It is possible to write endless while loops.
  4. The colon after while may be omitted.
  5. The code block after while is executed at least once.

What are possible structures of a conditional statement? ⌘

  1. if <expression> .. else
  2. if <expression> .. else if <expression>
  3. if <expression> .. elif <expression> .. else
  4. if <expression> .. elif <expression> ..else <expression>
  5. If <expression>.. else <expression> .. efli

Which of these for commands are correct? ⌘

  1. for char in “ABCD”:
  2. for i in range(10):
  3. for num in (4,6,8):
  4. for k in 3+7:
  5. for (i=0; i<10; i+=1):
  6. for var in seq:

Which of these if statements are syntactically correct? ⌘

  1. if a and b:
  2. if len(s) == 23:
  3. if a but not b<3:
  4. if a ** 2 >= 49:
  5. if a != 3:
  6. if (a and b) or (c and d):

Modules and Packages ⌘

Check the correct answer(s).

Which of these import statements are correct? ⌘

  1. import re
  2. import re.sub
  3. from re import sub
  4. from re.sub import *
  5. from .re import *
  6. from re import *

Where does Python look for modules to import ⌘

  1. In the sys.path variable.
  2. In the current working directory.
  3. In the directory where the current module is.
  4. In the directory where Python was started.
  5. In the site-packages folder
  6. In directories in the PYTHONPATH variable
  7. In the root directory.

Which statements about packages are true? ⌘

  1. A package is a directory with modules.
  2. A package may contain zero modules.
  3. Packages in site-packages/ are imported automatically.
  4. A package must contain a __init__.py file.
  5. A package may contain no code.
  6. Packages are useless in small programs.

Which packages are installed by default? ⌘

  1. os – manipulating files and directories.
  2. psyco – makes Python faster
  3. time – accessing date and time.
  4. csv – reads and writes tables.
  5. numpy – number crunching.
  6. 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? ⌘

  1. f = open(“:::”)
  2. char = “abc”[7]
  3. a = (5+9) / (6-(2*3))
  4. num = [1,2,3][0]
  5. l = range(10)[:20]
  6. num = {1:'a'}['a']

Which are common types of Exceptions? ⌘

  1. ZeroDivisionError
  2. IOError
  3. ValueError
  4. NullPointerException
  5. KeyError
  6. InfiniteLoopError

Which commands for managing Exceptions exist? ⌘

  1. try: … else: ...
  2. raise ValueError('text')
  3. try: … except: … error:
  4. try: … except: ...

Working with Files ⌘

Check all correct answers

Which are correct commands working with files? ⌘

  1. for line in open(filename):
  2. f = open(filename,'w')
  3. open(filename).writelines(out)
  4. f.close()

Which statements about the csv module are correct? ⌘

  1. It can save tables of strings and numbers.
  2. csv reads tables of strings.
  3. csv cannot handle the quote character.
  4. 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? ⌘

  1. p = plot(x_data,y_data, 'k-')
  2. title(Name of the graph')
  3. xlabel('x axis name')
  4. axis([0,15,0,10])
  5. savefig('figure1.png'
  6. show()

Pages in category 'Language Basics - Excercises'

This category contains only the following page.