Download Introduction to Python - Computational Linguistics

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
Introduction to
Python
algorithms
data types
variables
statements
Input
Introduction to Python
functions
modules
programs
L555
Dept. of Linguistics, Indiana University
Fall 2012
strings
editing files
1 / 19
A Tour of Python
Introduction to
Python
algorithms
data types
variables
statements
Today we’re going to take a quick tour of Python.
I
I
You’ll see many different types of things, but nothing
in-depth
By the end, you should be able to:
I
I
I
Have some sense of what Python has to offer
Run some basic Python commands interactively
Write short Python programs and run them
Input
functions
modules
programs
strings
editing files
2 / 19
Algorithms (reminder)
Introduction to
Python
algorithms
data types
variables
statements
Input
functions
Definition
An algorithm is a set of instructions or a recipe for a
computer to carry out.
modules
programs
strings
editing files
3 / 19
Hello World
Introduction to
Python
algorithms
data types
variables
statements
Input
print ’ Hello world . ’
functions
modules
programs
strings
print ’ Hello world . ’
print 4 + 5
editing files
4 / 19
Data Types
Introduction to
Python
algorithms
data types
Data types are the building blocks from which everything
else is built
variables
statements
Input
I
Simple Types: numbers and strings
I
I
I
numbers: 3, 12.443, 89, ...
strings: ”hello”, ’manny’, ”34”, ...
Complex Types: lists and dictionaries (& sets & tuples)
I
I
lists: [1,2,3], [1,2,”a”], [”john”, ”george”, ”paul”, ”ringo”],
...
dictionaries: {”a”:1, ”b”:16}, ...
functions
modules
programs
strings
editing files
Python is dynamically typed: you do not have to declare
what type each variable is
5 / 19
Numbers
Introduction to
Python
algorithms
>>> 2+2
4
>>> 3/2
1
>>> 3/2.
1.5
data types
variables
statements
Input
functions
modules
programs
strings
Python has integers and floating point numbers (& complex
numbers), and operations to convert between them:
editing files
>>> float(3)
3.0
>>> int(4.123)
4
6 / 19
Variables
Introduction to
Python
algorithms
data types
What is a variable?
variables
statements
Definition
A variable is a name that refers to some value (could be a
number, a string, a list etc.)
Input
functions
modules
programs
Example
strings
editing files
7 / 19
Variables
Introduction to
Python
algorithms
data types
What is a variable?
variables
statements
Definition
A variable is a name that refers to some value (could be a
number, a string, a list etc.)
Input
functions
modules
programs
Example
strings
editing files
1. Store the value 42 in a variable named foo
foo = 42
7 / 19
Variables
Introduction to
Python
algorithms
data types
What is a variable?
variables
statements
Definition
A variable is a name that refers to some value (could be a
number, a string, a list etc.)
Input
functions
modules
programs
Example
strings
editing files
1. Store the value 42 in a variable named foo
foo = 42
2. Store the value of foo+10 in a variable named bar
bar = foo + 10
7 / 19
Statements
Introduction to
Python
algorithms
data types
variables
statements
Input
What is the difference between an expression and a
statement?
Definition
functions
modules
programs
strings
An expression is something, and a statement does
something.
editing files
8 / 19
Introduction to
Python
User Input
algorithms
data types
variables
statements
Input
Example
functions
1. Ask the user to input a name, and store it in the variable
name
name = raw input(’enter a number:
’)
modules
programs
strings
editing files
9 / 19
Introduction to
Python
User Input
algorithms
data types
variables
statements
Input
Example
functions
1. Ask the user to input a name, and store it in the variable
name
name = raw input(’enter a number:
2. create a new string with a greeting
’)
modules
programs
strings
editing files
greet = ’hello ’ + name
9 / 19
Introduction to
Python
User input
algorithms
data types
variables
Example
statements
1. Ask the user to input a number, and store it in the
variable foo
foo = int(raw input(’enter a number:
’))
Input
functions
modules
programs
strings
editing files
10 / 19
Introduction to
Python
User input
algorithms
data types
variables
Example
statements
1. Ask the user to input a number, and store it in the
variable foo
foo = int(raw input(’enter a number:
2. Add foo and bar together
foo + bar
’))
Input
functions
modules
programs
strings
editing files
10 / 19
Introduction to
Python
User input
algorithms
data types
variables
Example
statements
1. Ask the user to input a number, and store it in the
variable foo
foo = int(raw input(’enter a number:
’))
2. Add foo and bar together
foo + bar
Input
functions
modules
programs
strings
editing files
3. Calculate the average of foo and bar, and save it in a
variable named avg
avg = (foo + bar)/2
10 / 19
Functions
Introduction to
Python
algorithms
data types
variables
statements
Input
What is a function?
functions
modules
Definition
A function is a mini-program. It can take several arguments,
and returns a value.
programs
strings
editing files
11 / 19
Modules
What is a module?
Introduction to
Python
algorithms
data types
Definition
Python is easily extensible. Users can easily write programs
that extend the basic functionality, and these programs can
be used by other programs, by loading them as a module
Example
variables
statements
Input
functions
modules
programs
strings
1. load the math module
editing files
import math
12 / 19
Modules
What is a module?
Introduction to
Python
algorithms
data types
Definition
Python is easily extensible. Users can easily write programs
that extend the basic functionality, and these programs can
be used by other programs, by loading them as a module
Example
variables
statements
Input
functions
modules
programs
strings
1. load the math module
editing files
import math
2. Round 35.4 to the nearest integer
math.round(35.4)
12 / 19
Modules
What is a module?
Introduction to
Python
algorithms
data types
Definition
Python is easily extensible. Users can easily write programs
that extend the basic functionality, and these programs can
be used by other programs, by loading them as a module
Example
variables
statements
Input
functions
modules
programs
strings
1. load the math module
editing files
import math
2. Round 35.4 to the nearest integer
math.round(35.4)
3. Round the quotient of foo and bar down to the nearest
integer
math.floor(foo/bar)
12 / 19
Saving and executing programs
Introduction to
Python
algorithms
data types
variables
statements
Example
Script File: hello.py
# t h i s s c r i p t p r i n t s ’ hello , world ’ , to stdout
print ” hello , world ”
Input
functions
modules
programs
strings
editing files
Run the program:
python hello.py
13 / 19
String Basics
Introduction to
Python
algorithms
data types
variables
statements
Input
functions
I
Strings must be enclosed in quotes (double or single)
modules
programs
I
Strings can be concatenated using the + operator
strings
editing files
14 / 19
Introduction to
Python
Strings
algorithms
I
Many ways to write a string:
data types
variables
I
I
I
single quotes: ’string’
double quotes: "string"
can also use """ to write strings over multiple lines:
>>> """<html>
... <body>
... something
... </body>
... </html>
... """
’<html>\n<body>\nsomething\n</body>\n</html>\n’
I
I
statements
Input
functions
modules
programs
strings
editing files
There are string characters with special meaning: e.g.,
\n (newline) and \t (tab)
Get the length of a string by the len function
15 / 19
String indices & slices
You can use slices to get a part of a string
>>> s = "happy"
>>> len(s) # use the len function
5
>>> s[3]
# indexed from 0, so 4th character
’p’
>>> s[1:3] # characters 1 and 2
’ap’
>>> s[:3]
# first 3 characters
’hap’
>>> s[3:]
# everything except first 3 characters
’py’
>>> s[-4]
# 4th character from the back
’a’
Introduction to
Python
algorithms
data types
variables
statements
Input
functions
modules
programs
strings
editing files
16 / 19
Creating/Editing Python files
Introduction to
Python
algorithms
data types
Python files are simply text files, so we just need a text
editor. Some options:
I Windows: Notepad or Wordpad → Save as plain text
I
I
I
Sometimes Windows is set up s.t. it forces you to add a
.txt extension to your file.
This isn’t a problem, but to get rid of it, (I think) you
need to save as “All files” and also change your desktop
settings so that they show file extensions
variables
statements
Input
functions
modules
programs
strings
editing files
Mac/Unix: pico, Emacs (or Aquamacs [which I use]),
Vim, and probably others
I
I’ll focus on emacs/aquamacs and IDLE (next slide) this
semester, but use what you like ...
17 / 19
Introduction to
Python
IDLE
algorithms
data types
Some text editors offer syntax highlighting, which shows
variable names, indentation, etc.
variables
statements
Input
Integrated Development Environments (IDEs) offer syntax
highlighting, debugging features, streamlined code-running,
etc.
I One IDE which comes with Python is IDLE
(http://www.python.org/idle/doc/idlemain.html)
I
I
functions
modules
programs
strings
editing files
Windows: Once you’ve installed Python, this should be
available from Start → Applications → Python27 → ...
Mac: Check the Applications folder (or use spotlight to
find it)
18 / 19
Emacs
Introduction to
Python
algorithms
data types
variables
I
emacs is a fairly basic text editor that can be run in a
window or in the shell
I
to start emacs:
emacs <filename>
I
to quit:
Ctrl-x Ctrl-c
I
statements
Input
functions
modules
programs
strings
editing files
save:
Ctrl-x Ctrl-s
I
search:
Ctrl-s
19 / 19