This tutorial is a composite of a number of sources:
[1]Python for Data analysis: Appendix Python Essentials
[2] https://developers.google.com/edu/python/introduction
I reccommend making a copy of this notebook and change the code to test your understanding of the concepts.
Python is an interpreted language. The interpreter executes one line at a time. Python can be invoked at the command line with the python command:
>>> is a prompt for the interpreter asking for input. To exit python type exit() or press the combination of keys: ctrl-D
The other way to use ipython is in a ipython notebook like this one. Then instead of >>> the lines of input are delineated by code cells labeled In \[N\]:
In [5]:
a=5
print ("a")
a
Out[5]:
The print statement printed out right after the input cell and the value of the last variable was displayed in an Out[N]: label in the code cell. Learn about the features of IPython Notebook from http://ipython.org/notebook.html. The use of the cell labels In and Out are from using ipython as the ehanced interpreter shell.
Python is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code. This makes the code short and flexible, and you lose the compile-time type checking of the source code. Python tracks the types of all values at runtime and flags code that does not make sense as it runs.
The emphasis in this language is readablity, simplicity, and explicitness. Some refer to python as executable psuedo code. The best way to learn this code is to run it.
Python uses whitespace (tabs or spaces) to structure code instead of using braces. Some suggest this makes it more readable but you do need to take care that the number and type of white spaces is consistent to maintain working code. The reccommended indent style is to use 4 spaces for each level of the code. You can set an IDE to replace tabs with 4 space characters to speed up development. The notebook interfaces does this for you.
Fix the indent in the following code block ( move the else: back inline with the if block ) and then rerun the cell.
In [6]:
for x in [1,2]:
if x == 1:
print 'x=1'
else:
print 'x!=1'
Semicolons (\;) are not needed to terminate a line but they can be used to concatenate a number of statements in a line
In [7]:
a=50;print("a equals "+str(a));b="b contains this string";b
Out[7]:
A fundemental characteristic of python is the consistency of its object model. Numbers, strings, data structures, classes, modules, ... are python objects ( Objects have a name and internal data). Even functions are first class objects and treated just like everything else in the language.
Good code is self commenting. Better code uses comments to convey intention and dispel confusion. A # stops the interpreter from treating the rest of the line as code, so effectively anything after a # is a comment.
In [8]:
length=7;width=10;#width=55; oops better not process
#the second width assignment
print (width)
# best to use comments indented to the block that
# the comment refers to ( not like this comment) but a #
# could be used to describe the line of code.
# See the first line in this cell
In [9]:
### Mutable and immutable objects