Python
Quick guide: http://www.tutorialspoint.com/python/python_quick_guide.htm
Learning Python: HTML: https://www.safaribooksonline.com/library/view/learning-python-5th/9781449355722
PDF:
https://ps07grupe3.googlecode.com/files/Learning Python (3th Edition) - Ascher, Lutz (O'Reilly, 2008).pdf
Python Cookbook: http://chimera.labs.oreilly.com/books/1230000000393/index.html
Multi-Line statements
total = item_one + \
item_two + \
item_three
Multi-Line statements inside [], {}, or () brackets
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
Quotes
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
Multiple Statements on a Single Line
import sys; x = 'foo'; sys.stdout.write(x + '\n')
Assigning Values to Variables
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
a = b = c = 1
a, b, c = 1, 2, "john"
Undefine variables
del var
del var_a, var_b
Strings
str = 'Hello World!'
print str # Prints complete string
print str[0] # Prints first character of the string
print str[2:5] # Prints characters starting from 3rd to 5th
print str[2:] # Prints string starting from 3rd character
print str * 2 # Prints string two times
print str + "TEST" # Prints concatenated string
Lists
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]tinylist = [123, 'john']print list # Prints complete listprint list[0] # Prints first element of the listprint list[1:3] # Prints elements starting from 2nd till 3rd print list[2:] # Prints elements starting from 3rd elementprint tinylist * 2 # Prints list two timesprint list + tinylist # Prints concatenated listslist[0] = 'xyz'del list[0]len(list[0]) # 33 in [1, 2, 3] # True[1, 2, 3] + [4, 5, 6] # [1, 2, 3, 4, 5, 6]['Hi!'] * 4 # ['Hi!', 'Hi!', 'Hi!', 'Hi!']list[-1] # 70.2list functions:
http://www.tutorialspoint.com/python/python_lists.htm
Tuples (read-only lists)
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )tinytuple = (123, 'john')onetuple = (123,)emptytuple = ()print tuple # Prints complete listprint tuple[0] # Prints first element of the listprint tuple[1:3] # Prints elements starting from 2nd till 3rd print tuple[2:] # Prints elements starting from 3rd elementprint tinytuple * 2 # Prints list two timesprint tuple + tinytuple # Prints concatenated liststuple functions:
http://www.tutorialspoint.com/python/python_tuples.htm
Dictionary
dict = {}dict['one'] = "This is one"dict[2] = "This is two"tinydict = {'name': 'john','code':6734, 'dept': 'sales'}print dict['one'] # Prints value for 'one' keyprint dict[2] # Prints value for 2 keyprint tinydict # Prints complete dictionaryprint tinydict.keys() # Prints all the keysprint tinydict.values() # Prints all the valuesdictionary functions:
http://www.tutorialspoint.com/python/python_dictionary.htm
Data Type Conversion
int(x [,base]) # Converts x to an integer. base specifies the base if x is a string.
long(x [,base]) # Converts x to a long integer. base specifies the base if x is a string.
float(x) # Converts x to a floating-point number.
complex(real [,imag]) # Creates a complex number.
str(x) # Converts object x to a string representation.
repr(x) # Converts object x to an expression string.
eval(str) # Evaluates a string and returns an object.
tuple(s) # Converts s to a tuple.
list(s) # Converts s to a list.
set(s) # Converts s to a set.
dict(d)
# Creates a dictionary. d
must be a sequence of (key,value) tuples.
frozenset(s) # Converts s to a frozen set.
chr(x) # Converts an integer to a character.
unichr(x) # Converts an integer to a Unicode character.
ord(x) # Converts a single character to its integer value.
hex(x) # Converts an integer to a hexadecimal string.
oct(x) # Converts an integer to an octal string.
membership operators (in, not in)
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];
if ( a in list ):
print "Line 1 - a is available in the given list"
else:
print "Line 1 - a is not available in the given list"
if ( b not in list ):
print "Line 2 - b is not available in the given list"
else:
print "Line 2 - b is available in the given list"
identity operators (is, is not)
operators precedence
** # Exponentiation (raise to the power)
~ + - # Ccomplement, unary plus and minus (method names for the last two are +@ and -@)
* / % // # Multiply, divide, modulo and floor division
+ - # Addition and subtraction
>> << # Right and left bitwise shift
& # Bitwise 'AND'
^ | # Bitwise exclusive `OR' and regular `OR'
<= < > >= # Comparison operators
<> == != # Equality operators
= %= /= //= -= += *= **= # Assignment operators
is is not # Identity operators
in not in # Membership operators
not or and # Logical operators
if statements
if cond:
....
elif cond2:
....
else:
....
if cond: ....
while-loops
while cond: ....
while cond:
....
while cond:
....
else
.... # post-loop block
for-loops
for num in range(10,20): ....
for letter in 'Python':
print 'Current Letter :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits:
print 'Current fruit :', fruit
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print 'Current fruit :', fruits[index]
(also can have "else" post-loop clause)
loop control
break
continue
pass # no-op statement
math functions: http://www.tutorialspoint.com/python/python_numbers.htm
escape characters: http://www.tutorialspoint.com/python/python_strings.htm
substrings
var1 = 'Hello World!'
var2 = "Python Programming"
print var1[0], var2[1:5]
strings: http://www.tutorialspoint.com/python/python_strings.htm
exceptions
try:
....
except Exception1:
....
except Exception2:
....
except (Exception3,Exception4,Exception5):
....
else:
.... # if there is no exception
finally:
.... # always executed
exception classes: http://www.tutorialspoint.com/python/python_exceptions.htm
raise [ex [, args [, traceback]]] # "ex" can be string, class or object
functions:
modules: http://www.tutorialspoint.com/python/python_modules.htm
time: http://www.tutorialspoint.com/python/python_date_time.htm
file IO: http://www.tutorialspoint.com/python/python_files_io.htm
http://www.tutorialspoint.com/python/file_methods.htm
http://www.tutorialspoint.com/python/os_file_methods.htm
classes: http://www.tutorialspoint.com/python/python_classes_objects.htm
regex: http://www.tutorialspoint.com/python/python_reg_expressions.htm
multithreading: http://www.tutorialspoint.com/python/python_multithreading.htm