The PythonAdvance2 of Python learning

Source: Internet
Author: User

#!/usr/bin/python#coding=utf-8# Loop Design # range function s =  ' Abcdefghijk '                #a  b c d e f g h i j  Kfor i in range (0, len (S),  2): #0  1 2 3 4 5 6 7  8 9 10 11    print s[i]print len (S) #遍历数组S  =  ' Abcdefghijk ' for  (Index, char)  in enumerate (S):     print index     print char# multiple equal-length sequence ta = [1, 2, 3]tb = [9, 8,  7]tc = [' A ',  ' B ',  ' C ']for  (a, b, c)  in zip (TA, TB,  TC):     print (a, b, c) #分解聚合ta  = [1,2,3]tb = [9,8,7]#  clusterzipped = zip (TA,TB) print  (zipped) # decomposena, nb = zip (* zipped) Print (NA, NB) #循环对象f  =&Nbsp;open (' Test.txt ') F.next () F.next () F.next () F.next () F.next () F.next () F.next () For line in open (' Test.txt '):     print line# generator Def gen ():     a = 100     yield a    a = a*8    yield  a    yield 1000for i in gen ():     print  idef gen ():     for i in range (4):         yield iG =  (X for x in range (4)) #表推导L  = []for  x in range ():     l.append (x**2) print ll = [x**2 for  x in range (Ten)]print l# function object #lambda function func = lambda x,y: x +  Yprint func (3, 4) Def func (x, y):     return x + yprint  func (3, 4) #函数作Pass Def test (f, a, b):    print  ' test '     print for parameters  f (a, b) test (func, 3, 5) def u (a, b, c):    print  ' U '     print a (b, c) u (func, 5, 6) #map () function, the function of map () is to function objects in sequence to each element of the table re  = map (lambda x:x+3), [1, 2, 3, 5, 6]) Print rere = map (( Lambda x,y: x+y), [1,2,3],[6,7,9]) Print re#filter () function, if the function object returns True, the element is stored in the returned table def  Func (a):    if a > 100:         return true    else:        return  Falseprint filter (func, [10, 56, 101, 500]) #reduce () function, reduce can progressively function on each parameter print  reduce ((lambda x, y: x + y),  [1, 2, 3, 4,9]) #错误处理re  =  iter (Range (5)) try: &Nbsp;  for i in range (+):         print  re.next () except stopiteration:    print  ' here is end  ', iprint  ' Hahahaha ' Re = iter (range (5)) For i in range (+):     print re.next () print  ' Hahahaha ' Try:    print (a*2) Except TypeError:     print ("TypeError") except:    print ("not type error  & error noted ") Def test_func ():    try:         m = 1/0    except NameError:         print ("Catch nameerror in the sub-function") try:     test_func () except zerodivisionerror:    print ("catch error in  The main program ") print  ' Lalala ' raise stopiteration (' this is error ') print  ' hahaha ' #动态类型a  = 3a =   ' au ' print aa = 5b = aa = a + 2print aprint bl1  = [1, 2, 3]l2 = l1l1 = 1print l1, l2l1 =[1, 2,  3]l2 =l1l1[0] = 10print l2# parameter passing of a function from a dynamic type def f (x):     x  = 100    PRINT XA = 1F (a) print adef f (x):     x[2] = 100    print xa =[1, 2, 3]f (a) print  axl =[1, 3, 5]yl =[9, 12, 13]l = [x**2 for  (x, y)  in  zip (xl, yl)  if y > 10]print l# is passed through parameters to determine the number, string, list, tuple, Whether a data type such as a dictionary is a mutable data Object a = 2b =  ' Aha ' c = [1, 2, 3]d =  (1, 2,  3) e = {' Tom ':  11,  ' Sam ': 57,  ' Lily ':  100}def num (x):        #数字      x = 100    print xnum (a) print adef str (x):          #字符串     x =  ' ade '      print xstr (b) print bdef list (x):         #list     x[0] = 100    print xlist (c) print cdef  Tuple (x):        #tuple     x[0] = 100     print x    tuple (d)     raise stopiteration () print ddef dic (x):          #词典      x[' Tom '] = 100    print xdic (e) print e

This article from "Little Mo Growth Road" blog, declined reprint!

The PythonAdvance2 of Python learning

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.