Top | Previous | Next |
Basic Syntax |
The basic syntax of Python is easy to learn, because there isn't much of it. Hello World Lets get right to everyone's favorite example: the following script will print out "Hello World" to the output console. print "Hello World"
The print keyword is a handy tool in Python, allowing you to put text into the output console. This is useful for debugging your scripts. You can print multiple things by separating them with commas. Variables Variables are created by simply assigning a value to them. Variables do not need to be declared, because Python has a dynamic type system. That means Python figures out the type of the variable on the fly, when the script is executed.
The following script would print out: 15 x=5 y=3 print x*y Strings, Numbers, and Booleans Literal strings can be typed in using either double quotes or single quotes. This can be handy when your string contains one quote or the other. You can also use the backslash character to escape special characters. Strings that contain characters beyond 7-bit ASCII, such as é or щ need to be marked as unicode strings by placing the letter u in front of the string. There is no harm in marking all strings as unicode strings. The following prints a unicode string: print u'été'
Numbers can just be typed in normally, like 42 or 3.14159. Python does not have a boolean type. 0 is false and 1 is true. For convenience, Python also has constants named False and True which can be used in place of 0 and 1. (This is an oversimplification, but should suffice for now). The following prints out "1" x="isn't this grand" y='isn\'t this grand print x==y The None Value There is a special value in Python called None (with a capital N). This is simply a special value that means: no value. This value is equivalent to Java's null value. Lists In Python, lists (arrays) are a built-in type that contains multiple other values. Lists can contain any type of items, and the items in a list do not all need to be the same type. You can create a list by enclosing multiple items in square brackets ([]), separated with commas. You can pull items out of a list with the square-bracket list index notation. Note that lists are zero-indexed, meaning that the first item in the list is at position 0. This code will print out "a list". a = ['this', 'is', 'a list', 8, 93.928] print a[2] Basic operators Python has all of the normal arithmetic operators you'd expect, addition(+), subtraction(-), division(/), multiplication(*), modulus(%), etc.
The comparison operators are just like in C: equals(==), not equals(!=) greater than (>), greater than or equal(>=), etc.
The logical operators are just typed in plain text: and, or, not.
These are just the basics. There are other operators, like bit shift operators. Read about them at: http://docs.python.org/library/stdtypes.html Comments Comments start with a hash sign. Add comments to your code so that when you go back to it after a long time, you know what the code is trying to do. # Prints out 'Hello World' 5 times. for x in range(5): print 'Hello world' Whitespace Perhaps its most unique feature, logical blocks are defined by indentation in Python. A colon (:) starts a new block, and the next line must be indented (typically using a tab of 4 spaces). The block ends when the indentation level returns to the previous level. For example, the following will print out "5 4 3 2 1 Blast-off". The final print is not part of the loop, because it isn't indented. countdown=5 while countdown > 0: print countdown, countdown = countdown - 1 print "Blast-off!" |