29 Points of knowledge in the Python Foundation

Source: Internet
Author: User

(1) The difference between input and raw_input:

#-*-Coding:utf-8-*-

#除非有特别需要, you should try to use raw_input.

x = input ("Please enter your name:")

Print "Hello" +x

y = raw_input ("Please enter your name:")

Print "Hello" +y

# Please enter your first name: "Tom"

# Hellotom

# Please enter your name: Tom

# Hellotom


(2) STR and REPR parsing:

#-*-Coding:utf-8-*-

# The STR function converts the value to a string, and repr creates a human string and expresses it

x = "helloworld!"

Print repr (x)

Print str (20)

# Run Result:

# ' helloworld! '

# 20


(3) Find (), RFind (), Rindex (), index ()

Find (): Find the location of the character.

RFind (): The position of the reverse lookup character.

Index (): detects whether the string contains the specified substring, and if STR does not report an exception in the string.

Rindex (): whether the specified string is contained in the reverse lookup string.

(4) Count (): Calculates the number of strings in the specified string.

(5) Replace (): replaces the specified string.

(6) Split (): Cuts the string with the specified symbol.

(7) Capitalize (): Capitalize first letter.

(8) title (): Capitalize the first letter of each word.

(9) Lower (): Convert to lowercase.

(ten) Upper (): converted to uppercase.

(one) Startwith (): Starts with the specified content.

() EndsWith (): ends with the specified content.

(in) Ljust (): left-justified.

(+) Rjust (): Right-aligned.

() Center (): center alignment.

(+) Lstrip (): Deletes the space to the left of the specified string.

(+) Rstrip (): Deletes the space to the right of the specified string.

(+) strip (): Deletes a space for the specified string.

(+) partition (): A segmented string that does not delete spaces, cut into three sections.

() rpartition (): Reverse cut.

(+) Isalpha (): Determines whether the specified string is all letters.

IsDigit (): Determines whether the specified string is all pure digits.

Isalnum (): Determines whether the specified string contains numbers or letters.

Isspace (): Determines whether the specified string is a space.

Join (): Concatenate the strings in the list.

(26) List:

>>> list=["01.py", "WhoAmI"]

>>> List

[' 01.py ', ' WhoAmI ']

>>> List[0]

' 01.py '

List of additions and deletions to change:

Append (): Adds an element after the list.

Extend (): Adds another list behind the specified list.

Insert (): receives two parameters and inserts at the specified position.

The list uses indexes for modifications and lookups in conjunction with in and not.

Del (): Use subscript to delete del list name [index value]

Pop (): Deletes the last list name. Pop ()

Remove (): Deletes the specified value, list name. Remove (value)

(27) Tuples: functions are similar to lists, modifications are not supported, properties are read-only.

>>> tuple = ("Ha")

>>> tuple

(1, 2, 3, ' Ha ')

(28) Dictionary:

>>> dict = {"Name": "Xvge", "Age": 18}

>>> Dict

{' name ': ' Xvge ', ' Age ': 18}

>>> dict[' age ']

18

Use the Get () method to get a value by key, you can use the default value get (key, default value)

Use the Del method to delete and empty the dictionary using the Clear () method.

Len (): Calculates the key-value pairs in the dictionary.

When you define a dictionary, the key of the dictionary is the same, and the value corresponding to the key will overwrite the previous corresponding value.

Keys (): Returns the key of the dictionary

VALUES (): Returns the value of the dictionary

Item (): Returns the entry for the dictionary, using the way nested tuples are in the list.

Has_key (): Determines whether the specified key exists in the dictionary.

Enumerate (): Enumeration dictionary


(28) Function:

Use def for definition.

def test ():

Xxx

Description of the function: the first line of the definition function can be written directly to the string for function description

def test ():

"Function description"

Xxx

User-defined functions override system functions.

The parameters are passed in a different order, and the values of the formal parameters can be specified at the time of invocation.

def test (A,B,C):

Xxx


Test (A=XXX,C=XXX,B=XXX)

Default parameters for the function:

def test (A,B,C=XXX):

Xxx

1 def addall (a,*b,**c):

2 Print (a)

3 Print (b)

4 print (c)

5

6 AddAll (10,20,30,M=40,N=50)

Operation Result:

10

(20, 30)

{' m ': +, ' n ': 50}

(29) Object-oriented:

To define a class:

Class Name:

def test (self):

Xxx

To create an object:

Object name = Class name ()

Class name. Method ()

Class name. field

The field does not exist to add the field, and there is only an assignment to indicate.

Properties that access the class within a class use the "self. Property" To assign the value.

You can set the initialization method "__init__ ()" In the class, similar to the constructs in Java, which can be used to initialize the default property values in the class.

__str__ (self): The//str method is defined in the class and is used to display relevant information when the class name is printed.

return XXX

Hide data: It is recommended that you modify the properties in the class using the methods in the class.

__del__ (self): defined in the class, called when all objects are destroyed.

A method of calling a parent class in a subclass can be called by: Parent class. Method () or super (). Method ().


29 Points of knowledge in the Python Foundation

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.