Python Programming · High School (9–10) · 1 course, 14 worked problems, 40 practice questions
No programmer writes everything from scratch. A module is a file of Python code that other programs can use, and a library is a collection of modules. You bring one in with import, for example import math, and then use math.sqrt() or math.pi. With from random import randint, you take just one name. The standard library comes with Python and includes math, random, statistics and datetime. Other libraries, such as NumPy and matplotlib, are installed separately. You can also write your own module of functions and import it into another program.
math and random to solve small problemsChoose an answer, then check it. Each one shows a full explanation.
A module geo.py holds a class. The program main.py, in the same folder, imports it in two ways.
geo.py:
class Point:
made = 0
def __init__(self):
Point.made += 1
main.py:
import geo
from geo import Point
a = Point()
b = geo.Point()
print(Point.made, type(a).__module__, isinstance(b, Point))
What does main.py print?
Three files sit in one folder. The module settings.py makes one object when it is loaded. The module helper.py changes that object. You run main.py.
settings.py:
class Settings:
def __init__(self):
self.theme = "light"
current = Settings()
helper.py:
import settings
def go_dark():
settings.current.theme = "dark"
main.py:
import settings
from settings import current
import helper
helper.go_dark()
settings.current = settings.Settings()
print(current.theme, settings.current.theme)
What does main.py print?
The program main.py imports floor from math, and then everything from its own module tools.py.
tools.py:
from math import ceil
def floor(x):
return int(x) + 10
main.py:
from math import floor
from tools import *
print(floor(2.7), ceil(2.1))
What does main.py print?
A module config.py holds a number and a class whose method uses that number. The program main.py imports both and then changes a variable called RATE in two places.
config.py:
RATE = 2
class Meter:
def reading(self, n):
return n * RATE
main.py:
import config
from config import Meter, RATE
m = Meter()
RATE = 5
first = m.reading(3)
config.RATE = 10
After this code, which of these expressions are True? Select all that apply.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
A module calc.py and the program main.py sit in the same folder. The program imports the module inside a function.
calc.py:
def double(n):
return n * 2
main.py:
import sys
def load():
import calc
return calc.double(4)
print(load(), "calc" in dir(), "calc" in sys.modules)
What does main.py print?
A program asks the platform module which version of Python is running. It prints nothing.
import platform
v = platform.python_version()
t = platform.python_version_tuple()
Which expressions are True after this code, on any computer? Choose every correct option.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
A program mixes the letters of a list with random.shuffle, then uses two more functions of random.
import random
letters = list("abcde")
random.shuffle(letters)
print(sorted(letters) == list("abcde"), len(set(random.sample(letters, 5))), random.randint(3, 3))
What does the program print, every time it runs?
A program imports math under a short name and works with three numbers read from a string.
import math as m
values = "9 16 25".split()
roots = [m.sqrt(int(v)) for v in values]
print(roots[0], m.ceil(roots[1] / 3), m.factorial(len(values)))
What does the program print?
A module strtools.py and the program main.py sit in the same folder. The program lists the module's names with dir().
strtools.py:
def shout(s):
return _clean(s).upper()
def _clean(s):
return s.strip()
VOWELS = "aeiou"
main.py:
import strtools
names = [n for n in dir(strtools) if not n.startswith("__")]
print(names, strtools.shout(" hey "))
What does main.py print?
The program is saved as main.py and run with the command python main.py, with nothing typed after it.
import sys
name = sys.argv[0]
print(name, name.endswith(".py"), name[:-3].upper(), len(sys.argv))
What does the program print?
Each one has a hint, the key idea and a full worked solution.