Python Chapter 3 Reading strings and printing. © Samuel Marateck To write and run a program: 1.
Download
Report
Transcript Python Chapter 3 Reading strings and printing. © Samuel Marateck To write and run a program: 1.
Python
Chapter 3
Reading strings and printing.
© Samuel Marateck
1
To write and run a program:
1. On the shell’s file menu click
New Window
2. Type your program.
3. Use a # to begin a comment.
4. Use the Edit window to edit your program.
5. Save your program to the proper directory
To save, choose save on the file menu.
6. To run your program, click run module on
the run menu,
2
When you save your program, type the
program’s name a period and then py. For
Instance prog1.py . The program you write
is called the source program. When you
run your program, the compiler produces
what is called the object program. If you
look at the directory, in our case you will see
prog1.pyc; you will not be able to read this.
3
The entity that runs the object program is
called the Python virtual machine.
4
The input() statement
Input to a program is read using the
input() statement and is read as a string,
for instance,
name = input(‘Type your input’)
The variable name is a string variable.
The string ‘Type your input’ is printed in
the shell when the program is run.
5
Our program is:
#our first program
name = input(‘Type your input ’)
Print(‘Our input is ‘, name)
6
When your run the program, the following
appears in the shell:
>>>
Type your input
You then type anything after the word input.
Type your input NYU rocks
Here we typed NYU rocks.
7
The computer responds:
Our input is NYU rocks
The entire session is shown on the next
slide.
8
>>>
Type your input NYU rocks
our input is NYU rocks
>>>
9
To be able to type the input on another line,
place \n at the end of the string:
name = input(‘Type your input \n’)
10
After you save the program and run it,
and type asd as input, the following appears on the
shell:
>>>
Type your input
asd
our input is asd
>>>
11
The characters \n does not appear in the
shell.; \n is called an escape sequence and
n is called a control character and forces a
a skip to the next line (carriage return).
12
To see the functions that can be used with
a string, type dir(‘’) in the shell. The result is:
13
• >>> dir('')
• ['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__',
'__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'_formatter_field_name_split', '_formatter_parser', 'capitalize',
'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format',
'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower',
'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip',
'swapcase', 'title', 'translate', 'upper', 'zfill']
• >>>
14
To see the effect of one of these functions,
type a string, then a period, then the function
ending with (). So
>>>’asd’.upper()
‘ASD’
upper() converts every letter in the string to
uppercase. It leaves all other characters alone.
The following are examples of some of
these functions.
15
lower() Changes uppercase to
Lowercase and leaves all other variables
alone.
Example:
>>>’THE’
‘the’
16
isalpha() Returns True if the string
consists of only letters else False.
Example:
>>> ‘the’.isalpha()
TRUE
>>>’the12’. isalpha()
False
17
capitalize(). Capitalizes a string.
Example:
>>>’asd’. capitalize()
‘Asd’
18
strip() Removes the leading and trailing
blanks.
Example:
>>>‘ asd ‘.strip()
‘asd’
19
To convert numeric input to an integer, use
the int() function.
value = input(‘type your integer’)
number = int(value)
print(number + 3)
20
Normally, two print statements print their
output on two separate lines, Thus
print(‘abc’)
print(‘def’)
Produces
>>>
abc
def
>>>
21
To have the output on one line, end the first
print with end= ‘’
print(’abc’, end = ‘’)
print(‘def’)
produces
>>>abcdef
22
We’ll investigate the following program:
length = 87
width = 5
area = length*width
print(‘area =‘, area)
23
If we wrote prin(‘def’) the computer would
respond with
• Traceback (most recent call last):
• File "<pyshell#0>", line 1, in <module>
•
prin('def')
• NameError: name 'prin' is not defined
24
This is another example of a syntax error .
It is also called a compilation error since it
occurs during compilation time.
Such an error must be corrected before the
program will run.
25
The following table describes how the
variables are defined.
undef means undefined.
26
statement
length width
length = 87
87
undef undef
width = 5
87
5
undef
area=length*width 87
5
435
print
5
435
87
area
27
Now let’s analyze:
length = 87
width = 5
area = length*width
print(‘area =‘, area)
width = 3
print(‘area =‘, area)
28
We see that the width has been redefined.
What happens in the computer’s memory
is that the bits in the location width are
reconfigured.
29
statement
length width
area
length = 87
87
undef
undef
width = 5
87
5
undef
area=length*width 87
5
435
print
87
5
435
width = 3
87
3
435
print
87
3
435
30
We see that since the area does not appear
on the left side of an assignment statement,
it is not redefined and the original value
of the area will be printed. This is called a
logical error because it is an error in
logic and will produce the wrong results.
You must detect a logical error yourself.
31
In the corrected program we recalculate
the area as is shown in the next slide.
32
length = 87
width = 5
area = length*width
print(‘area =‘, area)
width = 3
area = length*width
print(‘area =‘, area)
33