A sequence of Python-based learning code

Source: Internet
Author: User
str1 = ' abced ' for I in range ( -1,-len (STR1),-1) + [None]: print str1[:i]s, t = ' abc ', ' Def ' print zip (S, t) for I, T in    Enumerate (STR1): Print I, Tprint isinstance (' foo ', str) import Stringdef checkid (): Alphas = string.letters + ' _ ' Nums = string.digits check = raw_input (' Input ID for check: ') if Len (check) > 1:if check[0] not in Alphas                    : print "Invaid id" else:for o in check[1:]: If O No in Alphas + nums: print "Invaid id" break else:print ("valid ID") def func  1 (): Alist = ["xx3", "1TC"] for I, S in Enumerate (alist): Print I, Sdef func2 (): alist = ["Hello", "Come",    "["] blist = ["Welcome", "what", "a" for I, s in Zip (alist,blist): Print I, Sdef func3 (): Alist = [] Anum = raw_input (' input>> '). Strip () for I in Anum:alist.append (i) Alist.sort () Alist.reverse () r Eturn alistdef Func4 ():   Alist = [] While true:num = Int (raw_input (' Input >> '). Strip ()) if num = = 0:break Alist.append (num) alist.sort () return alistimport keyworddef Func5 (): Alphas = string.letters Nums = Stri    Ng.digits print ' The ID check! '    str1 = raw_input (' Input ID: ') if str1 in Keyword.kwlist:print ' Invalid,it is a keyword! '        Else:if str1[0] Not in Alphas + ' _ ': print ' invalid,first symbol must be alpha or underline! ' Else:for C in str1[1:]: If C isn't in Alphas + nums:print ' Invalid,symbol mus            T be Alpha or numbers! ' print ' Valid id! '        Def showstr (): STRs = raw_input (' Input strings:: ') If Len (strs) = = 0:return False elif len (strs) = = 1: Print STRs return True for I, J in Enumerate (STRs): if i = = 0 and len (strs)! = 1:print J , strs[i+1] elif i! = 0 and I = = (len (strs)-1): Print strS[I-1] Else:print Strs[i-1], J, Strs[i+1] return truedef cmpstr (): str1 = raw_input (' Input string 1> ') str2 = raw_input (' input string2> ') If Len (str1)! = Len (str2): Return False for I, J in Enumerat E (str1): If Ord (j)-Ord (str2[i])! = 0:return False return truedef func6 (): str1 = Raw_input (' INP UT your strings:>> ') i = 0 j = Len (str1)-1 while str1[i] = = ": i = i + 1 while str1[j] = =" : j = j-1 str1 = str1[i:j+1] Print str1def func7 (): Alist = ["Zero", "one", "one", "three", "four", "FIV        E "," Six "," seven "," eight "," nine "] nlist =" while True:number = raw_input (' Input a number (0~1000): ')                If len (number) = = 0:break Else:number = str (number) for I in number: Nlist + = Alist[int (i)] + '-' return Nlist[0:-1]def Func8 (): minutes = Int (raw_input (' Input minutes: ') )) Minu = minutes% 60 hour = minutes/60 print "%d:%d"% (hour, minu) def func9 (): str1 = raw_input (' Input string:>> ') retur N Str1.swapcase () def func10 (): mystr = Raw_input (' Enter a number: ') number = Int (mystr) alist = range (1, number+1 print ' Before: ', ' alist ' i = 0 while I < Len (alist): If number% alist[i] = = 0:del Alist[i ] I = i + 1 print ' after ', Alistdef findchr (str1, char): Lchar = Len (char) if char not in Str1:retu    Rn-1 for I, J in Enumerate (STR1): If J in char:if str1[i:i+lchar] = = Char:return I     Return-1def RFINDCHR (str1, char): Lchar = Len (char) lstring = Len (str1) if char not in str1:return-1                For I, J in Enumerate (STR1): If J in char:if str1[lstring-1-i:lstring-1-i+lchar] = = Char: return i return-1def SUBCHR (string1, Origchar, Newchar): Import string return String.Replace (string1, orig Char, Newchar) def ATOC (sTrparm): CIndex = Strparm.rfind ('-') if cindex <= 0:cindex = strparm.rfind (' + ') if CIndex > 0:  Real = float (Strparm[0:cindex]) compl = float (strparm[cindex:-1]) return complex (real, compl) import Randomdef FUNC11 (): alist = ["paper", "Shears", "stone"] g = Int (raw_input (' Input 1:paper,2:shears,3:stone:> '))-1 PRI NT "Your come%s"% alist[g] r = Random.choice ([0, 1, 2]) print "Him come%s"% alist[r] if g = = R:print    ' Nobody win! '    # R Win if (R > G and G-r! =-2) or r-g = = -2:print ' him win! ' # G win Else:print ' I win! '         Import Datetimedef isleapyear (year): if (year% 4 = = 0) and (year% = 0) or (year% 4 = = 0) and (year% 100 = = 0): return True Else:return falsedef func12 (): month = {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10: 31,11:30,12:31} while true:sdate = Raw_input (' input start date[dd/mm/yyyy]: '). Split ('/') dd, mm, YYYY = 0, 1, 2        SDATE[DD],SDATE[MM],SDATE[YYYY] =int (sdate[0]), int (sdate[1]), int (sdate[2]) if SDATE[MM] > or sdate[mm ] < 1:continue if Isleapyear (sdate[yyyy]): month[2] = if SDATE[DD] < 1 or SD ATE[DD] > MONTH[SDATE[MM]]: Continue break while true:edate = raw_input (' Input end DATE[DD /MM/YYYY]: Split ('/') edate[dd],edate[mm],edate[yyyy] =int (edate[0]), int (edate[1]), int (edate[2]) if edate[  MM] > or edate[mm] < 1:continue if Isleapyear (sdate[yyyy]): month[2] = if EDATE[DD] < 1 or EDATE[DD] > month[edate[mm]: Continue break

The above is the Python Basic Learning Code sequence 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.