forked from ninas/umonya_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_input.html
196 lines (153 loc) · 8.05 KB
/
basic_input.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Introductory Programming in Python: Basic Input</title>
<link rel='stylesheet' type='text/css' href='style.css' />
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script src="animation.js" type="text/javascript">
</script>
</head>
<body onload="animate_loop()">
<div class="page">
<h1>Introductory Programming in Python: Lesson 4<br />
Basic Input</h1>
<div class="centered">
[<a href="basic_output.html">Prev: Basic Output</a>] [<a href="index.html">Course Outline</a>] [<a href="program_state.html">Next: Program State and Basic Variables</a>]
</div>
<h2>Literal Values</h2>
<p>So far our programs have been pretty uninteresting. They
consistently produce the same results because they consistently act on
the same data, or input, which we have specified by <strong>hard
coding</strong> values for our inputs in the form of
<strong>literals</strong>. A 'constant' or 'literal' is sort of the opposite to a
variable, in that its value does not change over the course of program
execution, and more importantly, its value is specified
<em>literally</em>, within the program code.</p>
<p>As mentioned before, Python understands a fairly limited set of key
words and symbols as atomic statements or operators. Previously, we
said that python treats unrecognised words (used is a very loose sense)
as variable names, but this is not strictly true. Python treats
unrecognised words as expressions, and attempts to determine their
value. Now, the word can specify a value directly, in which case it
called a literal value, or simply 'literal' for short. Only if the word
isn't a literal, does Python treat is as a literal.</p>
<pre class='listing'>
#An example differentiating literals and variables
a = 3 #a is a variable name, 3 is an integer literal
b = 'b' #b is a variable name, 'b' is a string literal
c = True #c is a variable name, True is a boolean literal
if 3*'b' == a*b: #3 and 'b' are literals, a and b are variables
print c #is c literal or variable?
else:
print "False" #is "False" a literal or a variable?
#Are False and "False" the same?
</pre>
<h2>The raw_input() Function</h2>
<p>It's not very helpful to us if we must change our programs every
time we want to change the data they work with. So instead we want to
be able to tell our program to get input from somewhere. The simplest
place to get input from is the keyboard, in the form of text entered by
the user. Python provides a <strong>function</strong> to do just this,
called <a class="doclink"
href="http://docs.python.org/lib/built-in-funcs.html">raw_input</a>:</p>
<pre class='listing'>
Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) [MSC v.1310 32 bit (Intel)] on win32
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> name = raw_input("What is your name? ")
What is your name? James
>>> print name
James
>>>
</pre>
<p>We've seen 'raw_input' before in the basic concepts section, and
there really isn't much to it. Just a few things to note really.
Firstly, raw_input is a function. A function is a defined collection of
statements that produce or <strong>return</strong> a value when
executed. You could think of them as a way of turning a small set of
statements into an expression. We will learn how to define our own
functions later on in the course, but Python provides quite a few basic
built in ones which we are going to be using before that, so let's look
briefly at how they work.</p>
<ul>
<li><code>raw_input</code> is the name of the function. Like
variables, functions have names to identify them. Like a variable,
a function name is a label that points to the collection of
statements to be executed.</li>
<li>To execute the statements in a function, better known as
<strong>calling a function</strong> we write the function's name
followed by round brackets. <code>raw_input()</code></li>
<li>Using the name just by itself treats the function like a
variable. <!--Functions, when not called, are also expressions and
have a value. Their value in this case just happens to be the
function itself.--></li>
<li><code>"What is your name? "</code> is a parameter to the
function. Functions are like mini programs. They always do the same
thing, but they can do the same thing to different data. We put the
data we want a function to operate on in between the brackets when
we call it. Expressions and variables <strong>passed</strong> to
functions in this way are temporarily known as
<strong>parameters</strong>. Multiple parameters can be passed to a
function by separating them with commas, and in general any valid
expression is a valid parameter; e.g.<br /> <code>minimum(6, 2,
3+4, 4**2, 8.3, 3*1)</code><br /> has six parameters, some of which
are not just simple expressions like plain numbers.</li>
<li>Despite the fact that any valid expression is a valid
parameter, functions are usually quite picky about the parameters
they can work with. For example, we couldn't find them minimum of a
collection of strings. That only really works with integers and
floats. Functions usually specify the <em>number</em> of parameters
they accept. Often functions may accept less parameters than they
specifically ask for, by substituting default values for the
parameters not provided. Such is the case with 'raw_input',
speaking of which, we digress.</li>
</ul>
<p>Back to 'raw_input'! 'raw_input' captures a single line of text from
the keyboard, as entered by the user. It returns a string containing
the captured text, leaving out the "\n" produced when the user hits the
Enter key. In addition, it takes one optional parameter, which is
displayed as a prompt prior to accepting input from keyboard. If this
parameter is left out, the default prompt is an empty string, so
nothing is printed.</p>
<h2>Exercises</h2>
<ol>
<li>If there is a function named <em>my_function</em>, how do I call
it?</li>
<li>Are functions expressions or statements? What about when they
are called?</li>
<li>Start the python interactive interpreter:
<ol>
<li>Assign what the user types to a variable called
's'.</li>
<li>Print the value of the variable s.</li>
<li>Print 's'.</li>
<li>Use raw_input() to prompt the user for a number, get that number, and assign it to a variable called 'n'.</li>
<li>Print double the value of the variable 'n'.</li>
</ol>
Exit the python interactive interpreter.
</li>
<li>Write a program that asks the user user for their name and
stores it in a variable. Then output "Hello", followed by the
user's name</li>
<li>Write a program that asks the user to enter two numbers, and
prints the sum of those two numbers.</li>
<li>Write a program that asks the user to enter two numbers, and
prints the difference of those two numbers.</li>
<li>Write a program that asks the user to enter two numbers, and
prints the product of those two numbers. Are you sensing a pattern
here?</li>
<li>Write a program that asks the user for some text, and a number.
The program prints out the text a number of times equal to the
number entered, without line breaks or spaces in between each
repetition.</li>
</ol>
<div class="centered">
[<a href="basic_output.html">Prev: Basic Output</a>] [<a href="index.html">Course Outline</a>] [<a href="program_state.html">Next: Program State and Basic Variables</a>]
</div>
</div>
<div class="pagefooter">
Copyright © James Dominy 2007-2008; Released under the <a href="http://www.gnu.org/copyleft/fdl.html">GNU Free Documentation License</a><br />
<a href="intropython.tar.gz">Download the tarball</a>
</div>
</body>
</html>