Additional Python Resources
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
If you're having trouble with a particular concept or simply want to have access to more information, try one of the following links.
- Official Python 3 Documentation - "official"/technical explanation of what a particular function/operator does, examples of correct syntax, what the various libraries are, etc.
- Get Programming: learn to code with Python by Ana Bell An intro to programming assuming absolutely no knowledge of programming at all.
- Dive Into Python - another survey of Python syntax, datatypes, etc.
- Think Python by Allen Downey - a good general overview of the Python language. Includes exercises.
- The Official Python Tutorial - self-explanatory
- Learn Python the Hard Way - (note: for Python 2) another free online text
- Reserved Keywords in Python - don't use these as variable names
- PEP 8 - Style Guide for Python Code - learn what is good and bad style in Python
- CheckIO - learn Python by exploring a game world
- Invent with Python - develop your Python skills by making games or hacking ciphers
- Codecademy - (note: for Python 2) learn Python by building web apps and manipulating data; interactive tutorial sequence
- Python Tutor - interactive tutorial sequence of exercises
- Python Tutor - an excellent way to actually visualize how the interpreter actually reads and executes your code
- DiffChecker - compares two sets of text and shows you which lines are different
- Debugging in Python - steps you can take to try to debug your program
- Python Tools for Visual Studio - Visual Studio plug-in enabling Python programming
- Stack Overflow - a large Q&A forum for programming concepts (not just Python). Try searching here before you post on the edX forum, and you may find that someone has already answered your question.
- Python Challenge - a series of puzzles you can try to test your Python abilities
- Project Euler - additional programming challenges you can try once your Python knowledge becomes stronger; problems are sorted by increasing difficulty
- Coding Bat - problems you can solve within an online interpreter, specifically loops and lists
- Codewars - improve your skills by training on real code challenges
- Dictionaries - exercises on Python dicts
- Classes and Object Oriented Programming - exercises on Python classes
x
has been bound to a value of type int
, and that the variable s
has been bound to a value of type str
. Write code that prints the value of x
if that value is the same as the number of characters in s
; otherwise it prints the string 'unequal'
.
# For example:
# when x = 3 and s = 'abc'
# the code prints 3
# when x = 3 and s = 'abcde'
# the code prints 'unequal'
s
has been bound to a value of type str
. Write code that prints the number of times a lowercase vowel (one of 'a', 'e', 'i', 'o' or 'u') appears in s
.
# For example:
# when s = 'abcdefghi'
# the code prints 3
x
is a positive number bound to a value of type int
. Write code that prints the largest power p (an integer) such that 2^p evenly divides x
.
# For example:
# when x = 45
# the code prints 0
# when x = 48
# the code prints 4
def mult(s1, s2):
"""
s1 and s2 are non-empty strings of equal length
If s1 and s2 can be converted to integers, (i.e. s1 and s2 are strings
containing only digits), returns the sum of pairwise products of every index.
The pairwise product of index i is the result of multiplying the digit in s1
and s2 that are at index i. If s1 or s2 cannot be converted to ints, return None.
"""
# your code here
# For example:
print(mult("3", "2")) # prints 6
print(mult("123", "200")) # prints 2, because 1*2 + 2*0 + 3*0 = 6
print(mult("12a3", "2.0.1")) # prints None
print(mult("0123", "4321")) # prints 10
def binary_to_intlist(s):
"""
s is a string made up of digits 0 and 1, representing a binary number.
Converts s to its whole number representation in base 10, and returns each
digit of that whole number representation as an INTEGER element in a list
(in order from most significant to least significant).
"""
# your code here
# For example:
s = "0001101"
print(binary_to_intlist(s)) # prints the list [1, 3]
s = "100"
print(binary_to_intlist(s)) # prints the list [4]
def coeffs_tup(x, y, z, total):
"""
x, y, z are positive ints
Returns the tuple of non-negative and integer coefficients a, b, c
(in this order) such that a*x+b*y+c*z = total where total is a
non-negative integer. If multiple values of a, b, c exist, choose any.
If no such coefficients exist, returns None.
"""
# your code here
# For example:
print(coeffs_tup(1,2,3,1)) # prints (1, 0, 0) because 1*1+0*2+0*3 = 1
print(coeffs_tup(6,2,4,5)) # prints None
print(coeffs_tup(6,2,3,5)) # prints (0, 1, 1) because 0*6+1*2+1*3 = 5
def apply(a, b, c, f):
""" Assumes a, b, c are non-empty lists of ints and of the same length.
f is a function that maps an int to an int.
Returns a list A of the same length as a,b,c. For all i in
0...len(a)-1, A[i] is the result of applying f to the smallest of
the values at index i of a, b, and c.
"""
# your code here
# For example:
a = [12, 3, 8]
b = [-11, 4, -7]
c = [3, 2, 2]
print(apply(a, b, c, abs)) # prints the list [11, 2, 7]
def get_min(d):
""" Assumes d is a non-empty dict mapping lower case letters to ints.
Returns the value associated with the key in d that occurs the earliest in the alphabet.
"""
# your code here
# For example:
d = {'x': 11, 'y': 3, 'c': 4}
print(get_min(d)) # prints 4
def sum_floats(L):
""" Assumes L is a list
Returns the sum of all of the elements in L that can be cast to a float.
"""
# your code here
# For example:
L = ['a', []]
print(sum_floats(L)) # prints 0
L = [1, '2.1', 'a', -1, '-1', [], (2,)]
print(sum_floats(L)) # prints 1.1