This link is refferenced all the time. Keep it open in tab. http://docs.python.org/2/reference/
The numbers before each section refer to this document. Use it to get more detail on a subject.
You're probably going to want to have Python installed before starting. The good news is that the majority of Linux users already have it, and if you don't, simply type 'sudo apt-get install python' (or equvalent package manager by your distribution). On Windows, you're going to need to install Python for Windows here: http://www.python.org/download/releases/ , although it is recommended that you use a UNIX based OS if you want to start writing programs with a purpose other that of simple maths and such.
While reading, the python docs are written very litterally, and cover all of their bases. Every sentence in this site can be deemed true, however this does make it a little dificult to understand. I recommend reading it either very lightly, or in intense detail. Don't be afraid to spend a few minutes on a single sentence, but try to pick up the main concepts too(Which I have outlined).
Code structure is MANDATORY.
While reading, feel free to test out what you learn in the python shell. On Linux, type 'python' in the terminal. In Windows, you have to open the python program.
Why is this guide different to all the other guides out there? Because this one is ultimately minimalist. It's impersonal and efficient. There's so much shit you don't need to know yet. But people go explaining how to use complicated libraries straight away, when all the learner wants to do it write something they understand, and know that shit that they learn can be applied to anything. All the other one off bullshit can be learned at their own leisure, and they can focus on learning logical thinking.
Variables are shorthand names for chunks of data. To make a variable called 'test' containing 'Hello World' for later use, do:
test = 'Hello World'
We can do maths with variables by putting numbers in them, and using them in an expression:
a = 1
b = 2
print a + b
The output of that code would simply be '3'
6.6: Print is used in almost every program. Allows you to show data to the terminal.
2.1.3: lines beginning with '#' will be ignored
2.3.1: Don't call variables or functions any of these:
and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try
7: Description of statements
The if, while and for statements implement traditional control flow constructs. try specifies exception handlers and/or cleanup code for a group of statements. Function and class definitions are also syntactically compound statements.
Each clause header begins with a uniquely identifying keyword and ends with a colon. A suite is a group of statements controlled by a clause. A suite can be one or more indented statements on subsequent lines. Example:
# Clause header breakdown:
if # unique keyword
1 == 1 # condition
: # always ends in a colon
if 1 == 1: # clause header
print "I am doing this code" # suite 1
print "And this code too" # suite 2
You need to know what these mean. Namely + - * / < > <= >= == !=
Learn them Here
5.{9..10}: Everything has a true or false value. This works for maths stuff like '1 == 2' returns false. '1' alone, however, returns true. This is used in statements mentioned above.
7.{1..4}: Syntax of basic statements if, while, for, and try.
2.1.8: How to correcty tab out your code (Just the normal way, but correct formatting is mandatory).
The indentation levels of consecutive lines are used to generate INDENT and DEDENT tokens. Here is an example of a correctly indented piece of Python code:
def perm(a):
# Compute the list of all permutations of a
if len(a) <= a:
return [a]
r = []
for i in range(len(a)):
s = a[:i] + a[i+1:]
p = perm(s)
for x in p:
r.append(a[i:i+1] + x)
return r
7.6: How to define functions.