Python 1-Minute Tutorial for Those with Previous Programming Experience

Python 1-Minute Tutorial for Those with Previous Programming Experience

Last updated:

Useful if you need to do something useful with Python if you're in a hurry and already have programming experience in other programming languages.

Whatever is not mentioned here is probably like what you are already doing (if you program in C-derived languages (C++, Java, PHP) and Lisp-derived languages like Ruby)

just use lists

Lists are what you'll probably be using for most you do. Especially at the beginning, probably after as well. They can hold anything and have a simple interface. Here's a good place to start learning about lists.

my_list = [ 'foo', 'bar', 89 ]
my_list.append('a string')
my_list.append(45)

indentation counts

# RIGHT
if some_condition:
    # stuff
def my_function():
    # stuff

# wrong
def my_function()
    # stuff
end

# also wrong 
function my_function(){
    # stuff
}

elif

# RIGHT
if something:
    # stuff
elif something_else:
    # stuff
else:
    # stuff

# wrong
if some_condition:
    # stuff
else if other_condition:
    # stuff
if some_condition:
    # stuff
elsif:
    # stuff

no semicolon (';') at the end of lines

# RIGHT
foo = bar()

# wrong
foo = bar();

colon(':') after control structures and iterators

# RIGHT
for el in my_list:
    # stuff
if some_condition:
    # stuff

# wrong
for el in my_list
    # stuff
end

# also wrong
if some_condition{
    # stuff
}

for ... in is the simplest iterator for lists

for el in mylist:
    # use el

not instead of !

# RIGHT
if not some_function():
    # stuff

# wrong
if ! some_function():
    #stuff

True and False are case insensitive

# RIGHT
True
False

# wrong
true
false
TRUE
FALSE

None instead of Null or nil

Test whether a variable is None (Case-sensitive) using is None or is not None

# RIGHT
my_var = None
if my_var is None:
    # stuff
if another_var is not None:
    # stuff

Unix shebang for scripts

#!/usr/bin/env python