Python Basics (1)

Source: Internet
Author: User

operator

Displacement: 2<<3 turn decimal into binary and move 3 bits to the left (increase)

2 >>3 convert decimal to binary and move right 3 bits (decrease)
Comparison: <, >, <=, >=,! =

Bitwise OR: | Both binaries have 0 in the same position and 0 in all other cases, 1

Bitwise AND: & two binary same position 1 is 1, all other cases are 0

Bitwise XOR: ^ Two binary identical locations are unequal when 1, equal to 0

Operator precedence: monocular > Binocular > Compare > Logic

python data type

List:

L.append (object), L.extend (object), L.pop (index), L.insert (Index,object), L.remove (object), L.count (object), L.index (index), l.sort order, L.reverse reverse

Ps.index and remove should be judged or wrongly captured.

list slices (Li2=li and li2=li[:] Not the same, the former if you modify the list of two lists are modified):

list[:] Entire list slice

List[2:5] Elements of index=2,3,4

List[::-1]=l.reverse

LIST[::2] Index is an odd number of elements

LIST[1::2] Index is an even number of elements

Meta-group :

Tuples cannot change elements

T = (2,) if only one element must be added with a comma

tuple slices (T2=t and t2=t[:] Like, a change of two are modified)

Collection :

The elements of the collection are unique, unordered, and the element can be a tuple, but not a list

S.difference = S-S1 Difference Set

S.intersection = S & S1 intersection

s | S1 and set

S.add () Adding elements

S.update ([200,100]) does not add the list to the collection, but instead adds the elements of the table to the collection

S.discard and S. Remove, but discard does not error when the element does not exist

Dictionary:

D = {' key ': ' Value '}

D.keys () returns a list of keys in a dictionary

D.values () Returns a list of values in a dictionary

D.items () Returns a list of key-value pairs in a dictionary

D.iterkeys () returns a key with an iterator object

D.itervalues () returns a value with an iterator object

D.iteritems () returns a key-value pair with an iterator object

D.get (Key,value) If key exists returns true value, does not exist return value

D.has_key (Key) returns true if key exists, false if not present

D[key] = ' value ' If key exists will change the original value, does not exist then new Key:value

D.copy () D1 = D.copy (), copies the key:value of D to D1

To parse directory contents into iterators:

For k,v in D.items ():

Print K,v

Iterators:

Iterator > List parsing >for loops

The For loop can be followed by a list because it automatically converts the list to an iterator:

Li = [1,2,3,4]

it = iter (LI)

List resolution:

Li = [1,2,3,4]

Li2 = [i+1 for i in Li] is equivalent to

J=0

For I in Li:

LI2[J] = i+1

J + = 1


Li2 = [i+1 for i in Li if i%2 = = 0] is equivalent to

For I in Li:

If i%2 = = 0:

Li2.append (i+1) # # #使用这种形式需要定义li2为一个空列表


Li2 = [i+1 for i under li if i%2 = = 0 if I >3] = Li2 = [I+1 for i in if i%2 = = 0 and I >3] Equivalent to

For I in Li:

If i%2 = = 0 and i>3:

Li2.append (i+1) # # #使用这种形式需要定义li2为一个空列表


List empty li = [] tuple empty t = () set NULL s = set () dictionary null d = dict ()


Python Basics (1)

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.