Self-taught Python drip

Source: Internet
Author: User

1. First day

Note-Any content that is to the right of the # symbol is a comment. Annotations are primarily used as notes for the reader of the program.

The program should contain these two lines

#! /user/bin/python

#Filename:* *.py

2. Enter Help () in the open Python3.4 in the program to help us find information quickly (especially at the command line with a prompt)

3. Integer division is denoted by//notation

~ is a bitwise fetch OR-(X+1)

4. In the output of the string or "" will be better, with "' sometimes error

Output when print () 3.0 version after the parentheses

5. Usage of the input

Guess=int (Input ("Please enter a number"))//Semicolon is a prompt (since 3.0 after the raw-input into input) version, the input will be different

6. Conditional Judgment

If condition:

Elif Conditions:

Else

No switch statement in 7.Python

Precautions for 9.docstrings

Print (max.__doc__) Note two underscores for left and right

Docstrings convention: The first line starts with an uppercase letter, the period ends, and the second line starts with a detailed description of the third line.

10. Column means the name plus [] together, [] is the name of each variable, the list can have the same element, the list can also have another list, the list can also append the list itself, no error, the list subscript must be an int type

tuples and lists are very similar, except that tuples and strings are immutable, meaning that you cannot modify tuples. Tuples are defined by a comma-separated list of items in parentheses. Tuples are typically used when a statement or user-defined function can safely take a set of values, that is, the value of the tuple being used does not change. , the tuple has no append usage, and the list has

The dictionary we associate the key (name) with the value (details). Note that the key must be unique, as if two people happen to have the same name, you cannot find the correct information.

Note that you can only use immutable objects (such as strings) as keys to the dictionary, but you may want immutable or mutable objects as the values of the dictionaries. Basically, you should only use simple objects as keys.

Key-value pairs are tagged in the dictionary in such a way that: D = {key1:value1, key2:value2}. Note that their key/value pairs are separated by colons, and each pair is separated by commas, all of which are included in curly braces.

Pathon after 3.0 dict deleted the Has_key function, but instead uses **in# #的方法

The two main features of a sequence are index operators and slice operators. The index operator allows us to fetch a specific item from the sequence. The slice operator allows us to get a slice of the sequence, which is part of the sequence.

Represents part of the sequence Shoplist[1:3], all [:], starting from a seat to the last [2:], the index can also be negative, in which case the position is calculated from the end of the sequence. Thus, Shoplist[-1] represents the last element of the sequence and Shoplist[-2] The penultimate item of the crawl sequence, the first number in the slice operator (preceded by a colon) indicates where the slice begins, and the second number (after the colon) represents the slice
Where to end. If you do not specify the first number, Python starts from the beginning of the sequence. If you do not specify a second number, Python stops at the end of the sequence. Note that the returned sequence starts at the start position and ends just before the end position. That is, the start position is contained in the sequence slice, and the end position is excluded from the slice.

Reference: The variable name points to the memory of the object stored in your computer. This is called the name-to-object binding.

Whether string begins with a string startswith ("), whether there is a character ' in * *, if there is a string **.find (')

The. Sayhi method in class 11 does not have any arguments, but still has self when the function is defined.

The __init__ method runs immediately when an object of the class is established. This method can be used to initialize your object with some of the things you want. Note that the start and end of this name are double underlines. Equivalent to a constructor, a sample:

def __init__ (self,name):
Self.name=name

About private and public issues

If you use a data member name with a double-underscore prefix such as __privatevar,python's name management system, it effectively takes it as a private variable. This makes it a rule that if a variable is only meant to be used in a class or object, it should be prefixed with a single underscore. Other names will be public and can be used by other classes/objects. Remember that this is only a convention, not what Python requires (unlike a double underscore prefix).

12. Destructors

def __del__ (self): to define Destructors

Examples of inheritance

Class Teacher (Schoolmember):
"' Represents a teacher."
def __init__ (self, name, age, salary):
Schoolmember.__init__ (self, name, age)
Self.salary = Salary

Python does not automatically invoke the constructor of the base class, you have to call it yourself.

13.

Read and Write files: You can open a file by creating an object of the file class, using the read, ReadLine, or write method of the file class to read and write files appropriately. The ability to read and write to a file depends on the pattern you specify when you open the file. Finally, when you are done with the file, you call the Close method to tell Python that we have finished using the file.

f = open (' Poem.txt ', ' W ') # Open for ' W ' riting
F.write (poem) # Write text to file
F.close () # Close the file

f = open (' Poem.txt ')
# If no mode is specified, ' R ' ead mode was assumed by default
While True:
line = F.readline ()
If Len (line) = = 0: # Zero length indicates EOF
Break
Print (line)

Self-taught Python drip

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.