Learning Python – the pretty devil

Switching from Java to python was not difficult for me. Though my code was not pythonic, I could code and have it up and run without any problem. We at Emerging Technologies Product Group, conduct workshops, and seminars on Python. After conducting a few hands-on workshops, I noticed a few some issues that most beginners to Python face.

I want to complie it into a list because I want to send a message to beginners out there that it is completely fine to make these mistakes while learning python and you are not alone and mentors to keep in mind while teaching Python to complete newbies.

Patterns of problems

Most of our students came with a knowledge of C type languages –  C/C++/Java. They get theoretical knowledge of these languages as a part of their curriculum. The patterns of problems were similar across the board. Here is a list of few problems.

The Problems

To decide whether to use Python 2 or Python 3.

Having both versions of python.org confused beginners. They had no clear direction on which one to choose. It is the first question asked by anyone who wanted to learn python.

shell vs. file

Most of the Python programmers understand how to leverage python shell and switch in and out of it. But for newbies, it was very confusing, mostly because they came from a background of compiled languages. Interactive shells and the ability to type and execute small code snippets was unknown to them. During their labs, they had written pieces of code using editors,  compiled and fixed errors and iterated till they got the program working.

Interactive input

Input in the shell was another problem. We used to show them simple programs to accept input from the console and perform some manipulations. raw_input() was particularly difficult for them. The program was waiting for them to give some input and they were waiting for the program to do something. That’s when I realized that we should always teach input statement with a prompt like raw_input("Enter your name ::")

Use Of colon

The students were so used to semi-colons of C/C++/Java that they mistook colons for semi-colons. Looking at the program projected on a screen did not help much, and they scratched their heads to find out whether it is a colon or semicolon. Most of them went with semi-colon since that is what other languages do. Thankfully I have never seen anyone putting semi-colon for all the lines.

Indentation

It is a nightmare for new Python programmers. Curly braces gave a visual attire to code blocks. Though python’s strict indentation make the code look neat and clean for beginners, it was a battlefield where they never knew whether a particular block should be indented or not.

Data type

Python’s weak and dynamic typing confused beginners. It was difficult because the code would give them no idea whether it is an integer or float or string. They all looked the same, to them. The lead to a host of problems.

Prints and Operations

Since the data-types are not obvious, they don’t know how to print those variables or how to play around with them. For ex., while learning lists, they were happy to create a list of 5 elements and print it using print statement. However, when you ask them to print the items in a list vertically, they panicked since they had no idea how to couple it up with Python’s for loop.

How and when to use dictionaries?

Things get worse when they discovered dictionaries since no other programming language had ever introduced such a data structure to them. While Java has HashMaps, they were not extensively used in Java programming examples they had seen. We realized that the best way to introduce data structures like dicts would be to show a table and show how to convert it into a dictionary representation.

There were other problems too. Like how to operate over the dictionary. How to find a key/value is present in the dictionary, return types on functions and so on.

TakeAway

What I learned from those workshops was that If your audience has prior programming knowledge, it is better to start with a comparison of what they already know and map it to similar programming constructs in Python.