Python Learning (10)

Source: Internet
Author: User
Tags reverse text
ArticleDirectory
    • 1. user input
    • 2. Files
    • 3. Pickle)
10. Input/Output

In some cases, you haveProgramInteract with users. For example, you need to obtain the input from the user and then output the calculation result. We can do this through the input () and print () functions respectively.
For the output, we can also use various methods of the STR (string) class. For example, you can obtain a right-aligned string with a specified width using the just ust method. For details, see help (STR ).
Another common input/output type is file processing. The ability to create, read, and write files is required by many programs. We will explore how to implement these features in this chapter.

1. user input
 
# User_input.py def reverse (text): return text [:-1] def is_palindrome (text): return text = reverse (text) Something = input ('enter text :') if (is_palindrome (something): Print ("Yes, it is a palindrome") else: Print ("no, it is not a palindrome ")

Output:

C: \ Users \ Administrator> Python D: \ Python \ user_input.py

Enter Text: sir

No, it is not a palindrome c: \ Users \ Administrator> Python D: \ Python \ user_input.py

Enter Text: racecar

Yes, it is a palindrome

Working principle:

In this example, we use the slice operation to reverse the text. We have learned how to use seq [A: B] (from A to B) to slice the sequence.

For the slice operation, we can also specify the third parameter step. The step size defaults to 1 to return a continuous part of the text. Given a negative step-1, the reverse text is returned.

The input () function receives a real string parameter and prints it to the user. Then, the function waits for the user to input something. Once the user presses the Enter key, the input ends. The input function returns the input text.

Then we reverse the text. If the reversed text is the same as the original text, it indicates that it is a back-to-text.

2. Files

You can create a file class object to open a file and use the read, Readline, or write methods of the file class to read and write the file properly. The ability to read and write files depends on the mode specified when you open the file. Finally, when you complete file operations, you call the close method to tell Python that we have used the file.

For example:

# Filename: using_file.py poem = ''' \ programming is funwhen the work is doneif you wanna make your work also fun: Use python! '''F = open('poem.txt ', 'w') # Open for 'W' ritingf. write (POEM) # write text to filef. close () # close the file F = open('poem.txt ') # If no mode is specified, 'R' EAD mode is assumed by defaultwhile true: line = f. readline () If Len (line) = 0: # Zero Length indicates EOF break print (line, end = '') F. close () # close the file

Output:

C: \ Users \ Administrator> Python D: \ Python \ using_file.py

Programming is fun

When the work is done

If you wanna make your work also fun:

Use python!

Working principle:

First, we open a file through the built-in function open. In the function, we specify the file name to be opened and the desired open mode. The open mode can be read mode ('R'), write mode ('W'), or append mode ('A '). In addition, we can also process file files ('T') and binary files ('B '). There are actually many available modes. For details, see help (open ). By default, open treats the file as a text file 'T' and opens it in Read mode 'R.

In this example, we first open the file in write text mode, use the write method of the file object to write the file, and call close to close it. Then we open the same file again for reading. Here we do not need to specify the open mode because 'read text file' is the default open mode.

In the loop, we use the Readline method to read each row of the file. This method returns an entire line of text including the line break at the end. When an empty string is returned, it means that we have come to the end of the file, so we use the break to jump out of the loop.

By default, the print () function automatically prints a line feed. Because a line break is included at the end of the line read from the file, we specify the end parameter to suppress the line feed.

Finally, close the file.

Now, check the content of the poem.txt file to make sure that the program has actually written and read the file.

3. Pickle)

Python provides a standard module named pickle to store or read any Python object from a file. This is called a permanent storage object (persistently ).

For example:

# Filename: pickling. PY import pickle # The name of the file where we will store the objectshoplistfile = 'shoplist. data' # The list of things to buy shoplist = ['apple', 'mango', 'carrot'] # Write to the filef = open (shoplistfile, 'wb') pickle. dump (shoplist, f) # dump the object to a filef. close () del shoplist # destroy the shoplist variable # Read back from the storagef = open (shoplistfile, 'rb') storedlist = pickle. load (f) # load the object from the fileprint (storedlist)

Input:

C: \ Users \ Administrator> Python D: \ Python \ pickling. py

['Apple ', 'mango', 'carrot']

Working principle:

to store objects to files, we must first 'wb' write binary file mode to open the file and then call the dump function of the pickle module. This process is called a collection object.
next we use the load function of pickle to retrieve the object again. This process is called unpickling objects.

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.