Comments and indentation in Python
Single-line comments within Python begin with the # symbol, the rest of the line is treated as a comment.
Indentation in Python is used to indicate blocks, or groups of commands (unlike most languages, which typically use some form of bracketing). For example, in the code snippet below, each of the print statements is part of the if:
if (x < y): print "this is part of the if" print "this is too" print "This is not part of the if"
Variables do not need to be declared - they are dynamically created and their type is dynamically determined when you use them.
(DEBUGGING WARNING: if you make a typo in a variable name you won't get any warnings, it will just create a new variable with the new name.) Numeric expressions and assignment work pretty much as expected, though there is also a form of multiple assignment, e.g.
a, b, c = 3 # assign 3 to each x = y = z = 3 # assign 3 to each x = 3 * (y / 4) # etcType casting can be performed in a fashion similar to C, e.g.:
x = float(y) z = int(x)1 and 0 are used to represent the boolean true/false values.
String literals can be enclosed in either single or double quotes, and can be spread across multiple lines using \ as the continuation character, e.g.
str1 = "blah blah blah" str2 = 'blah blah blah' str3 = "this is the first part of str3 \ this is still part \ and so is this"In the example above,
str3
would be
"this is the first part of str3 this is still part and so is this".
If you don't wish the contents of the string (e.g. \n ) to be interpretted
you can create a "raw" string, e.g.
str4 = r"the newline is represented using \n"
You can also create Unicode strings, e.g.
str5 = u'This will be stored as a unicode string, not ascii'
There are a handful of useful string operators and built-in functions, including
Lists are comma seperated in square brackets, e.g.
Many of the strings operators can be applied to lists, with similar effects
(len(), +, [], etc)
One interesting difference is that you can change the contents of a list,
but you cannot change the contents of a string. For example, the
Lists are in fact objects, and there are a variety of other methods you can apply to them, including:
sort(), reverse(), pop(), append(item), extend(otherlist),
insert(position, element), remove(element), index(element),
count(element), x in l, x not in l, len(l), min(l), max(l), l * n, l1 + l1, etc.
(Note that
Python also supports sets, and the Python also supports the use of dictionaries (similar to the idea of associative arrays),
which allow the programmer to associate values with keywords in a dictionary, e.g.:
mylist = [3, 'blah', x, 100.0]
del
operator allows you to delete one or more elements from a list:
del mylist[i] # delete the i'th element
del mylist[m:n] # delete elements in positions m through n-1
pop()
and append(element)
can effectively be used to
treat a list as a stack, while pop(0)
and append(element)
can
effectively be used to treat a list as a queue.)
in
operator to test membership, e.g.:
myset = set([3, 'blah', x])
if 3 in myset:
print "yes"
else:
print "no"
# associate a value with a word
mydictionary['foo'] = "the meaning of foo"
# look up the value associated with a word
print mydictionary['foo']
# initialize a dictionary with a variety of keys/values
dict = { 'foo':"some meaning", 'Pi':3.14 }
There are also many other built-in methods for manipulating dictionaries, including