Introduction To Python
Introduction To Python
http://www.flos-freeware.ch/notepad2.html
http://www.python.org/download/releases/2.6.6/
http://www.barebones.com/products/TextWrangler/download.html
Why Python?
• Python
C is much is faster but much harder to
learn
– easyand use.
to learn
• Java is somewhat
– relatively fast faster and harder to
learn and use.
– object-oriented
– widely
• Perl is a used
little slower and not as easy to
– fairly portable
learn.
Getting started on the Mac
• Start a terminal session
• Type “python”
• This should start the Python interpreter
(often called “IDLE”)
> python
Python 2.6.4 (something something)
details something something
Type "help", "copyright", "credits" or "license"
for more information.
>>> print "Hello, world!"
Hello, world!
The interpreter
• Try printing various things (in your spare time)
– Leave off the quotation marks.
– Print numbers, letters and combinations.
– Print two things, with a comma between.
– Enter a mathematical formula.
– Leave off the word “print”.
• The interpreter allows you to try things out
interactively and quickly.
• Use the interpreter to test syntax, or to try
commands that you’re not sure will work when
you run your program.
Your first program
• In your terminal, Ctrl-D out of the python interpreter.
• Type “pwd” to find your present working directory.
• Open TextWrangler.
• Create a file containing one line:
print “hello, world!”
• Be sure that you end the line with enter.
• Save the file as “hello.py” in your present working
directory.
• In your terminal, type “python hello.py”
• Each type of object has its own properties, which we will learn about in
the next few weeks.
• It is also possible to define your own types, comprised of combinations
of the six base types.
Literals and variables
• A variable is simply a name for an object.
• For example, we can assign the name “pi” to the
Number object 3.14159, as follows:
>>> pi = 3.14159
>>> print pi
3.14159
>>> pi = 3.14159
>>> pi = 3.14159
>>> pi = -7.2 you can see where
"variable" comes from -
>>> print pi pi can be changed
-7.2
The import command
• Many python functions are available only via
“packages” that must be imported (other functions
are always available - called "built-in").
foo and bar mean
>>> print log(10) something-or-
Traceback (most recent call last): other-goes-here
File foo, line 1, in bar
NameError: name 'log' is not defined
>>> import math
>>> print math.log(10)
2.30258509299
for now don't worry about
>>> print log(10) the details of the error
Traceback (most recent call last): message - just be aware that
this means there is an error
File foo, line 1, in bar in your program.
print log(10)
NameError: name 'log' is not defined
The command line
• To get information into a program, we can use the
command line.
• The command line is the text you enter after the
word “python” when you run a program.
python my-program.py 17
zeroth first
argument argument
Reading command line arguments
Access in your program like this: