Python Basics _ Learning Notes

Source: Internet
Author: User
Tags pow string format cmath

Python Basics _ Learning Notes


Python contains the built-in sequence in 6:

The two common types are: list and Ganso

String, Unicode string, buffer object, Xrange object

The difference between a list and a ganso:

The list can be modified, and the meta-ancestor cannot modify the


Input: You need to define the format of your inputs

Raw_input: Converts any input into a string


Function:

Pow: can be calculated as a power #pow (2,3)

ABS: Absolute value of the number of #abs (-10)

Round: The value can be rounded to a given precision #round (3.456,1)

Module: is an extension that enhances Python functionality

Math: A function of mathematical operations

Floor: To achieve a method #math. Floor (3.9)

Ceil: To achieve a #math of the law. Ceil (3.4)

SQRT: Realize open square #math. sqrt (9)

Cmath: Calculating Imaginary numbers

SQRT: realizes the open square #cmath of imaginary numbers. sqrt (-9)

Methods for translating numbers into strings:

REPR: Can translate numbers into string format #repr (23) or ' 23 '

STR: Values can be translated into string format

Raw string: Each character entered in the original string will be consistent with the way it is written

Print R "Let\ 's Go" will be output as: Let\ ' s Go

Sequence:

List:

Index: All elements in a sequence are numbered and increment from 0.

Sharding: Access to a range of elements by two indexes separated by colons


Max: Determine the maximum number of values in a sequence #max ([1,2,3,4)]

Min: Determine the smallest number of values in a sequence #min ([1,2,3,4])

Len: Determine the length of the sequence #len ([1,2,3,4])

Del: Remove elements from the list #del a[-1]

Append: Append list #a =append (b)

Count: The number of statistical elements #a. COUNT (1)

Extend: Extended list #a. Extend (b)

Index: Finds the indexed #a of the first occurrence of a value. Index (2)

Insert: Inserts the specified value into the specified position #a. Insert (2, ' ma ')

Pop: can be a list of elements, the default is the last one, you can specify #a. Pop () |a.pop (1)

Remove: Removes the first element #a the specified match to. Remove (' ma ')

Reverse: Stores the elements in the list in reverse #a. Reverse ()

Sort: #a the order in which the list is sorted. Sort ()

CMP: Compare two values, return 0, 1, -1 #cmp (up to four)

Ganso

Tuple: Similar to the list function in the sequence #tuple (' ma ') |tuple ([+])

String:

a=1.23

b= ' Long '

Print "num is%d"%a

Print "num is%.2f"%a

Print "Name is%s"%b

Print "Name is%s num was%d float is%.2f"% (b,a,a)

%10D: Indicates the output field length is 10

%010: Indicates the output field length is 10, if not enough can be used 0-padded

%.2F: Indicates that the exact number of digits is 2 decimal places

%-10D: Indicates left alignment, not enough to use space to supplement

String method:

Find: You can find the specified string in a longer character, return the leftmost index position of the string, if no

Returns 0

' This is a example '. Find (' example ') results: 10

' This is a example '. Find (' test ') Result: 1

' This is a example '. Find (' example ', 0,10) results: 1

Join: Used to add elements to a string queue

A= ' Test '

': '. Join (a) is added between each character in the string:

Lower: Returns the lowercase of a string

A= ' This is A EXAMPLE '

A.lower ()

Upper: Returns the uppercase of a string

A= ' This is a example '

A.upper ()

Title: Converting a string to a caption string

A= ' This is a example '

A.title ()

Replace: Replacing the matched string

A= ' This is example '

A.replace (' is ', ' isn't ')

Split: Splits a string based on a given delimiter

A= '/etc/init.d/rc.local '

A.split ('/')

Strip: Removing spaces on both sides of a string

A= ' This is a example '

A.strip ()


Dictionary:

Find his definition (value) by looking for a specific word (key)

The keys in the dictionary are unique, and the values are not unique

dict function:

items=[(' name ', ' Test '), (' age ', 14)]

D=dict (items)

d[' name ']

Len (d) Returns the number of key-values in dictionary D

D[K] Returns the value associated to the key

D[k]=v to associate the value V to the K key

Del D[k] Delete item with key K

K in D determine if dictionary D contains key k

Example for:

people={' test1 ': {' phone ': 123, ' addr ': ' Beijing '}, ' test2:{' phone ', 12345, ' addr ': ' Tianjing '}}

people[' test1][' phone ']

Html= ""

<title>% (title) s</title>

<body>% (body) s</body>

‘‘‘

data={' head ': ' This was example ', ' title ': ' This is a example ', ' body ': ' Hello world! '}

Print Html%data

Clear: Clears all the entries in the dictionary, and can clear the other dictionaries of negative values at the same time, equivalent to recursive cleanup

Items.clear ()

Copy: Copies the dictionary, the original dictionary will not be affected when the value is replaced, but if a value is modified, the original

Dictionaries are affected (similar to deleting values in an item)

Deepcopy: Deep copy, copy modification does not affect the original dictionary

From copy import deepcopy

Items.deepcopy ()

Fromkeys: Creates a new dictionary with the given key, the default corresponding value for each key is None

{}.fromkeys ([' Name ', ' age '])

Get: Gets the value of the key in the dictionary, if there is no error

Items.get (' name ')

Has_key: Determine if the dictionary contains a corresponding key, similar to K in B, but there is no such function in python3.0.

A.has_key (' name ')

Items: Display the dictionary as a list

A.items ()

Iteritems: Similar to items, but returns an iterative object

B=a.iteritems ()

Keys: Displays the keys in the dictionary as a list

Iterkeys: Similar to keys, except that an iteration object is generated

Pop: Removes the value of the given key

A.pop (' name ')

Popitem: Randomly deleting items from a dictionary

A.popitem ()

SetDefault: You can set a default value for a key that has no value, and if a key has a value, it returns the corresponding key value.

A.setdefault (' name ', ' Test ')

Update: Updates the entries in the dictionary, if the old dictionary and the new dictionary are duplicated, the duplicate entries are overwritten

A.update (b)

Values: Returns the value in the dictionary as a list

A.values ()

Itervalues: Returns the iteration result of the value in the dictionary, which can be called

A.itervalues ()


Python Basics _ Learning Notes

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.