Login | Register
Nerd ParadiseArtisanal tutorials since 1999
This is a crash course on steroids. It assumes you already know another language...or two...and builds off your previous knowledge. No point in reading through a 1-inch-thick book that spends most of the time explaining concepts you already know from another language.

Explaination of the Syntax

Most languages use curly braces to group pieces of code. Like everything in a for loop will be surrounded by a { and a }. Not in Python. Indention is the key. Everything on the same indention level is part of the same group of code.

  • C/Java/whatnot
    for (loop stuff)
  • Python
    for loop stuff:
Each statement is (usually) on its own line whereas a semicolon is used to delimit lines in most other programming language syntaxes.


Functions are defined using the def statment. It goes like this...
def functionname(parameter1, parameter2):
    code goes here
    and here 

And you can have more indentions under a def in the same way you would have used curly braces before...

def foo():
    do stuff
    do more stuff
    for loop time:
        this is part of the loop
        so is this
    but this is not, this is just part of the rest of the function


Lists are very versatile.
shopping_list = ["bread","milk","hamster brains"]

then if you want to add an item to the list...
shopping_list = shopping_list + ["turnips"]

If you want to access just one item in that list you can do it like this...

That's the first item in the list. shopping_list[1] would be the 2nd, etc.

If you want a range of items in the list you can do that with colon magic...
shopping_list[0:3] is the first 3 items in the list. The way it works is the first number is where you want to start, and the second number is where you want to end (not how many you want). It goes up to but not including the 2nd number.

Lists do not all have to contain the same type of data. It's ok to mix strings and numbers and objects and nested lists, etc.

For Loops

This is the biggest change from most languages. Traditionally, you have a variable and increment it until some condition is false. With for loops in Python, you have a list and a variable and then the interpreter goes through each item in the list making the variable equal to each one as it goes...
for thingy in shopping_list:

there's a loop (assuming we're using the same shopping list from earlier)

If you want a list of numbers that go from x to y, here's how you do that:
list_of_nums = range(x, y + 1)

then list of nums will be a list of numbers...in that range. It always starts at the first number and goes up to (but not including) the 2nd number. That's what that + 1 thing is in there for.

If Statements

if some condition:
    do this

Simple enough.

if x > 4:
    print "OMG, x must be HUGE!"

Then there's else:
if x > 4:
    print "OMG, x must be HUGE!"
    print "x ain't nuttin'"

else if's are denoted by elif's

if x > 4:
    print "OMG, x must be HUGE!"
elif x == 4:
    print "x is four"
    print "x is tiny"

While Loops

While loops look just like if's and work like how you would imagine them
while condition:
    do this


Strings are concatenated with + signs
greeting = "Hello, " + "World!"

== and = are just like most languages
foo = "yo"
if (foo == "yo"):
    print "foo is yo"


Comments are done with a #.
print "this is some code" #this line prints stuff

And and Or

There are no && or ||, it's just "and" and "or"
if (x == 2and (y > 5):

Congratulations, you have now learned Python in under 4 minutes!
Hey, there, Python folks. Hope you enjoyed the post. I just wanted to give a quick shout-out for a weekly Python code golf that I recently started up over on StringLabs.io. If you like Python puzzles please come check it out!