Sunday, August 02, 2020

Learning Python: Part 1

Python has been on my radar for years but I haven’t really had the motivation to learn it myself. It’s time though for several reasons. Reason number one is that I need a learning goal to keep stretching my mind and knowledge. I don’t have a bunch of students finding new puzzles to solve so I have to make my own mistakes.

First step was to pick a development tool. I have played with a bunch. IDLE, PyCharm, Processing, and some I forget. I didn’t like any of them. So I am using Visual Studio. I expect many to tell me that was/is a horrible choice but its the devil I know so there is that. Many of the tools feel like steps backwards to me. I’ve gotten spoiled by the Intellisence in Visual Studio among other features. IDLE felt like what I used to learn BASIC-Plus 40+ years ago. Anyway, I want to spend my time learning a language not an IDE.

OF course there are two parts to learning a new programming language. Part one is the basic syntax. Part two is the idiom of the language. The first part is easy. The second not so much.

I’ve started with easy stuff. Declaration of variables, simple math, declaration of functions, and control flow. IF statements and while loops were pretty straight forward. For loops are different and idiom rears its ugly head here first for me.

For loops in Python seem to be what I think of a ForEach loops in other languages. Very powerful and useful. On the other hand, how do I do the equivalent of:

for (i=0; i <= 100; i+=5)
for i =0 to 100 step 5

Or do I need to use a While loop to do this? Go ahead, show off in the comments and help me out.

String manipulation, one of my favorite things to code, is probably next for me.I think that will be ok.  Lists and dictionary come after that. I think I will have to read up on them as they look to be thought of differently than arrays in the languages I am used to using. They also look like fun and powerful to use. So there is that.

Classes after that. Hopefully that moves quickly.

Once I get some basics down I’ll start looking into various libraries and what not. Perhaps go back to try some graphics in Processing. Is there an equivalent to Windows Forms like I use with C# and Visual Basic? Or do I have to go backwards again to do that?

At least I’m having fun. Now to go crack a book.


Geoff Schmit said...

I hope you enjoy your journey. I tackled Python just a couple of years ago myself.

for i in range(0, 101, 5):

would do

Mike Zamansky said...

What's interesting is that people that come to Python from C type backgrounds keep thinking about the Python for as a counting loop whereas people who come to it from a language with higher order functions look at it as a foreach (which it is).

In the construct:

for i in range(0,10):

range returns a list (or really, a generator or whatever it's called in Python these days) and then the for loop iterates over each item.

I find it really interesting how people look at the construct very differently depending on background.

Alfred C Thompson II said...

I've been programming for almost 48 years. ForEach is relatively new to my experience. I don't think we even had While loops when I started. We faked it with if statements and GoTos. Of course in Assembly language we faked all sorts of loops. I think that the Assembly language for the VAX was the first time I saw a loop instruction in Assembly language.

Mike Zamansky said...

And then you learn that things like jg jl etc have a limited range so you have to do the small jump to the big jump :-)

Garth said...

I love VSC for Python. Easy to set up and just works. If you need a text here is a decent free one.

There is sort of a Forms in Python. Not pretty and not simple. VB and C# have spoiled users there.

Matija Lokar said...

When I teach my students loops, I explain them for loop in Python as:

use each element in the collection of things

So in

for x in {2, 5, 7, 10}:

x "becomes" each element in the set (namely 0, 3, 7 and 10)


for x in "alfred":

x "becomes" each letter in


for x in range(1, 101, 5):

x "becomes" 1, 6, 11, ... (as range(s, e, st) is a "counter" which starts with s and ends with e with increase of st


for x in open('file.txt'):

x becomes each line in the file.txt

Auro said...

There are lots of great books about Python, but as a learning tool, I appreciate Automate the Boring Stuff with Python, which is free online at Think Python is also a good intro book and is free online.