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/whatnotfor (loop stuff)
{
weeee;
fun;
}
this_isnt_part_of_the_loop(); - Pythonfor loop stuff:
weee
fun
this_isnt_part_of_the_loop()
Functions
Functions are defined using the def statment. It goes like this...def functionname(parameter1, parameter2):
code goes here
and here
etc.
code goes here
and here
etc.
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
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
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...
shopping_list[0]
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:
buy(thingy)
buy(thingy)
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
do this
Simple enough.
if x > 4:
print "OMG, x must be HUGE!"
print "OMG, x must be HUGE!"
Then there's else:
if x > 4:
print "OMG, x must be HUGE!"
else:
print "x ain't nuttin'"
print "OMG, x must be HUGE!"
else:
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"
else:
print "x is tiny"
print "OMG, x must be HUGE!"
elif x == 4:
print "x is four"
else:
print "x is tiny"
While Loops
While loops look just like if's and work like how you would imagine themwhile condition:
do this
do this
Strings:
Strings are concatenated with + signsgreeting = "Hello, " + "World!"
== and = are just like most languages
foo = "yo"
if (foo == "yo"):
print "foo is yo"
if (foo == "yo"):
print "foo is yo"
Comments
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 == 2) and (y > 5):
do_stuff()
do_stuff()
Congratulations, you have now learned Python in under 4 minutes!