3-1. identifier. Why is variable name and variable type declaration not required in Python?
Python variables are defined internally.
3-2. identifier. Why does not the function type need to be declared in Python?
The definition is an internal definition type.
3-3. identifier. Why should we avoid double underscores (_) at the beginning and end of the variable name?
Double underscores have special meanings and are internal variables.
3-4. Statements. Can multiple statements be written in one line in Python?
Yes. Use semicolons to separate them.
3-5. Statements. In Python, can a statement be divided into multiple lines for writing?
Yes. Use a backslash to continue the row.
3-6. Variable assignment
(A) What values will be assigned to the values x, y, z = 1, 2, and 3 in x, y, and z?
X = 1
Y = 2
Z = 3
(B) After z, x, y = y, z, x is executed, what values are contained in x, y, and z?
X = 3
Y = 1
Z = 2
3-7. identifier. Which of the following are valid Python identifiers? If not, describe the reason! Under a valid tag
What are keywords in the identifier?
The python identifier starts with an underscore (_) or a letter, followed by an underscore (_).
The following problems involve the makeTextFile. py and readTextFile. py scripts.
3-8. Python code. Copy the script to your file system and modify it. You can add and modify comments.
The prompt '>' is too monotonous.) modify the code to make it look more comfortable.
#! /Usr/bin/env python
Import OS
Ls = OS. linesep
# Enter file
While True:
F1 = raw_input ('input filename :')
If OS. path. exists (f1 ):
Print "error the file name % s exists." % f1
Else:
Break
# Write to file
All = []
Print "input contanct '. 'end"
While True:
Cont = raw_input ('input context :')
If cont! = '.':
All. append (cont)
Else:
Break
Print all
File1 = open (f1, 'w ')
For nn in all:
File1.writelines (nn)
File1.close ()
3-9. Port. If Python is installed on different types of computer systems, check that,
Whether the OS. linesep values are different. Write down the operating system type and linesep value.
3-10. Exception. Replace readTextFile. py with the exception handling method similar to readTextFile. py.
The call to OS. path. exists () in makeTextFile. py. In turn, replace OS. path. exists ()
The exception handling method in readTextFile. py.
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
Import OS
F = raw_input ('input file name :')
# Try:
# File1 = open (f, 'R ')
# Handle T IOError, e:
# Print "file open error:", e
# Else:
# For nn in file1.readlines ():
# Print nn
# File1.close ()
If OS. path. exists (f ):
File1 = open (f, 'R ')
For nn in file1:
Print nn
File1.close ()
3-11.
String formatting does not suppress the NEWLINE characters generated by the print statement in readTextFile. py. Modify your
Code to delete the white space at the end of each line before a row is displayed. In this way, you can remove the comma at the end of the print statement.
Tip: Use the strip () method of the string object
The question should be changed from print nn to print nn. strip ()
3-12. Merge source files. Combine the two programs into one and give it a name you like, for example
ReadNwriteTextFiles. py. You can choose whether to create or display a text file.
# Ch = raw_input ('select w: write r: read :')
# If ch = 'W ':
# Elif ch = 'r ':
# Else:
# Print 'error'
3-13. Add new features. Add a new feature to readNwriteTextFiles. py, which is transformed from your previous question.
Yes: allows you to edit an existing text file. You can use either of the following methods:
Is to edit all texts at a time. Note that it is difficult to edit all the text at a time. You may need to use the GUI
Toolkit or a module based on screen text editing, such as the curses module. To allow users to save their changes
File) or cancel the modification without changing the original file), and ensure the security of the original file regardless of whether the program is
).
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
Def write ():
Filename = raw_input ("input file name :")
FileOne = open (filename, 'w ')
AllContext = []
While True:
Context = raw_input ('input content> ')
If context = ".":
Break
Else:
AllContext. append (context)
For con in allContext:
FileOne. writelines (con)
FileOne. close
Def read ():
Filename = raw_input ("input file name :")
While True:
Try:
FileTwo = open (filename, 'R ')
Break
Handle t IOError, e:
Print "the file name not exists", e
Continue
For cont in fileTwo:
Print cont
FileTwo. close ()
Def quit ():
Print ""
Def add ():
Pass
Def operaterFiles ():
ChoseCheck = {'W': write, 'r': read, 'A': add, 'q': quit}
I = 1
While I <= 5:
Op = raw_input ('Operation to be input :')
ChoseCheck [op] ()
I + = 1
If _ name __= = "_ main __":
OperaterFiles ()
This article from "My Heart" blog, please be sure to keep this source http://leeforget.blog.51cto.com/6950397/1286484