Python Intro
CSC447 - Spring 2010
Getting Started with Python:
We will be using Python in two different, though related ways in this course. First, it is the language used to program Myro. Therefore, it is a language that we need to learn. Second, Python is also a language that is widely used for an ever-growing number of applications. Fortunately, among these is artificial intelligence, since it has a number of features which make it especially suited for A.I.
There are some issues regarding which version of Python to download. Myro seems to prefer 2.4.4, but it is not among the versions listed for download on their main page. I have both 2.4.4 and 2.6.4 on my machine at home and they do not seem to interfere with each other. For the time being, though, you may want to avoid 3.xx.
– Dowloading links
http://www.python.org/download/
http://www.python.org/download/releases/2.4/
http://www.python.org/download/releases/2.4.4/
– Studying
There are dozens of books. One that eases into Python:
Starting Out with Python, Toney Gaddis, Addison-Wesley
Rohrbach Library has two electronic books:
Python Pocket Reference, Mark Lutz
Python in a Nutshell, Alex Martelli
There is tons of material online - one good source:
The Python Tutorial http://docs.python.org/tutorial/
Other online sources:
Steven Lott: Building Skills in Python (recommended)
http://www.diveintopython.org/
http://heather.cs.ucdavis.edu/~matloff/Python/PythonIntro.pdf
But, since we will be using Myro, the best place to start:
Learning Computing with Robots, Deepak Kumar
http://cs.brynmawr.edu/~dkumar/Myro/Text/June09/PDF/LCRJune2009.pdf
Other Python books:
Object-Oriented Programming in Python – Goldwasser & Letscher
Python in a Nutshell, Alex Martelli
Exercise:: Getting Acquainted with Python
1. Download Python from: http://www.python.org/
2. Choose a convenient place for it and make a new folder called Learn Python
– Using Notepad save an empty file in that folder under the name StartPython.py
and close that file.
– Go to the folder Learn Python and right click on StartPython.py
– From the dialogue box that pops up, choose Edit with IDLE
– Two windows will open up
– one labeled StartPython.py (which is empty) and the other Python Shell
– choose Python Shell
– the cursor/prompt is >>>
3. At the prompt type the following, hitting enter after each line
5
3+24
7*77
999/443
True
False
true
9
[]
()
pow(3,7)
pow(989,756)
pow(989,756) % 989
(pow(989,7788) + 17) % 989
4. Choose the second window, the one labeled StartPython.py
– from the File menu, choose Save As and save the file as fact.py
– type in following import statement and function definition:
from math import *
def fact(n):
if n > 0:
return n*fact(n-1)
else:
return 1
– Notice how IDLE automatically indents and color-codes your code.
If the indentation and the colors do not match, then you probably mistyped
something.
5. From the Run menu choose Run Module
– when asked if it is OK to save, click on OK
– notice you are now back in Python Shell
– at the prompt type the following
fact (5)
fact (6)
fact (7)
fact(15)
fact(27)
6. Why are some of the answers followed by “L”?
7. At the prompt type the following
log10 (fact (5))
log10(fact (6))
log10(fact (7))
int(log10 (fact (5)))
floor(log10 (fact (5)))
ceil(log10 (fact (5)))
8. What do the answers represent?
9. Can you think of a way to find out how many digits 900! has? How many does it have?
10. EXTRA CREDIT: Write very efficient code to find out how many trailing zeros n! has for any input value n.