A collection of images of Python's basic Learning Code

Source: Internet
Author: User
Def func1 (): Dict1 = {} Dict2 = {' name ': ' Earth ', ' Port ': +} return dict1,dict2def Func2 (): Return Dict ([' X ', 1] , [' Y ', 2]) def func3 (): Adict = {}.fromkeys ([' X ', ' y '],23) return adictdef Func4 (): alist = {' name ': ' Earth ', ' Port ': For keys in Alist.keys (): print "%s%s"% (Keys,alist[keys]) def func5 (): alist = {' name ': ' Earth ', ' Port ': 8    0} for keys in Alist:print "%s%s"% (Keys,alist[keys]) def func6 (akey): alist = {' name ': ' Earth ', ' Port ': 80}    If Akey in Alist:return True else:return falsedef func7 (akey): alist = {' name ': ' Earth ', ' Port ': 80} If Alist.has_key (akey): Return True Else:return falsedef func8 (): alist = {' name ': ' Earth ', ' Port ': 8 0} print ' host% (name) s is running on% (port) d '% alistdef func9 (akey): alist = {' name ': ' Earth ', ' Port ':--} if Ake Y in Alist:del Alist[akey] return True else:return falsedef func10 (): alist = {' name ': ' Earth ' , ' Port ': alist.cle}AR () return alistdef func11 (): alist = {' name ': ' Earth ', ' Port ': +} del alistdef func12 (akey): alist = {' Name ': ' Earth ', ' Port ': ' If Akey in Alist:return Alist.pop (Akey) def func13 (): alist = {' name ': ' Earth ', ' a ': +} BL ist = {' name ': ' earth ', ' Z ': 8080} return cmp (alist,blist) def func14 (): Alist = Zip ((' x ', ' Y '), ()) blist = Dict ([(  ' XY ' [i-1],i] for I in range (1,3)]) return Dict (alist), Blistdef func15 (): adict = {' name ': ' Earth ', ' Port ': +} bdict = Adict.copy () return Bdict,len (bdict) def FUNC16 (): adict = {' name ': ' Earth ', ' Port ': +} bdict = {' name ': ' Earth ', ' Port ': 8080} print Adict.keys () print adict.values () print adict.items () adict.update (bdict) Print Adictdef FUNC17 (): adict = {' name ': ' Earth ', ' Port ': +} for keys in sorted (adict): print ' adict%s have value%s '% (keys        , Adict[keys]) def func18 (akey): adict = {' name ': ' Earth ', ' Port ': +} If Adict.get (Akey): return True Else: Return ' no such key!' Def func19 (): adict = {' name ': ' Earth ', ' Port ': bdict} = {}.fromkeys (' abc ') print Bdict return Adict.setdefaul T (' name ', ' wycqhost ') Import timenowtime = Time.time () def gettime (nowtime): Return Time.strftime ('%y/%m/%d%h:%m:%s ', Time.localtime (nowtime)) login = {}def NewUser (): prompt = ' login desired: ' name = ' while true:name = RA W_input (Prompt) if Login.has_key (name): prompt = ' name taken,try another: ' Continue El Se:break pwd = raw_input (' passwd: ') login[name] = [ABS (PWD)] login[name].append (0) Print log Indef Olduser (): Nowtime = time.time () name = Raw_input (' login: ') if name not in Login:choose = Raw_input        (' Would you create a new user (y/n) ') if Choose.lower () [0] = = ' Y ': newuser () Else:pass ShowMenu () else:pwd = Raw_input (' passwd: ') passwd = login.get (name) [0] If ABS (hash (passwd)      ) = = ABS (hash (PWD)):      If login[name][1] = = 0:print Login print ' Welcome back ', name, ' You first time Loggin '                    Else:print ' Welcome back ', name if nowtime-login[name][1] <= 14400:             print ' You is already logged at time ', gettime (login[name][1]) login[name][1] = Nowtime Else: print ' login incorrect ' returndef showuser (): print ' Show all User: ' for user in Login.keys () : Print Userdef deleteuser (): Duser = raw_input (' Delete User: '). Lower () if duser in Login.keys (): Del Login[duser] else:print ' user%s is not exists '% Duser showmenu () def showmenu (): prompt = "' (n) EW u Ser login (o) LD user login (s) how all user (d) elete User (Q) uitenter choice: "Done = False and not Done:chos            En = False while not chosen:try:choice = Raw_input (Prompt). Strip () [0].lower () Except (Eoferror,keyboArdinterrupt): choice = ' Q ' print ' You picked%s '% choice if choice not in ' Noqds ':                 print ' Invalid option,try again ' Else:chosen = True if choice = = ' Q ':                Done = True if choice = = ' n ': NewUser () if choice = = ' O ': Olduser () if choice = = ' d ': deleteuser () if choice = = ' s ': showuse R () #if __name__ = = ' __main__ ': # showmenu () def func20 (): str1 = ' 093keffeoelgn ' t = Set (STR1) s = Frozenset (str 1) return t = = Sdef func21 (): Aset = set (' Xiewenbin ') if ' X ' in the Aset:print "x in Aset" Def FUNC22 (): As ET = set (' Strings ') aset.add (' http ') aset.update (' httpx ') aset.remove (' http ') aset-= set (' x ') for I in ASE T:print I del asetdef func23 (): Aset = set (' abc ') bset = set (' Abcedf ') return aset <= bsetdef Func2 4 (): Aset = set (' MarkShop ') bset = Frozenset (' earthshop ') print Aset | bset print bset & aset print aset ^ bset print aset-bsetdef func25 (): s = set (' Cheeseshop ') u = Froze    Nset (s) s |= set (' Xie ') print s = set (U) s &= set (' Shop ') print s S = set (U) S-= set (' Shop ') Print s S = set (u) t = frozenset (' bookshop ') s ^= t print s print len (s) import Osdef func26 (): Frozens ET ([' A ', ' B ', ' C ']) F = open (' Test.txt ', ' W ') for I in range (5): F.write ('%d\n '%i) f.close () F = open (' tes T.txt ', ' R ') print set (f) f.close () os.remove (' Test.txt ') def func27 (): alist = [' A ', ' B '] blist = [+] pr int Dict (Zip (alist,blist)) def func28 (): Adict = {' A ': 1, ' B ': 2, ' C ': 3} bdict = {} for keys in Adict.keys (): b    Dict[adict[keys]] = keys return bdictdef func29 (sstr,dstr,string,casemap=true): Assert Len (SSTR) >= len (DSTR) Table = Dict (Zip (sstr,dstr)) Print table if Len (SSTR) > Len (dstr): temp ={}.fromkeys (Sstr[len (DSTR))) table.update (temp) print table ls = [] for ch in string:if not CA                 Semap:if Ch.lower () in Table:ls.append (Table[ch.lower ())) Elif Ch.upper () in table:        Ls.append (Table[ch.upper ())) Else:ls.append (CH) continue If ch in table:ls.append (Table[ch]) else:ls.append (ch) ls = [ch to ch in ls if CH] p  Rint ls return "". Join (LS) def func30 (sstr): alist = [Chr (num +)% + ord (' a ')) for NUM in range [] blist  = [Chr (num + ord (' a ')) for Num in range (+)] table = Dict (Zip (blist,alist)) Astr = "". Join (alist). UPPER () BSTR =            "". Join (blist). Upper () table.update (dict (Zip (bstr,astr))) ls = [] for ch in sstr:if ch in table:  Ls.append (Table[ch]) else:ls.append (CH) return "". Join (LS) import randomdef func31 (): Alist = [Random.randint (i,10) For I in range (Ten)] blist = [Random.randint (i,10) for I in range] Aset = set (alist) bset = set (blist) print Aset | bset Print Aset & bset

The above is the Python Basic Learning Code of the image collection content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.