Download Introduction to Python A Tour of Python Algorithms

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
A Tour of Python
algorithms
algorithms
data types
data types
variables
variables
statements
Introduction to Python
statements
Today we’re going to take a quick tour of Python.
Input
functions
I
modules
L555
Dept. of Linguistics, Indiana University
Fall 2012
Introduction to
Python
programs
I
strings
You’ll see many different types of things, but nothing
in-depth
By the end, you should be able to:
I
editing files
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
1 / 19
Algorithms (reminder)
Introduction to
Python
Introduction to
Python
algorithms
algorithms
data types
variables
variables
statements
statements
print ’ Hello world . ’
functions
An algorithm is a set of instructions or a recipe for a
computer to carry out.
Hello World
data types
Input
Definition
2 / 19
Input
functions
modules
modules
programs
programs
strings
strings
print ’ Hello world . ’
print 4 + 5
editing files
editing files
3 / 19
Data Types
Data types are the building blocks from which everything
else is built
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}, ...
Introduction to
Python
4 / 19
Numbers
Introduction to
Python
algorithms
algorithms
data types
data types
>>> 2+2
4
>>> 3/2
1
>>> 3/2.
1.5
variables
statements
Input
functions
modules
programs
strings
variables
statements
Input
functions
modules
programs
strings
Python has integers and floating point numbers (& complex
numbers), and operations to convert between them:
editing files
editing files
>>> float(3)
3.0
>>> int(4.123)
4
Python is dynamically typed: you do not have to declare
what type each variable is
5 / 19
6 / 19
Variables
Introduction to
Python
What is a variable?
algorithms
algorithms
data types
What is a variable?
variables
variables
statements
Definition
Input
A variable is a name that refers to some value (could be a
number, a string, a list etc.)
Introduction to
Python
data types
statements
Definition
Variables
Input
A variable is a name that refers to some value (could be a
number, a string, a list etc.)
functions
modules
programs
Example
functions
modules
programs
Example
strings
editing files
strings
editing files
1. Store the value 42 in a variable named foo
foo = 42
7 / 19
Variables
Introduction to
Python
What is a variable?
Definition
algorithms
data types
variables
variables
statements
statements
What is the difference between an expression and a
statement?
functions
modules
strings
editing files
1. Store the value 42 in a variable named foo
Introduction to
Python
algorithms
programs
Example
Statements
data types
Input
A variable is a name that refers to some value (could be a
number, a string, a list etc.)
7 / 19
Input
functions
modules
Definition
programs
An expression is something, and a statement does
something.
editing files
strings
foo = 42
2. Store the value of foo+10 in a variable named bar
bar = foo + 10
8 / 19
7 / 19
User Input
Introduction to
Python
Example
name = raw input(’enter a number:
algorithms
algorithms
data types
data types
variables
variables
statements
statements
Example
Input
functions
1. Ask the user to input a name, and store it in the variable
name
’)
User Input
Introduction to
Python
Input
functions
1. Ask the user to input a name, and store it in the variable
name
modules
programs
strings
name = raw input(’enter a number:
editing files
2. create a new string with a greeting
’)
modules
programs
strings
editing files
greet = ’hello ’ + name
9 / 19
9 / 19
User input
Introduction to
Python
foo = int(raw input(’enter a number:
algorithms
algorithms
data types
’))
variables
Example
statements
1. Ask the user to input a number, and store it in the
variable foo
Introduction to
Python
data types
variables
Example
User input
Input
statements
1. Ask the user to input a number, and store it in the
variable foo
functions
modules
foo = int(raw input(’enter a number:
programs
’))
2. Add foo and bar together
strings
editing files
foo + bar
Input
functions
modules
programs
strings
editing files
10 / 19
User input
Introduction to
Python
Example
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
10 / 19
Functions
Introduction to
Python
algorithms
algorithms
data types
data types
variables
variables
statements
statements
Input
Input
What is a function?
functions
modules
Definition
programs
A function is a mini-program. It can take several arguments,
and returns a value.
strings
editing files
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
Modules
What is a module?
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
1. load the math module
Introduction to
Python
11 / 19
Modules
What is a module?
algorithms
data types
Definition
variables
statements
Input
functions
modules
programs
strings
import math
algorithms
data types
variables
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
statements
Example
programs
1. load the math module
editing files
Introduction to
Python
Input
functions
modules
strings
editing files
import math
2. Round 35.4 to the nearest integer
math.round(35.4)
12 / 19
12 / 19
Modules
What is a module?
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
1. load the math module
Introduction to
Python
Saving and executing programs
Introduction to
Python
algorithms
algorithms
data types
data types
variables
variables
statements
statements
Example
Input
functions
modules
programs
strings
editing files
Script File: hello.py
functions
# t h i s s c r i p t p r i n t s ’ hello , world ’ , to stdout
print ” hello , world ”
programs
Run the program:
import math
Input
modules
strings
editing files
python hello.py
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
String Basics
Introduction to
Python
13 / 19
Strings
Introduction to
Python
algorithms
algorithms
data types
I
variables
I
statements
I
Input
I
functions
I
I
Strings must be enclosed in quotes (double or single)
Strings can be concatenated using the + operator
Many ways to write a string:
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’
modules
programs
strings
editing files
I
I
data types
variables
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
14 / 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
15 / 19
Creating/Editing Python files
Introduction to
Python
algorithms
algorithms
data types
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
variables
statements
Input
functions
I
modules
programs
I
strings
editing files
I
statements
Input
functions
modules
programs
strings
editing files
Mac/Unix: pico, Emacs (or Aquamacs [which I use]),
Vim, and probably others
I
16 / 19
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
I’ll focus on emacs/aquamacs and IDLE (next slide) this
semester, but use what you like ...
17 / 19
IDLE
Introduction to
Python
Some text editors offer syntax highlighting, which shows
variable names, indentation, etc.
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
Emacs
Introduction to
Python
algorithms
algorithms
data types
data types
variables
statements
emacs is a fairly basic text editor that can be run in a
window or in the shell
I
to start emacs:
functions
emacs <filename>
modules
to quit:
programs
Input
functions
modules
programs
I
strings
Ctrl-x Ctrl-c
editing files
I
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)
variables
I
statements
Input
strings
editing files
save:
Ctrl-x Ctrl-s
I
search:
Ctrl-s
18 / 19
19 / 19