Python chapter 1

Intro
simplest use
Declaring variables
Reading Input
Errors

Intro

notes taken from Python in easy steps

is a high level language (human-readable) which is processed by a python interprutor
python includes a big list of modules which can be included in your code.
it uses english key words
it uses indentation to group together statments
python 3.x is not compatiable with 2.7
named after monty python flying circus
installed by default with centos
to check version run python -V or python3 -V
interactive mode needs you to enter python and the command prompt and then you get the >>>
prompt. This is good to test snippets of code and for debugging.

simplest use

>>> 8 + 4
12

[root@testing python]# cat helloworld.py
print( "hello world" )
[root@testing python]# python ./helloworld.py
hello world

or

[root@testing python]# cat helloworld.py
#!/usr/bin/python
print( "hello world" )
[root@testing python]# ./helloworld.py
hello world

Declaring variables

[root@testing python]# cat var.py
#!/usr/bin/python
a = 2
print (a)
[root@testing python]# ./var.py
2
[root@testing python]#

reading input

note the differences between version 3.3.3 and the default 2.6.6

to install 3.3 follow details at http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

[root@testing python]# cat input.py
#!/usr/local/bin/python3.3
choice = input("enter a number less than 10:")
print ("welcome you entered the number" , choice )

[root@testing python]# ./input.py
enter a number less than 10:bla
welcome you entered the number bla

[root@testing python]# cat input_old.py
#!/usr/bin/python
choice = input("enter a number less than 10:")
print ("welcome you entered the number" , choice )

Errors

there are 3 sorts:

1) syntax error - you have messed up the code i.e

[root@testing python]# cat error.py
#!/usr/bin/python
print( "hello world )
[root@testing python]# ./error.py
File "./error.py", line 2
print( "hello world )
^
SyntaxError: EOL while scanning string literal
[root@testing python]#

2) runtime error - the program runs but errors during the run
it is reported as an exception i.e

[root@testing python]# cat error_2.py
#!/usr/bin/python
a = 2
print ( a1 )
[root@testing python]# ./error_2.py
Traceback (most recent call last):
File "./error_2.py", line 3, in
print ( a1 )
NameError: name 'a1' is not defined
[root@testing python]#

3) semantic error, the code is run but does not output an error
i.e the order precedence has not been specified in an expression

questions

1) run python in interactive mode and what are the advantages
1b) in interactive mode add 2 numbers together
2) why in python a good scripting language
3) how do you check which version of pyhton you are using
4) create a hello world program
5) create a program to declare the variable a is 2
6) read in an input from a user
7) what are the 3 sorts of errors you can get
8) what you should be careful of between python version 2 and 3?