Download Python

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Python
Introduction
Designed by
Guido van Rossum (1990)
Name based on favorite
show of Guido van Rossum
Which is Monty Python
Local Guru = Ruben
Why use it?
(1)Python is powerful... and fast
(2) Python plays well with others (Java etc.)
(3) Python runs everywhere (every system)
(4) Python is friendly... and easy to learn
(5) Python is Open Source
The interpreter
• Open IDLE (Python GUI)
• When you see ‘>>>’ the interpreter is ready
and you can type
• If you want to abort:
• Press ctrl+c
String
• Strings are delimited by single or double
quotes.
• We can combine strings with + and make
multiple copies with *
• 'spam’
• ‘spam’ * 3
• >>> spamspamspam
Print and comment
• In order to print something simply type print
in front of anything:
• Print ‘spam’
• Commenting is done with ‘#’
• Print ‘spam’ #this is going to print span
Variable
• Variables are named locations that hold
values using the ‘=‘ sign
• chocolate2 = 1.99
• Legal variable names must start with a letter.
They can include letters, digits
• and underscores (_). No spaces or dashes!
• The decimal point in numbers is written with
a dot (American style).
One step at a time
• Expressions calculate a value. Assigment saves
only the result.
• mango = 2.99
• mango = 1.50
• n = (2 * mango ) + 2.50
• mango = 0.99
• print n # What will this print ?
• We can increment a variable by adding one to
its old value: boxes = boxes + 1
• In javascript, but not python: boxes++ ;
List
• A list can contain anything, or any mix of things
(including other lists)
• Primes = [8,9,10]
• mix = [ 3, 4, 5, " text ", primes ]
• print mix # Note the brackets showing a sublist
• print mix [ -1] # Last element : the entire list “
• Print mix[0] # first element
While loop
•
•
•
•
•
•
Loops allow us to execute part of a program more than once.
n=0
While n < 10: #always use the :
print " SPAM !"
n=n+1
n is a counter that we use to control how many times the loop is
executed.
• We must increase its value on each pass through the loop, or we
get an infinite loop.
•
Note that python does not use curly braces for loops. Humans rely
on indentation, so it’s simpler not to have two systems.
If and else
• If: conditional statements used to test
whether or not some condition is met and
then continue executing the script
• n = “bla”
• If n == “bla”:
• Print “yes”
The for-loop
• A for loop counts through a range of numbers,
running a block of code each time it iterates
through the loop
• Names = [‘a’,’b’,’c’]
• For name in names:
– Print name
Error?
•
•
•
•
•
•
x = ['a', 'b', 'c']
Print x[3]
IndexError: list index out of range
Search in google: results from stackoverflow
You are trying to access an element of the list
that does not exist.
Function
• A function is defined with ‘def’
• Def counting(number):
– Value = number+1
– Return value
Counting(5)
>>> 6
Documentation
• http://docs.python.org/ (practical)
• The book:
• https://sites.google.com/site/naturallanguage
toolkit/book
• Think python (very useful) :
• http://www.greenteapress.com/thinkpython/t
hinkpython.pdf
Assignment
• (1) print the values from 0 to 10 using a while
loop
• (2) print the total of the values from 0 to 10
using a while loop
• (3) try ‘beer’ assignment
• Write a python program that generates the lyrics to the song
• "99 bottles of beer". Leave a blank line between verses.
• The song's simple lyrics are as follows:
•
•
•
•
99 bottles of beer on the wall,
99 bottles of beer.
Take one down, pass it around,
98 bottles of beer on the wall.
• The same verse is repeated, each time with one fewer bottle. The song
• is completed when the singer or singers reach zero. After the last
bottle
• is taken down and passed around, there is a special verse:
•
•
•
•
No more bottles of beer on the wall,
No more bottles of beer.
Go to the store and buy some more,
99 bottles of beer on the wall.
Some more advanced
• For those of you who can program already:
Print the numbers as text
• ("ninety nine bottles") instead of printing the
digits.
• Hint: While developing the program, start
from a small number, and
• change it to 99 when you are done