Section 3.8. Exercises


3.8. Exercises

3-1.

Identifiers. Why are variable name and type declarations not used in Python?

3-2.

Identifiers. Why are function type declarations not used in Python?

3-3.

Identifiers. Why should we avoid beginning and ending variable names with double underscores?

3-4.

Statements. Can multiple Python statements be written on a single line?

3-5.

Statements. Can a single Python statement be written over multiple lines?

3-6.

Variable Assignment.

  1. Given the assignment x, y, z = 1, 2, 3, what do x, y, and z contain?

  2. What do x, y, and z contain after executing: z, x, y = y, z, x?

3-7.

Identifiers. Which of the following are valid Python identifiers? If not, why not? Of the invalid ones, which are keywords?

int32

40XL

$aving$

printf

print

_print

this

self

__name__

0x40L

bool

true

big-daddy

2hot2touch

type

thisIsn'tAVar

thisIsAVar

R_U_Ready

Int

true

if

do

counter-1

access

_


The remaining problems deal with the makeTextFile.py and readTextFile.py programs.

3-8.

Python Code. Copy the scripts to your file system and customize (tweak, improve) them. Modifications can include adding your own comments, changing the prompts ('>' is pretty boring), etc. Get comfortable looking at and editing Python code.

3-9.

Porting. If you have Python installed on different types of computers, check to see if there are any differences in the os.linesep characters. Write down the type/OS and what linesep is.

3-10.

Exceptions. Replace the call to os.path.exists() in makeTextFile.py with an exception handler as seen in readTextFile.py. On the flip side, replace the exception handler in readTextFile.py with a call to os.path.exists().

3-11.

String Formatting. Rather than suppressing the NEWLINE character generated by the print statement in readTextFile.py, change your code so that you strip each line of its whitespace before displaying it. In this case, you can remove the trailing comma from the print statement. Hint: Use the string strip() method.

3-12.

Merging Source Files. Combine both programs into onecall it anything you like, perhaps readNwriteTextFiles.py. Let the user choose whether to create or display a text file.

3-13.

*Adding Features. Take your readNwriteTextFiles.py solution from the previous problem and add a major feature to it: Allow the user to edit an existing text file. You can do this any way you wish, whether you let the user edit line by line or the entire document at once. Note that the latter is much more difficult as you may need help from a GUI toolkit or a screen-based text editing module such as curses. Give users the option to apply the changes (saving the file) or discard them (leaving the original file intact), and also ensure the original file is preserved in case the program exits abnormally during operation.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net