Python Learning Notes

Source: Internet
Author: User

Char,byte not supported

REPR equivalent to anti-quote

ID (x)

ID (a) ==id (b) <=> A is B


Foostr= ' ABCDE '

foostr[::-1]= ' EDCBA '

foostr[::-2]= ' ECA '//take one from one



############ R ' \ n ' \ n ' ################

>>> ' \ n '

' \ n '

>>> print ' \ n '



>>> print R ' \ n '

\ n


################ RE Regular ##################

Import re

>>> M=re.search (R ' \\[rtfvn] ', R ' Hello world\n ')


>>> if M is not none:m.group ()

...

' \\n '



Add a U ################ when a string appears in the ############# program

>>> u ' \1234 '

U ' S4 '

>>> u ' \u1234 '

U ' \u1234 '

>>> str2= ' LMN '

>>> str3= ' xyz '

>>> Max (STR2)

N

>>> Max (STR2)

N

>>> min (STR3)

' X '

>>> str= ' ab12mn '

>>> Max (str)

' B '


############### Enumerate ##################

>>> for I,t in enumerate (s):

... print I,t

...

0 F

1 o

2 O

3 b

6 #

3 4


>>> user_input=raw_input (' Enter u Name: ')

Enter u Name:wangweiwei

>>> Print User_input

Wangweiwei



############## isinstance ###################

>>> not isinstance (' foo ', Unicode)

True

>>> isinstance (U ' \0xab ', str)

Traceback (most recent):

File "<stdin>", line 1, in <module>

Typeerror:isinstance () Arg 2 must be a class, type, or tuple of classes and types

>>> isinstance (U ', basestring)

True

>>> isinstance (' foo ', basestring)

True

>>>


######### Capitalize,center,count,isspace,upper,lower ########################

>>> s= ' abc '

>>> s.capitalize ()

' ABC '

>>> S.center (10)

' ABC '

>>> S.count (s,0,12)

1


>>> S.isspace ()

False

>>> S.upper ()

' ABC '

>>> S.lower ()

' ABC '



############# Split, join ########################

>>> queset= ' what\ ' s u favorite color? '

>>> Queset.split ()

["What ' s", ' u ', ' favorite ', ' color? ')

>>> ': ' Join (Queset.split ())

"What ' S:u:favorite:color?"


########## #reversed #####################

s=[' They ', ' is ', ' Flowers ']

>>> for T in reversed (s):

.. print T

...

Flowers

Is

They

>>> Sorted (s)

[' Is ', ' flowers ', ' they ']



############ use Unicode (), UNICHR () where the STR,CHR will be used


#!/usr/bin/python

Codec= ' Utf-8 '

Filename= ' file.txt '

Fp=open (filename, ' W ')

Str=u ' Hello world!\n '

Str=str.encode (Codec)

Print str

Fp.write (str)

Fp.close ()


Hp=open (filename, ' R ')

Str=hp.read ()

Hp.close ()

Str=str.decode (Codec)

Print str



################## append (), pop ()





>>> CMP ((4,2), (3,5))

1


########## Shallow copy = person[:]: Copy, copy () deep copy: Import copy Copy.deepcopy ()

>>> person=[' A ', ' B ', ' C ']

>>> habby=person[:]

>>> Print Habby

[' A ', ' B ', ' C ']

>>>





########### dict (): Create Dictionary Fromkeys (): Built-in method for creating dictionaries



Fdict=dict ([' X ', 1],[' Y ', 2]))



>>> Ddict={}.fromkeys ((' x ', ' Y '),-1)

>>> ddict

{' Y ':-1, ' X ':-1}


############ #访问字典中的键

>>> for key in Ddict.keys ():

... print ' key=%s,value=%s '% (Key,ddict[key])

...

Key=y,value=-1

Key=x,value=-1



############## #删除字典值del ddict[' x '],ddict.clear ()



############## #dict. Get ()


############# Dict.itere ()

############# Iteritems (), Iterkeys (), Itervalues ()




Update method for ##################### dictionary

>>> dic2={' A ': ' 1 ', ' B ': ' 2 '}

>>> dic3={' C ': ' 3 ', ' d ': ' 4 '}

>>> dic2.update (DIC3)

>>> Print Dic2

{' A ': ' 1 ', ' C ': ' 3 ', ' B ': ' 2 ', ' d ': ' 4 '}

>>> Dic2.clear ()

>>> Print Dic2

{}

>>>




####################### #setdefault (): If this value does not exist in the dictionary, it can be set up immediately, if present, it can be directly taken to



################# #fromkeys ()


>>> {}.fromkeys (' xyz ')

{' Y ': None, ' X ': none, ' Z ': none}

>>> {}.fromkeys (' love ', ' honor '), True)

{' Love ': true, ' honor ': true}






########## #只有程序一运行便可以访问这三个文件sys. Stdin,sys.stdout,sys.stderr




Tempfile used to generate a temporary file name

Shutil provides advanced file access capabilities










This article is from the "Wang Nimei Adult Road" blog, reprint please contact the author!

Python Learning Notes

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.