文件名称:
Non-Programmer's_Tutorial_for_Python.pdf
开发工具:
文件大小: 202kb
下载次数: 0
上传时间: 2019-08-18
详细说明:Non-Programmer's_Tutorial_for_Python.pdf Non-Programmer's_Tutorial_for_Python.pdfabstract
Non-Programmers Tutorial For Python is a tutorial designed to be a introduction to the Python programming language
This guide is for someone with no programming experience
If you have programmed in other languages I recommend using The Python Tutorial written by Guido van Rossum
ThisdocumentisavailableasIaTex,Html,Pdf,andPostscriptgOtohttp:/www.honors.montana.edu/jjc/easytut/
to see all these forms
If you have any questions or comments please contact me at jjc(ainame com I welcome questions and comments
about this tutorial. I will try to answer any questions you have as best as i can
Thanks go to James A. Brown for writing most of the Windows install info. Thanks also to Elizabeth Cogliati for
complaining enough: about the original tutorial, that is almost unusable for a non-programmer) for proofreading and
for many ideas and comments on it. Thanks to Joe Oppegaard for writing all the exercises. Thanks to everyone I have
missed
Dedicated to Elizabeth Cogliati
CONTENTS
1 ntro
1.1 First things first
1. 2 Installing python
1.3 Interactive Mode
1.4 Creating and Running Programs
2
1.5 Using Python from the command line
2 Hello world
2. 1 What you should know
2
2.3 Expressions
4
2.4 Talking to humans(and other intelligent beings)
2.5 Examp
2.6 Exercises
7
3 Who Goes There?
3. 1 Input and Variables
3.2 Examp
xercises
12
4 Count to 10
4. 1 While loops
13
4.2 Examples
s Decisions
17
5.1 If statement
17
5.2 Examples
18
5.3 Exercises
21
6 Debugging
6. 1 What is debugging?
23
6. 2 What should the program do
6.3 What does the program do?
24
6.4 How do i fix the program?
27
7 Defining functions
29
7. 1 Creating fi
29
7.2 Variables in functions
7.3 Function walkthrough
32
7.4 Examples
7.5 Exercises
36
8 L
37
8.1 Variables with more than one value
8.2 More features of lists
37
8.3上 xamples
42
8.4 Exercises
43
9 For l
45
10 Boolean Expressions
10. 1 Examples
10.2 Exercise
11 Dictionaries
12 Using modules
59
12.1 Exercises
13 More on Lists
14 Revenge of the Strings
14.1 Examples
71
15 File io
15.1 Exercises
77
16 Dealing with the imperfect(or how to handle errors)
79
16.1 Exercises
1 The End
8 FAQ
Index
85
CHAPTER
ONE
Intro
1.1 First things first
So, you've never programmed before. As we go through this tutorial I will attempt to teach you how to program
There really is only onc way to learn to program. You must read code and write code. I 'm going to show you lots of
code. You should type in code that I show you to see what happens. Play around with it and make changes. The worst
that can happen is that it wont work. When I type in code it will be formatted like this
#+Python is easy to learn
print Hello
orld! n
Thats so it is easy to distinguish from the other text. To make it confusing I will also print what the computer outputs
in that same font
Now, on to more important things. In order to program in Python you need the Python software. If you dont
alreadyhavethePythonsoftwaregotohttp:/www.python.org/download/andgettheproperversionforyourplatform
Download it, read the instructions and get it installed
1.2 Installing Python
Firstyouneedtodownloadtheappropriatefileforyourcomputerfromhttp:/www.python.org/download.Gotothe2.0
link(or newer) and then get the windows installer if you use Windows or the rpm or source if you use Unix
The Windows installer will download to file. The file can then be run by double clicking on the icon that is downloaded
The installation will then proceed
If you get the Unix source make sure you compile in the tk extension if you want to use IDLE
1.3 nteractive mode
Go into idlE (also called the Python GUD). You should see a window that has some text like this
Python2.0(#4,Dec122c00,19:19:57)
IGCC 2.95.2 20000220(Debian GNU/Linux)] on linux 2
Typc "copyright,crcaitsor
consc for morc information
IDLE 0.6
s Fl for hel-
The >>>is Python way of telling you that you are in interactive mode. In interactive mode what you type is immedi-
ately run. Try typing 1+1 in. Python will respond with 2. Interactive mode allows you to test out and see what Python
If you ever feel you need to play with new Python statements go into interactive mode and try them out
.4 Creating and Running Programs
Go into IDLE if you are not already Go to File then New Window. In this window type the following
print Hello, Wor Id!
First save the program. Go to File then Save. Save it as ' hello. py,.(If you want you can save it to some other
directory than the default. Now that it is saved it can be run
Next run the program by going to Run then Run Module (or if you have a older version of idle use Edit then
Run script). This will output Hello, World! on the *Python Shell* window
Confusedstill?TrythistutorialforIdLeathttp:/hkn.eecs.berkeleyedu/dyoo/python/idleintro/index.html
1.5 Using python from the command line
If you dont want to use Python from the command line, you dont have too, just use IDLE. To get into interactive
mode just type python with out any arguments. To run a program create it with a text editor(Emacs has a good
python mode)and then run it with python program name
Chapter 1. Intro
CHAPTER
TWO
Hello World
2. 1 What you should know
You should know how to edit programs in a text editor or IDle, save them to disk(floppy or hard) and run them once
they have been saved
2.2 Printing
Programming tutorials since the beginning of time have started with a little program called Hello, World! So here it is
print Hello, World!
If you are using the command line to run programs then type it in with a text editor, save it as"hello. py and run it with
python hello. py
Otherwise go into IDLE, create a new window, and create the program as in section 1. 4
When this program is run here's what it prints
Hello
Now I'm not going to tell you this every time, but when i show you a program i recommend that you type it in and run
it. I learn better when I type it in and you probably do too
Now here is a more complicated program
print Jack and Jill went up a hill
print to fetch a pail of wateri
print " Jack fell down, ard broke his crown,
print and Jill came tumbling after
When you run this program it prints out
Jack and Jill went up a hill
to fetch a pail of water
Jack fell down, and broke his crown
and Jill came tumbling aft
When the computer runs this program it first sees the line
print Jack and Jill went up a hill
so the computer prints
Jack and Jill went up a hill
Then the computer goes down to the next line and sees
print to fetch a pail of wateri
So the computer prints to the screen
to fetch a pail of water
The computer keeps looking at each line, follows the command and then goes on to the next line. The computer keeps
running commands until it reaches the end of the program
2.3 Expressions
Here is another program
print 2 +2 is",2+2
rint 3
print 100-1
100-1
print"(33+2)/5+11.5=",(33+2)/5+11.5
And here is the output when the program is run
2 is 4
3*4is12
99
100-1
33+2)/5+11.5=18.5
As you can see Python can turn your thousand dollar computer into a 5 dollar calculator.
Python has six basic operations for numbers
Operation
Symbol Example
Exponentiation
5大2==25
Multiplication
DiⅤ nSIon
14/3=4
Remainder
14号3==2
Addition
1+2==3
Subtraction
3
1
Notice that division follows the rule, if there are no decimals to start with, there will be no decimals to end with. ( Note
This will be changing in Python 2. 3)The following program shows this
Chapter 2. Hello, World
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.