Basic syntax for Python first-time experience (2): python first-time experience

Source: Internet
Author: User

Basic syntax for Python first-time experience (2): python first-time experience

All test statements are based on Python 2.7.6 and Ubuntu 14.04 LTS.

Learn Python by yourself. please correct me if anything is wrong. Thank you.

The vast majority of examples are python tutorials from vamei in the blog Park.

1. file read/write operations

1. Reading files by byte
#! /Usr/bin/env python # coding = UTF-8 # default python encoding is ASCII encoding to support Chinese # plus coding = UTF-8 statement # open () open File r read-only w write a append r + w + a + mode # a + mode open file pointer pointing to the end, r pointer in file header # f. tell () test file offset position f. seek () changes the file stream file = open('test.txt ', 'R') content = file. read (1024) # read N bytes of data file. close () print "read by byte:", '\ n', content

There are no complicated file stream operations in other advanced languages. open () functions can directly operate on file objects, which is simple and crude.

 

2. Reading files by row

File = open('test.txt ', 'R') content = file. readline () # Read a row of file. close () print "read by row:", "\ n", content

Remember to close the Text object after use. File. close ()

 

Iii. file write operations

# Open a file. If not, create file = open('temp.txt ', 'w') file. write ('python is good! \ N') # erase the file content and write only one row of file. writelines (['objective-c is bad! \ N', 'SWIFT is wtf? \ N', 'c # and Java which better ']) file. close () file = open('temp.txt ', 'rw') content = file. readlines () # Read all rows and store them in the list file. close () index = 0 while (index <len (content): print content [index] index + = 1

Write multiple lines to the file, read all rows using readlines (), and return a list object. The naming format of object methods in Python is not the camper method,

As a result, I often write a format similar to the upper case of the first letter of readLines (). I feel that the Python method name is affected by the C language, and this needs to be adapted slowly.

 

2. simple use of modules

Create an utils. py file in the directory test. py first. We will combine the previously written functions for determining the leap year with the custom SuperList class for other modules to use.

#! /Usr/bin/env python # coding = UTF-8 # Add a custom list subclass to a subclass similar to [1, 2, 3]-[2, 3] operation support # This method comes with two _ underlines. It is a special method class SuperList (list): def _ sub _ (self, B ): # self is a SuperList object. You can use a method similar to list [:] to represent the entire list object a = self [:] B = B [:] # If list contains elements in B, after the loop ends, len () returns the number of elements in the list while len (B)> 0: element_ B = B. pop () # pop () pops up the last element value from the list and returns its value if element_ B in a:. remove (element_ B) return a # Here, the minus sign operator represents the _ sub _ () method. Similarly, the + operator represents the _ add () method () __# Special methods and operators are equal price # print SuperList ([1, 2, 3, 4])-SuperList ([1, 2]) # whether the function is defined as a leyear def isLeafYear (year ): return year % 400 = 0 or (year % 100! = 0 and year % 4 = 0)


Create a model. py file to import the utils module.

#! /Usr/bin/env python # coding = UTF-8import utils # One. the py file constitutes a module '''import a as B # introduce module a, and rename module a as bfrom a import function1 # introduce the function1 object from module. When calling an object in a # We do not need to describe the module, that is, directly use function1 instead of a. function1. From a import * # introduce all objects from module a (similar to Java's guide package) ''' # Python search module sequence ''' 1. folder where the program is located 2. standard Library installation path 3. the path ''' # module contained in the operating system environment variable PYTHONPATH. object To call a = utils. isLeafYear (2004) print als = utils. superList ([2, 3, 4, 5])-utils. superList ([1, 2, 5]) print ls

Use existing methods or classes by means of import, which is similar to operations on classes in Object-Oriented Java C.

 

3. Module package

Place functional modules in a folder and import them using the import module package name and module name.

Folder name. File Name

Create a maths folder and create an empty _ init _. py file, telling python that this is a module package

Create the uitls. py file and add the sum of squares to evaluate 1 ~ The function for the sum of n digits is as follows:

#! /Usr/bin/env python # coding = UTF-8 # Calculate the sum of squares of two numbers def square_sum (a, B): c = a ** a + B ** B return, b, c # evaluate 1 ~ And def sum (n) of n: c = n * (n + 1)/2 return c

Create test_01.py in the home directory as follows:

#!/usr/bin/env pythonimport maths.utils as functionprint function.square_sum(1,2)print function.sum(100)

 

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.