PYTHON
FROM THE BOOK “THINK PYTHON”
HTTP://WWW.GREENTEAPRESS.COM/THINKPYTHON/INDEX.HTML
HOW TO THINK LIKE A COMPUTER SCIENTIST
PYTHON HOME INSTALLATION
We will be using Python 2.7.6 for this class. There is a newer
version Python 3.3.3 but it is a little different in syntax. You can
use it but you will be responsible for the syntax conversions.
On your home machine go to http://www.python.org/getit/
Download the version you need, I picked
Python 2.7.6 Windows X86-64 Installer (Windows AMD64 / Intel
64 / X86-64 binary [1] -- does not include source)
Note there is a Mac version if you prefer.
We may well add additional libraries ( such as Swampy ) as we
go on.
http://www.greenteapress.com/thinkpython/swampy/
for other downloads check out
http://www.lfd.uci.edu/~gohlke/pythonlibs/
COMPUTER LANGUAGES
High Level Languages (Python, Java, C++…)
• Easy to program in and maintain
• portable
Low Level Languages
• Machine language ( binary , runs on machine)
• Assembly language ( Natural language version of Machine
TRANSLATION
Interpreters
• A program that reads a high level language and executes it,
often one line at a time.
Compilers
• Reads the entire program (source) and translates it to a
machine program (binary) that is directly executable.
Hybrid
• Java is translated to an intermediate code( byte code) and
that code is interpreted.
PYTHON
Interactive mode ( for testing and learning)
• Here you can type in a single instruction and have the Python
interpreter execute it immediately.
>>> 1 + 5
<- input
6
<- output
Script mode (normally what you do)
• In this case you write a program and store it in a file name.py
• The file name.py is then executed by Python in its entirety
WHAT IS A PROGRAM?
A program is a sequence of instruction that specifies how to
perform a computation.
Most languages have instructions that perform
•
•
•
•
Input ( reads data from the keyboard, file, …
Output ( Displays data on screen or writes it to a file, ….
Math ( performs mathematical operations)
Conditional Execution ( based on certain conditions
appropriate code sequences are executed.
• Repetition ( perform sections of code repeatedly.)
ERRORS
Syntax errors
• Instructions are written incorrectly
• prnt (5)
# did not spell print correctly
Semantic errors
• Instruction runs but the output is not what you thought would happen.
• These are the hardest errors to find and fix.
Runtime errors
• Here an error occurs while the program is running ( after syntax checking)
• These include divide by 0 errors as well as many others
• IE something bad happens when running after translation.
Debugging
• The different things you do to find and repair your errors. This is the detective work in software
development.
LANGUAGES
Natural languages (English, Spanish, French)
• Spoken languages
• Ambiguous
• Full of idiom and metaphors
Formal Languages
•
•
•
•
Special purpose languages (Chemistry, Math, Programming)
Strict rules and syntax
Unambiguous
Mean exactly what is said
FIRST PROGRAM
The first program many people run when learning a new
language is often referred to as the Hello World! Program
In Python it is a single liner
print(‘Hello World!’)
Running within the interactive mode we have
>>> print(‘Hello World!’)
Hello World!
Mistype the above command in a variety of ways and note
the interpreters comments.
LETS DO IT (METHOD 1)
Run the Python (IDLE) Gui app and type in the print
command. You should see something like
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print "Hello World!"
Hello World!
>>>
SCRIPT (METHOD 2)
Here we create a file named hello.py and run it from the
Python editor.
Click File then New File and type-in print ‘Hello World!’
Save the file under name hello.py
Position the edit window and the interactive window side by
side
Now in the edit window click Run and then run module
You should see the output within the interactive window
Add a new print instruction of your choosing and run again
Do this several times.
RUN SCRIPT FROM DOS
(METHOD 3)
The hello.py file can also be run from the DOS command line.
1. Go to the directory that contains the file hello.py
2. Click shift-right click. Then select option ‘open command
window here’
3. You should see a DOS window pop up.
4. Type DIR and check that hello.py is there
5. Now type python hello.py at the DOS command prompt
6. You should see the output
Hello World!
STUDY THE GLOSSARY
On page 7 there is a list of words that you need to know.
Most of these we will use often so they will become second
nature.
These include:
HLL,LLL, portability, interpret, compile, source, objectcode,
Executable, prompt, script , interactive mode, script mode,
program, algorithm, bug, debugging, syntax, syntax error,
semantics, exception (runtime error), natural language,
formal language, token, parse, print statement
Descargar

Python - Department of Computer Science