5 Common Beginner Python Mistakes
Tags: beginners, python, codenewbie, programming
Published: 2019-12-11 10:45:43

It seems like a lifetime ago I cobbled together some HTML, CSS, and PHP to build a website as my first foray into programming, soon after downloading Borland’s Turbo C++ and trying to teach myself (it didn’t go so well at the time– *I was young!*).

Lately I’ve been working with University students that are learning Python for the first time. For some of them it’s their first time programming *full stop*. It’s been an interesting insight into what can cause absolute beginners to trip up, using that to help improve material provided to them.

Over the various sessions a number of issues popped up, some I expected, and some I really didn’t. So, without further ado, these were the most common mistakes the beginner Python developers made.

#### Python 2.7 vs Python 3

This was a classic one. The University computers had two versions installed on each machine: 2.7 and 3.6. This led to a couple of situations that typified the kind issues you may come across when running two different versions in parallel.

One student had the classic case of *“but it worked when I ran it yesterday!”…*

After taking a look over their code it became apparent that they had accidentally opened it in Idle Python 2.7.

Shortly after, another exasperated student let out a cry of defeat. They had the exact same code as their neighbour and yet one worked correctly and the other gave an error. Yep, one was running the code on Python 2.7 and the other 3.6.

In both of these instances the actual issue was with the input function <a href="https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html#parsing-user-inputs-via-input">not working as they expected</a> in Python 2.7.

As a quick aside: a common question from new programmers is <a href="https://learntocodewith.me/programming/python/python-2-vs-python-3/">what version should I learn?</a> In my humble opinion you should go for 3.x and then check out the differences in 2.7 if (lets be honest, still when even now) you come across it in the wild.

#### No Indentation
Straight off the bat: officially, <a href="https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces">spaces not tabs</a>. In practice: *do whatever and let the IDE/linter handle converting it*.

Unlike other languages that use curly braces to denote code blocks `(` `{` `}` `)`, Python relies on whitespace.

```python
for(i=0; i<10; i++) {
    printf("%d", i);
}
```
vs

```python
for i in range(0, 10):
    print(i)
```

Indented lines typically follow statements that end with a colon.

Which brings us on to…

#### Missing Colons
Colons at the end of if/for/while/def/class/etc *are required*, but; aren’t really necessary to make grammatical sense, leading beginners to tend to omit them from the end of statements.

By that I mean that both:

```python
if foo == bar
    print(foo)
```

and

```python
if foo == bar:
    print(foo)
```

Both make grammatical sense but the latter of the two is a) right in Python and b) <a href="http://python-history.blogspot.com/2011/07/karin-dewar-indentation-and-colon.html">easier to read</a>.

Having a colon also allows for one line statements:

```python
if foo == bar: print(foo)
```

When followed with a blank line; not that I would advocate doing that.

#### Writing Sentences
This was a peculiar one. A fair number of the students would start all new lines with a capital letter, so something like:

```python
should_end = False
 
while not should_end:
    an_input = input(“Type something”)
 
    if an_input == “exit”:
        should_end = True
```

became:

```python
Should_end = False
While not should_end:
    An_input = input(“Type something”)
 
    If an_input == “exit”:
        Should_end = True
```

Which left them with a whole host of errors!

Can’t really explain that one to be honest.

The Python naming conventions for variables and the such can be found in <a href="https://www.python.org/dev/peps/pep-0008/#naming-conventions">PEP 8</a> and reserved words begin with a lower case letter.

#### Type Conversion
An interesting one for the students to get their heads around. Particularly if they have prior experience with more forgiving languages.

One of the tasks required the student to read in a number and then compare it to 1 to switch to a certain state. It looked something like this:

```python
state = 0
a_number = input(“Gimme a number: “)
 
if a_number == 1:
    state = 1
else:
    exit()
```

Can you spot the problem?

The state never changed because the if statement is comparing a string with an integer, boiling down to:

```python
if “1” == 1:
```
The solution to this being converting the variable to an integer first:

```python
a_number = int(input(“Gimme a number: “))
```

Note: validation not included.