Python built-in Type---next Day notes

Source: Internet
Author: User
Tags shallow copy

Strongly typed languages

Comparison of two objects *******************

1, value comparison, whether the data in the object is the same = =

2, identity comparison, Two variable names refer to whether the same object

3, type comparison,

Example:

Core Data type *****************

number: int Lang float complex bool

Character: Str Unicode

Listing: List

Dictionary: Dict

Tuples: tuple

Files: file

Other types: set (set) Frozenset class type None

Other file type Tools: pipes FIFOs sockets


Type conversion ****************

STR () repr () format (): converts non-character data to characters

int (): Convert to Integer

Float ():

In [1]: s = "My boss is lihong"

List (s): Convert string s to list

In [2]: l1 = List (s)

In [3]: print L1

[' M ', ' y ', ' ', ' b ', ' o ', ' s ', ' s ', ', ' I ', ' s ', ', ' l ', ' i ', ' H ', ' o ', ' n ', ' g ']

Tuple (s): Convert string s to tuples

in [4]: t1 = tuple (s)

In [5]: print T1

(' M ', ' y ', ' ', ' b ', ' o ', ' s ', ' s ', ', ' I ', ' s ', ', ' l ', ' i ', ' H ', ' o ', ' n ', ' g ')

Set (s): convert string s to collection

In [6]: S1 = set (s)

In [7]: print S1

Set ([' ', ' B ', ' G ', ' I ', ' H ', ' M ', ' l ', ' o ', ' n ', ' s ', ' y '])

Frozenset (): Converts a string s to an immutable collection

Dict (d): Create a dictionary where D must be a tuple sequence (key,vlue)

In [all]: L3 = [(' a ', 1), (' b ', one-to-one), (' c ', 45)]

In []: print L3

[(' a ', 1), (' b ', one-to-one), (' c ', 45)]

In []: D1 = dict (l3)

In [+]: print D1

{' a ': 1, ' C ': $, ' B ': 11}

Chr (): converts an integer to a character

Ord (): Converts a character to an integer value

Hex (): Converts an integer to 16 binary string

Bin (): converts an integer to 2 binary characters

Oct (): converting integers to 8 characters


Number Types And:

Python numeric literals: boolean, integer, float, plural

True:1

false:0

Import Math

Sequence type

Character Type:

String literal: Bar text in single, double, or triple quotation marks (can be three single or three double quotation marks)

If you want to use Unicode encoding, use the character u to identify before the character, such as U "tre"

Document String: A module, the first statement of a class or function is a character, and the string becomes a document character, which can be referenced with the __doc__ property

Operator:

Index operations

Slice operations

Extended operations


Get help ******************

Dir (sir) get a list of names

Help (str.find)


List Type *****************

Container type:

An ordered set of arbitrary objects that access elements through an index, mutable objects

Supports heterogeneous, arbitrary nesting

Define a List:

L1 = [] empty list

L2 = [1,2,3,4,5]

L3 = [1, ' b '] heterogeneous

L4 = [[1,2],[' a ', ' b ']] nesting

Support in situ modification:

Modifies the specified index element, modifies the specified shard, deletes the statement, and the built-in method

l2[1] = 32

l2[3] = ' xyz ' modification

l2[1:3] Show elements 1 and 2

l2[1:3] = [] Delete

L2.del (l2[1:]) Delete

In [all]: l2.append (l3)

In [+]: print L2

[1, 3, ' xyz ', 5, 106, [1, ' b ']]

In [one]: l3.

L3.append l3.extend L3.insert L3.remove l3.sort

L3.count L3.index L3.pop L3.reverse

In [+]: L1 = [+]

In [+]: L2 = [' x ', ' y ', ' z ']

In [+]: L1 + L2 connection Two list return a new list

Out[19]: [1, 2, 3, ' x ', ' y ', ' z ']

In []: L1 * 2 repeat L1 two times

Out[20]: [1, 2, 3, 1, 2, 3]

In: Member Relationship Qualifier

Not in: ...

2 in L1 returns True

Range ()

To copy a list:

L2 = L1 because the list supports the original modification, so if the modification at this time L1,L2 will also be modified, this is called shallow copy

L3 = l1[:] This is the time to copy a new list assignment to l3, and then modify the l1,l3 without changing

L5 = copy.deepcopy (l1) deep copy is also copied instead of pointing to the same address, as above

List Parsing: []

Tuple tuple****************

Expression Symbol: ()

Container type:

An ordered collection of arbitrary objects, which are accessed through an index, immutable objects, fixed Lengths

Supports heterogeneous, nested

definition: T1 = (1,2,3,4,5,1) parentheses can be omitted

T1.count (1) Count

T1.index (3) returns the specified index

Common Operations:

Print t1[1:]

T2 = () defines an empty ancestor

T3 = ()

+ Two meta groups can be combined and a new tuple generated

*

In

Not in

Dictionary dict*****************

In other programming languages, also known as associative arrays or hash lists:

Element access via key, unordered set, variable type container, variable length, support for heterogeneous, support nesting

Symbol {}, comma-delimited, colon-separated key-value Mapping table

{}: Empty Dictionary

In [+]: D1 = {' x ': +, ' y ': [1,2,3,4]}

In [x]: d1[' y '][1:]

Out[41]: [2, 3, 4]

In [all]: d1[' x '] = 88

In [+]: print D1

{' y ': [1, 2, 3, 4], ' x ': 88}

Dictionary copy:

D2 = D1.copy () creates a dictionary that is identical to D1 and assigns a value to D2

d3 = D1

In []: d1.

D1.clear

D1.get refers to a nonexistent key and does not throw an exception

D1.iteritems returns an iterator, I1.next can be displayed one at A-until exhausted

D1.keys List of returned keys

D1.setdefault

D1.viewitems

D1.copy

D1.has_key Determine if a key value exists

D1.iterkeys

D1.pop The Key-value mappings for the specified (specified Key) popup

D1.update Merge a dictionary into the current dictionary (note that the keys are the Same)

D1.viewkeys

D1.fromkeys

D1.items dictionaries are split into tuples, each pair of key values form a tuple, forming a tuple list

D1.itervalues Returns an iterator that contains the value

D1.popitem Random popup key-value mappings

D1.values A list of return values

D1.viewvalues

In [the]: t1,t2 = d1.it

D1.items D1.iteritems D1.iterkeys d1.itervalues


In [the]: t1,t2 = d1.items () variable unpacking

In [+]: print T1

(' y ', [1, 2, 3, 4])

In [+]: print T2

(' x ', 88)


In []: m1,m2 = D1

In [+]: print M1

Y

In [[]: print m2

X


Dict (), which defines the Dictionary's


zip******************

In [+]: zip (' xyz ', ' 123 ', ' ABC ')

Out[58]: [(' x ', ' 1 ', ' a '), (' y ', ' 2 ', ' b '), (' z ', ' 3 ', ' C ')]

In []: dict (' xyz ', ' 123 ') constructs a dictionary

Out[60]: {' x ': ' 1 ', ' y ': ' 2 ', ' z ': ' 3 '}


This article is from the "linux-halt" blog, make sure to keep this source http://loveall.blog.51cto.com/8658573/1872728

Python built-in Type---next Day 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.