# Uncomment and run the following line
#print "hello"
This statement will work in Python 2 but not in Python 3.
Python recognises 4 spaces (also produced by a tab) as an indent. If your indentation is not consistent, e.g. using 5 spaces instead of 4, python will give an IndentationError.
# Uncomment and run the following block
#for i in range(4):
# print(i)
# for j in range(3):
# print(i*j)
#
# print('done')
Remember that a loops do not include the last item specified in range. Forgetting this can lead to errors like this:
my_nums = []
for i in range(0,10):
my_nums.append(i)
# Uncomment and run the following line
#print(my_nums[10])
Scope is a tricky concept in Python, which relates to how variables are treated by different parts of your code. Scope is particularly relevant to functions that you have defined. Consider the following two examples:
a = 3
def f(x):
y = x**2 + a
print(y)
f(7)
52
def f(x):
y = x**2
print(y)
f(7)
# Uncomment and run the following line
# print(y)
49
In the first example, we create a variable a
outside of the function and then access it inside the function. This works fine. However, when we create a variable y
inside the function and try to access it outside the function, we get an error. This is because y
is 'local' to the function f
, whereas a
, which is not inside a function, is 'global'. Here's another example of how scope can lead to confusion:
a = 3
def f(x):
a = 5
y = x**2 + a
print(y)
f(7)
print(a)
54 3
Inside the function, we change the value of a
, and this is reflected in our calculation of y. But outside the function, when we access a
, it still has its original value. This is because a
inside the function is a copy of the original.
Scope is confusing! Even for more experienced programmers. So don't worry if it doesn't fully make sense, but be aware that it could be causing errors.
Note the following:
a = [3,5,7,9]
b = a
print(b)
a[1] = 11
print(b)
[3, 5, 7, 9] [3, 11, 7, 9]
Why did b
change, even though we only modified a
? Because here, b=a
actually makes a
and b
point to the same list. It's not making a new copy. If you want a
and b
to be unique copies of the same list, do this:
a = [3,5,7,9]
b = a.copy()
print(b)
a[1] = 11
print(b)
[3, 5, 7, 9] [3, 5, 7, 9]
Note: this applies not just to lists, but dictionaries, numpy arrays, and pandas dataframes too! In all cases, .copy() will make you a unique copy.
Often, the simplest way of finding problems in your code is simply to use print
statements to see what is happening to variables at different points in your code. A more advanced approach is to use the built-in debugger: https://ipython.readthedocs.io/en/stable/interactive/magics.html#magic-debug
If you don't understand why you are getting an error, it's good to look it up online. If you google the error message, you may find e.g. Stack Overflow pages with a solution. Issues specific to using functions from numpy, pandas, or other modules can often be found by looking up the relevant documentation pages: