Python core programming-Chapter 3-exercises, chapter 3 of python

Source: Internet
Author: User

Python core programming-Chapter 3-exercises, chapter 3 of python

1. This is a feature of python. python first creates an object. When assigning values to a variable, you do not need to define the name and type of the variable. Actually, it uses the variable to reference the object. The variable type is automatically declared when a value is assigned to the variable.

2. Similar reasons variables do not need to declare types

3. python uses underlines as the prefix and suffix of variables to specify special variables, which has special significance for the interpreter and is also a special symbol used by built-in identifiers. Therefore, it generally avoids using underscores as the beginning and end of a variable.

4. A python line can write multiple statements, separated. However, this is not recommended for a good programming style.

5. python can divide a statement into multiple lines for writing, and the end of the row is identified by a backslash. It is recommended that a single line of python statement should not exceed 80 characters, so if a statement is too long, it can be divided into multiple lines of books to write. In other cases, keep a physical row and write a logical row.

6. Variable assignment

(A) assignment statement x, y, z = 1, 2, 3 assign 1 to x, 2 to y, and 3 to z.

(B) after execution in z, x, y = y, z, x, the value of x is 3, the value of y is 1, and the value of z is 2.

7. identifier

Int32; printf; print; _ print; this; self ;__ name __; bool; true; type; thisavar; R_U_ready; Int; True; if; do; access

Is a legal python identifier

Print; if is the python keyword.

8.

#! /User/bin/env python #-*-coding: UTF-8-*-'maketextfile. py -- create text file 'import osls = OS. linesepwhile True: fname = raw_input ("enter the name of the file to be created ". decode ("UTF-8 "). encode ("gbk") # judge whether the file already exists. if yes, re-enter if OS. path. exists (fname): print u "error: '% s' file already exists! "% Fname else: breakall = [] print u" \ n enter the content of each line and enter the English state '.' in a single line to complete the input. \ N "while True: entry = raw_input (" Enter the content without a row: \ n ". decode ("UTF-8 "). encode ("gbk") # determine whether the input content is an exit character. if yes, end the input if entry = '. ': break else: all. append (entry) # create a file and open fobj = open (fname, 'w') in write mode # Write each input line to the fobj file. writelines (['% s % s' % (x, ls) for x in all]) fobj. close () print u "created successfully! Program ended! "

9. Operating System: Windows 10 64-bit

OS. linesep: '\ r \ N'

10. ① try-try t-else substitution

#! /User/bin/env python #-*-coding: UTF-8-*-'maketextfile. py -- create text file 'import osls = OS. linesepwhile True: fname = raw_input ("enter the name of the file to be created ". decode ("UTF-8 "). encode ("gbk") # judge whether the file already exists. If yes, enter try: fobj = open (fname) retry t IOError: break else: print u "again: "% fname all = [] print u" \ n already exists in the % s file. Please enter the content of each line and enter the English status 'in one line '. 'complete the input. \ N "while True: entry = raw_input (" Enter the content of each row: \ n ". decode ("UTF-8 "). encode ("gbk") # determine whether the input content is an exit character. if yes, end the input if entry = '. ': break else: all. append (entry) # create a file and open fobj = open (fname, 'w') in write mode # Write each input line to the fobj file. writelines (['% s % s' % (x, ls) for x in all]) fobj. close () print u "created successfully! Program ended! "

② OS. path. exists () replace try-release T-else

#! /User/bin/env python #-*-coding: UTF-8-*-'readtextfile. py -- read and display text file 'import OS # user input the file to be opened fname = raw_input ("Enter the file name:") print # Try to open the file in reading mode if OS. path. exists (fname): fobj = open (fname, 'R') for eachline in fobj: print eachline, fobj. close () else: print "error: file does not exist! "

11.

#! /User/bin/env python #-*-coding: UTF-8-*-"readTextFile. py -- read and display text file "# user input file fname = raw_input (" Enter file name ". decode ("UTF-8 "). encode ("gbk") print # try to open the file in Read mode try: fobj = open (fname, 'R') handle T IOError, e: print "*** file open error:", eelse: # print the file content for eachline in fobj: print eachline. strip ('\ n') fobj. close ()

Tip: The strip () method of the string is used to remove the specified character from the string header and tail. It returns the new string after the specified character is removed.

Eg:

str = "11111Let's see how the strip() works.1111111"print str.strip('1')

Output:

Let's see how the strip() works.

12. The modified program is as follows:

#! User/bin/env python #-*-coding: UTF-8-*-'read _ or_makeTextFile.py -- read and display text file, or create text file 'import osls = OS. linesepwhile True: choise = raw_input ("Enter 1 to open the file, input 2 to create the file, and enter 3 to exit :". decode ("UTF-8 "). encode ("gbk") if not choise. isdigit (): print u "Enter the number" elif int (choise) = 1: fname = raw_input ("Enter the file name :". decode ("UTF-8 "). encode ("gbk") print # Try to open the file if OS in Read mode. path. exists (fname): fobj = Open (fname, 'R') for eachline in fobj: print eachline, fobj. close () else: print "error: the file does not exist! "Elif int (choise) = 2: while True: fname = raw_input (" enter the name of the file to be created ". decode ("UTF-8 "). encode ("gbk") # judge whether the file already exists. if yes, re-enter if OS. path. exists (fname): print u "error: '% s' file already exists! "% Fname else: break all = [] print u" \ n enter the content of each line and enter the English state '.' in a single line to complete the input. \ N "while True: entry = raw_input (" Enter the content of each row: \ n ". decode ("UTF-8 "). encode ("gbk") # determine whether the input content is an exit character. if yes, end the input if entry = '. ': break else: all. append (entry) # create a file and open fobj = open (fname, 'w') in write mode # Write each input line to the fobj file. writelines (['% s % s' % (x, ls) for x in all]) fobj. close () print u "created successfully! Program ended! "Elif int (choise) = 3: print u" the program will exit "break else: print u". Please enter again"

13. The modified program is as follows:

1 #! User/bin/env python 2 #-*-coding: UTF-8-*-3 4 'read _ or_makeTextFile.py -- read and display text file, or create text file '5 6 import OS 7 ls = OS. linesep 8 print OS. getcwd () 9 while True: 10 choise = raw_input ("Enter 1 to open the file, input 2 to create the file, input 3 to edit the file, and exit input 4 :". decode ("UTF-8 "). encode ("gbk") 11 12 if not choise. isdigit (): 13 print u "Enter the number" 14 elif int (choise) = fname = raw_input ("Enter the file name :". decode ("UTF-8 "). encod E ("gbk") 16 17 print 18 19 # Try to open file 20 21 in Read mode if OS. path. exists (fname): 22 fobj = open (fname, 'R') 23 for eachline in fobj: 24 print eachline, 25 fobj. close () 26 else: 27 print u "error: file does not exist! "28 elif int (choise) = while True: 30 fname = raw_input (" enter the name of the file to be created ". decode ("UTF-8 "). encode ("gbk") 31 # determine whether the file already exists. if yes, enter 32 if OS again. path. exists (fname): 33 print u "error: '% s' file already exists! "% Fname34 else: 35 break36 all = [] 37 print u" \ n enter the content of each line and enter the English state '.' in a single line to complete the input. \ N "38 39 while True: 40 entry = raw_input (" Enter the content of each row: \ n ". decode ("UTF-8 "). encode ("gbk") 41 # determine whether the input content is an exit character. if yes, end the input 42 if entry = '. ': 43 break44 else: 45 all. append (entry) 46 # create a file and open 47 fobj = open (fname, 'w') in write mode 48 # Write each input line to 49 fobj in the file. writelines (['% s % s' % (x, ls) for x in all]) 50 fobj. close () 51 print u "created successfully! Program ended! "52 elif int (choise) = 3:53 fname = raw_input (" Enter the file name :". decode ("UTF-8 "). encode ("gbk") 54 print55 56 # Try to open the file in read/write mode ("r +") 57 if OS. path. exists (fname): 58 fobj = open (fname) 59 count = len (fobj. readlines () 60 fobj. close () 61 lines = [] 62 for I in range (1, count): 63 line = raw_input ("Enter the content of the modified row :". decode ("UTF-8 "). encode ("gbk") 64 lines. append (line) 65 while True: 66 save = raw_input ("save modify input 1, do not save input 2 :". decod E ("UTF-8"). encode ("gbk") 67 if not save. isdigit (): 68 print u "Enter the number 1 to save or 2 to save !! "69 elif int (save) = 1: 70 fobj = open (fname, 'w') 71 fobj. writelines (['% s % s' % (x, ls) for x in lines]) 72 fobj. close73 print u "modified successfully! "74 break75 elif int (save) = 2: 76 break77 else: 78 print u" error! The file does not exist! "79 80 elif int (choise) = 4: 81 print u" program will exit "82 break83 else: 84 print u" Please input again"

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.