Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
INFO1903: Introduction to Python
Bob Kummerfeld
Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 2
Outline
1 Admin
2 Python
3 Interpeter
4 BOTE
5 Experiment
6 Variables
7 Summary
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 3
Official notice
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been reproduced and communicated to you by or
on behalf of the University of Sydney pursuant to Part VB of the
Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright
under the Act. Any further copying or communication of this
material by you may be the subject of copyright protection under
the Act.
Do not remove this notice.
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 4
Python labs
Lab work related to Python begins in week 3
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 5
Python was developed by Guido van Rossum
• started over the Christmas break 1989
• developed in the early 1990s
• while Guido was at CWI and CNRI
• name from Monty Python’s Flying Circus
• Guido is known as the Benevolent
Dictator for Life (bdfl), meaning that
he continues to oversee Python’s
development.
http://www.python.org/~guido/
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 6
Python inherits from abc
• Guido previously worked on abc at CWI in the early 1980s
• abc was designed for beginners and non-professionals
• abc placed a high value on clarity:
• blocks identified by indentation
• minimal use of punctuation
• one preferred way of doing things
• but lacked extensibility and used idiosyncractic syntax and
terminology
• Python was designed to appeal to the Unix/C community
http://www.python.org/doc/essays/foreword/
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 7
What the Python language manual says...
Python is an interpreted, object-oriented, high-level programming
language with dynamic semantics. Its high-level built in data structures,
combined with dynamic typing and dynamic binding, make it very
attractive for rapid application development, as well as for use as a
scripting or glue language to connect existing components together.
Python’s simple, easy to learn syntax emphasises readability and
therefore reduces the cost of program maintenance. Python supports
modules and packages, which encourages program modularity and code
reuse. The Python interpreter and the extensive standard library are
available in source or binary form without charge for all major platforms,
and can be freely distributed.
http://docs.python.org/ref/front.html
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 8
huh?
interpreted you don’t need a separate compilation stage
high-level it protects you from low-level details
dynamic you can change a running program on the fly
builtin part of the core language (not an external library)
data structures ways of storing and manipulating data
script/glue simple programs that control other programs
typing knowing a variable holds an int, real, string, . . .
syntax the grammar which defines the language
library a reusable collection of code
binary an executable you can run immediately
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 9
So is Python a scripting or programming language?
• scripting started with simple batch programs
e.g. .BAT files in Windows and Unix shell scripts
• these basically controlled other, mainly compiled, programs
just like running them in an interactive shell
• used to speed up repetitive system administration tasks
• they were (almost) always interpreted
• the idea of scripting has broadened:
• interpreted languages got fast enough for complex systems
• the distinction between compiled and interpreted blurred
(e.g. Java and the Java virtual machine)
• the distinction between scripting and large-scale tasks blurred
(e.g. backends to dynamic websites)
• so the answer is both!
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 10
So why use Python?
• the clean simple syntax is very easy to read and write
it has been called executable pseudo-code
• it encourages good programming habits
• it still has a lot of power and features:
objects, exceptions, generators, iterators, threads,. . .
• but these don’t get in the way of beginners
• and a huge standard library (batteries included):
{}, os, email, urllib, bsddb
• Python scripts are portable to anywhere the interpreter runs
• and the interpreter is freely available for:
Windows 95-XP/CE, Mac (0S 9-X), any Unix, Amiga, JVM,. . .
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 11
Still not convinced?
• Python lets you get on with writing interesting programs
• Python programming is fun!
[Python is] the most efficient language I’ve ever used. It’s 10 times better
than any of the other tools I have used. It’s free, it’s object-oriented, it
adapts to everything, it runs on everything. There is almost an
indescribable, ’quality without a name’ attraction on my part
Bruce Eckel, author of Thinking in C++, Thinking in Java
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 12
Lots of big organisations are using Python
Python is everywhere at ilm. It’s used to extend the capabilities of our
applications, as well as providing the glue between them. Every cg image
we create has involved Python somewhere in the process.
Philip Peterson, Principal Engineer R&D, Industrial Light & Magic.
Python has been an important part of Google since the beginning, and
remains so as the system grows and evolves. Today dozens of Google
engineers use Python, and we’re looking for more people with skills in
this language.
Peter Norvig, Director of Search Quality, Google.
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 13
Download Python from the website
• run the installer from http://python.org/download
• Python 2.7.2 is the latest version (of Python 2.X)
• note: do not install Python 3.X
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 14
Installing Python
• if possible install Python as a user with administrator privileges
• on Windows, it’s best to install it in the default location,
which is probably C:\Python27
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 15
Running Python
• Under Unix/Linux, just type python at the shell prompt
• Under Windows, you can run it from a command shell:
• goto Start | Run, and enter cmd, and press OK.
• then type python
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 16
Running the Integrated DeveLopment Environment
• idle is an ide for Python written in Python
• Under Unix/Linux, just type idle at the shell prompt
• Under Windows, goto Start | Programs | Python2.7
and select IDLE (Python GUI):
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 17
Hello World is tiny in Python
• Hello World is the canonical first program to write
• In Python, Hello World is only one line!
• You can type it directly into the Python interpreter:
1 >>> print "hello world"
2 hello world
3 >>> x = "hello world"
• The colours in this (and all examples) matches the syntax
highlighting that idle produces.
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 18
Hello World in idle
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 19
Writing Python in idle
• idle is an ide for Python written in Python
• Under Unix/Linux, just type idle at the shell prompt
• Under Windows, goto Start | Programs | Python2.5
and select IDLE (Python GUI):
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 20
Only use the shell for experimentation
• The Python shell in idle is great for experimenting with
• But, for writing programs you want a separate editor window
• That way, you can rerun your programs without retyping them
• Goto File | New Window or press Ctrl-N:
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 21
Run your programs from the editor window
• The editor window doesn’t have command prompts (the >>>)
• It has additional menu items for formatting Python code
• To get syntax highlighting and to run your program you must
first save the file with a .py extension
• To run your program, goto Run | Run Module or press F5.
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 22
Back of the Envelope
• How long would it take you to fill a CD by typing?
CD capacity = 650 MB
# chars/word ' 5
∴ # words/CD ' 130 000 000
typing speed = 40 words/minute
∴ time ' 3 250 000 minutes
' 55 000 hours
' 6 years
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 23
The Hello World program is a single statement
1 >>> print "hello world"
2 hello world
3 >>>
• the print line is called a statement
• a statement is the smallest stand-alone unit in a program
• the "hello world" is a string
• a string represents zero or more characters of text
• the double quotes (") delimit the text i.e. mark the
boundaries, so the interpreter doesn’t expect it to be Python
syntax
• the interpreter then displays another prompt (>>>) which
indicates it is waiting for the next statement
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 24
Python is a great calculator
1 >>> 100 + 3*12 - 6.0
2 130
3 >>>
• * is used for multiplication (like almost all languages)
• it follows correct precedence (3*12 happens first)
4 >>> _/10
5 13
6 >>>
• the underscore variable _ stores the last result
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 25
Displaying results is not printing
1 >>> print "hello world"
2 hello world
3 >>> "hello world"
4 'hello world'
5 >>>
• the 1st statement prints the string "hello world"
• the 2nd statement displays the string "hello world"
• displaying only works in the interactive interpreter
• it uses Python syntax, so you still see quotes around the string
• it is only used for debugging purposes
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 26
Python operators do what you expect
1 >>> "hello" + "world"
2 'helloworld'
3 >>> "hello"*3
4 'hellohellohello'
5 >>>
• + adds two strings together (called concatenation)
• notice we don’t get a space unless we explicitly add it
• * works with a string and an integer
• "hello"*3 is just like adding three "hello"’s together
• notice we are displaying again because there is no print
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 27
No Storing is boring
• So far, we have only used literal values in our examples
• i.e. 5 is the integer value 5, "hello" is the string for hello
• programs that only use literal values don’t get very interesting
• we want to store values in memory from:
• the intermediate results of calculations
• files, user input, other programs, and elsewhere
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 28
We access memory using variable names
• Modern computers have lots of memory. Lots and lots:
E.g. 4GiB is 4 294 967 296 bytes.
• Each byte is accessible by number (we say addressable)
• We could store our values at practically any of these addresses:
e.g. put the result of "hello"*3 in memory at 0x54fd0.
• Remembering addresses for one (or many) values is tough!
• Humans are much better at remembering names than numbers
because we can choose meaningful names for each location.
• Let the computer do the work of translating for us from
variable names to addresses
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 29
Variables are like mailboxes
• A variable is a memory location for storing values
• A variable name is the name we use to refer to the variable
• Variables can be thought of as mailboxes with names on
them:
• you can put values into the mailbox
• you can look at the value in the mailbox
• Arrgh, except!:
• putting a new value in the mailbox overwrites the old one
• reading the value does not remove it from the mailbox
• peeking to check there is a value isn’t possible without reading
• Maybe that analogy is more trouble than it’s worth!
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 30
Python variables are created by assignment
• Variable names must contain only A-Z, a-z, 0-9 or _
• They must also start with A-Z, a-z or _
• Variables are created (and modified) by assignment
1 >>> x = "hi"
2 >>>
• here, the variable x is assigned the value "hi"
• we can easily display the contents of a variable:
3 >>> x
4 'hi'
5 >>>
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 31
Variables can be used just like literals
1 >>> x + x
2 'hihi'
3 >>> x*5
4 'hihihihihi'
• they can even be used to create new variables by assignment
5 >>> y = x
6 >>> y
7 'hi'
8 >>>
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 32
Each variables stores a separate copy of its value
1 >>> x = 'hello'
2 >>> y = x
3 >>> x = 'goodbye'
• What do x and y contain now?
4 >>> x
5 'goodbye'
6 >>> y
7 'hello'
8 >>>
• So, changing x does not change y because y is holding a
separate copy of the string
• Later we will see cases where this doesn’t work
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 33
You should now be able to:
• Install and run the Python interpreter under Windows
• Enter simple statements into the interactive Python Shell
• Understand why variables exist and how they work
• Create variables in Python
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014
Admin Python Interpeter BOTE Experiment Variables Summary 34
Sources
• Material from James Curran and Tara Murphy
• http://www.python.org/~guido
Bob Kummerfeld Python Intro Lecture 3, 5th March, 2014