📄 node4.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0061)http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html -->
<!--Converted with LaTeX2HTML 99.2beta6 (1.42)original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Hello, World</TITLE>
<META content="Hello, World" name=description>
<META content=easytut name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2614.3500" name=GENERATOR>
<META content=text/css http-equiv=Content-Style-Type><LINK
href="node4_files/easytut.css" rel=STYLESHEET><LINK href="node5.html"
rel=next><LINK href="node3.html" rel=previous><LINK href="easytut.html"
rel=up><LINK href="node5.html" rel=next></HEAD>
<BODY><!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html"
name=tex2html181><IMG align=bottom alt=next border=0 height=24
src="node4_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html177><IMG align=bottom alt=up border=0 height=24
src="node4_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node3.html"
name=tex2html171><IMG align=bottom alt=previous border=0 height=24
src="node4_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html179><IMG align=bottom alt=contents border=0 height=24
src="node4_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html"
name=tex2html182>Who Goes There?</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html178>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node3.html"
name=tex2html172>Intro</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html180>Contents</A></B> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A
name=CHILD_LINKS><STRONG>Subsections</STRONG></A>
<UL>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html#SECTION00410000000000000000"
name=tex2html183>What you should know</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html#SECTION00420000000000000000"
name=tex2html184>Printing</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html#SECTION00430000000000000000"
name=tex2html185>Expressions</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html#SECTION00440000000000000000"
name=tex2html186>Talking to humans (and other intelligent beings)</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html#SECTION00450000000000000000"
name=tex2html187>Examples</A>
<LI><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node4.html#SECTION00460000000000000000"
name=tex2html188>Exercises</A> </LI></UL><!--End of Table of Child-Links-->
<HR>
<H1><A name=SECTION00400000000000000000>Hello, World</A> </H1>
<H1><A name=SECTION00410000000000000000>What you should know</A> </H1>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.
<P>
<H1><A name=SECTION00420000000000000000>Printing</A> </H1>Programming tutorials
since the beginning of time have started with a little program called Hello,
World! So here it is:
<P><PRE>print "Hello, World!"
</PRE>
<P>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 ``<I>python hello.py</I>''
<P>Otherwise go into IDLE, create a new window, and create the program as in
section <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node3.html#create">1.4</A>.
<P>When this program is run here's what it prints:
<P><PRE>Hello, World!
</PRE>
<P>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.
<P>Now here is a more complicated program:
<P><PRE>print "Jack and Jill went up a hill"
print "to fetch a pail of water;"
print "Jack fell down, and broke his crown,"
print "and Jill came tumbling after."
</PRE>
<P>When you run this program it prints out:
<P><PRE>Jack and Jill went up a hill
to fetch a pail of water;
Jack fell down, and broke his crown,
and Jill came tumbling after.
</PRE>
<P>When the computer runs this program it first sees the line: <PRE>print "Jack and Jill went up a hill"
</PRE>so the computer prints: <PRE>Jack and Jill went up a hill
</PRE>
<P>Then the computer goes down to the next line and sees: <PRE>print "to fetch a pail of water;"
</PRE>
<P>So the computer prints to the screen: <PRE>to fetch a pail of water;
</PRE>
<P>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.
<P>
<H1><A name=SECTION00430000000000000000>Expressions</A> </H1>Here is another
program: <PRE>print "2 + 2 is", 2+2
print "3 * 4 is", 3 * 4
print 100 - 1, " = 100 - 1"
print "(33 + 2) / 5 + 11.5 = ",(33 + 2) / 5 + 11.5
</PRE>
<P>And here is the output when the program is run:
<P><PRE>2 + 2 is 4
3 * 4 is 12
99 = 100 - 1
(33 + 2) / 5 + 11.5 = 18.5
</PRE>
<P>As you can see Python can turn your thousand dollar computer into a 5 dollar
calculator.
<P>Python has six basic operations:
<P>
<TABLE border=1 cellPadding=3>
<TBODY>
<TR>
<TD align=left>Operation</TD>
<TD align=middle>Symbol</TD>
<TD align=left>Example</TD></TR>
<TR>
<TD align=left>Exponentiation</TD>
<TD align=middle><CODE>**</CODE></TD>
<TD align=left><CODE>5 ** 2 == 25</CODE></TD></TR>
<TR>
<TD align=left>Multiplication</TD>
<TD align=middle><CODE>*</CODE></TD>
<TD align=left><CODE>2 * 3 == 6</CODE></TD></TR>
<TR>
<TD align=left>Division</TD>
<TD align=middle><CODE>/</CODE></TD>
<TD align=left><CODE>14 / 3 == 4</CODE></TD></TR>
<TR>
<TD align=left>Remainder</TD>
<TD align=middle><CODE>%</CODE></TD>
<TD align=left><CODE>14 % 3 == 2</CODE></TD></TR>
<TR>
<TD align=left>Addition</TD>
<TD align=middle><CODE>+</CODE></TD>
<TD align=left><CODE>1 + 2 == 3</CODE></TD></TR>
<TR>
<TD align=left>Subtraction</TD>
<TD align=middle><CODE>-</CODE></TD>
<TD align=left><CODE>4 - 3 == 1</CODE></TD></TR></TBODY></TABLE>
<P>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: <PRE>print "14 / 3 = ",14 / 3
print "14 % 3 = ",14 % 3
print
print "14.0 / 3.0 =",14.0 / 3.0
print "14.0 % 3.0 =",14 % 3.0
print
print "14.0 / 3 =",14.0 / 3
print "14.0 % 3 =",14.0 % 3
print
print "14 / 3.0 =",14 / 3.0
print "14 % 3.0 =",14 % 3.0
print
</PRE>With the output: <PRE>14 / 3 = 4
14 % 3 = 2
14.0 / 3.0 = 4.66666666667
14.0 % 3.0 = 2.0
14.0 / 3 = 4.66666666667
14.0 % 3 = 2.0
14 / 3.0 = 4.66666666667
14 % 3.0 = 2.0
</PRE>Notice how Python gives different answers for some problems depending on
whether or not there decimal values are used.
<P>The order of operations is the same as in math:
<OL>
<LI>parentheses <CODE>()</CODE>
<LI>exponents <CODE>**</CODE>
<LI>multiplication <CODE>*</CODE>, division <CODE>\</CODE>, and remainder
<CODE>%</CODE>
<LI>addition <CODE>+</CODE> and subtraction <CODE>-</CODE> </LI></OL>
<P>
<H1><A name=SECTION00440000000000000000>Talking to humans (and other intelligent
beings)</A> </H1>
<P>Often in programming you are doing something complicated and may not in the
future remember what you did. When this happens the program should probably be
commented. A comment is a note to you and other programmers explaining what is
happening. For example: <PRE>#Not quite PI, but an incredible simulation
print 22.0/7.0
</PRE>Notice that the comment starts with a <CODE>#</CODE>. Comments are used to
communicate with others who read the program and your future self to make clear
what is complicated.
<P>
<H1><A name=SECTION00450000000000000000>Examples</A> </H1>Each chapter
(eventually) will contain examples of the programming features introduced in the
chapter. You should at least look over them see if you understand them. If you
don't, you may want to type them in and see what happens. Mess around them,
change them and see what happens.
<P>Denmark.py <PRE>print "Something's rotten in the state of Denmark."
print " -- Shakespeare"
</PRE>
<P>Output: <PRE>Something's rotten in the state of Denmark.
-- Shakespeare
</PRE>
<P>School.py <PRE>#This is not quite true outside of USA
# and is based on my dim memories of my younger years
print "Firstish Grade"
print "1+1 =",1+1
print "2+4 =",2+4
print "5-2 =",5-2
print
print "Thirdish Grade"
print "243-23 =",243-23
print "12*4 =",12*4
print "12/3 =",12/3
print "13/3 =",13/3," R ",13%3
print
print "Junior High"
print "123.56-62.12 =",123.56-62.12
print "(4+3)*2 =",(4+3)*2
print "4+3*2 =",4+3*2
print "3**2 =",3**2
print
</PRE>
<P>Output: <PRE>Firstish Grade
1+1 = 2
2+4 = 6
5-2 = 3
Thirdish Grade
243-23 = 220
12*4 = 48
12/3 = 4
13/3 = 4 R 1
Junior High
123.56-62.12 = 61.44
(4+3)*2 = 14
4+3*2 = 10
3**2 = 9
</PRE>
<P>
<H1><A name=SECTION00460000000000000000>Exercises</A> </H1>
<P>Write a program that prints your full name and your birthday as separate
strings.
<P>Write a program that shows the use of all 6 math functions.
<P>
<HR>
<!--Navigation Panel--><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html"
name=tex2html181><IMG align=bottom alt=next border=0 height=24
src="node4_files/next.png" width=37></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html177><IMG align=bottom alt=up border=0 height=24
src="node4_files/up.png" width=26></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node3.html"
name=tex2html171><IMG align=bottom alt=previous border=0 height=24
src="node4_files/prev.png" width=63></A> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html179><IMG align=bottom alt=contents border=0 height=24
src="node4_files/contents.png" width=65></A> <BR><B>Next:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node5.html"
name=tex2html182>Who Goes There?</A> <B>Up:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/easytut.html"
name=tex2html178>Non-Programmers Tutorial For Python</A> <B>Previous:</B> <A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node3.html"
name=tex2html172>Intro</A> <B><A
href="http://www.honors.montana.edu/~jjc/easytut/easytut/node2.html"
name=tex2html180>Contents</A></B> <!--End of Navigation Panel-->
<ADDRESS>Josh Cogliati <A
href="mailto:jjc@honors.montana.edu">jjc@honors.montana.edu</A>
</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -