*#while循环 number=20 k=True while k: gusses=int(input()) if gusses > number: print("big") elif gusses < number: print("small") else: print("right") k=False else: print("you done") print("ok") #for循环 for i in range(1,10,2): print(i) else: print("the loop is over") #break 用法 while True: s=input("input a string: ") if s=="quit": break; print("the string", s ,"length is", len(s)) #continue的用法 while True: s=input("input a string: ") if s=="quit": break; if len(s)<3: continue print("Input is of sufficient length") #函数 def fun(s): print(s) s=input() fun(s) #局部变量 def func(x): print('x is', x) x = 2 print('Changed local x to', x) x = 50 func(x) print('x is still', x) #全局变量 def func(): global x #定义全局变量 print ('x is', x) x = 2 print ('Changed local x to', x) x = 50 func() print ('Value of x is', x ) #使用默认参数 def say(message, times = 1): print (message * times) say('Hello') say('World', 5) #使用关键参数,有如下两点优势 #由于我们不必担心参数的顺序,使用函数变得更加简单了 #假设其他参数都有默认值,我们可以只给我们想要的那些参数赋值 def func(a, b=5, c=10): print ('a is', a, ' and b is', b, 'and c is', c) func(3, 7) func(25, c=24) func(c=50, a=100) #return语句 def min(a,b): if a<b: return a else: return b a=int(input("a= ")) b=int(input("b= ")) print("min", min(a,b)) 模块 #模块 import sys #sys模块包含了与Python解释器和它的环境有关的函数 print ('The command line arguments are:') for i in sys.argv: print (i) print ('/n/nThe PYTHONPATH is', sys.path, '/n') #模块的__name__ if __name__ == '__main__': print ('This program is being run by itself') else: print ('I am being imported from another module') #dir()函数 ''' 你可以使用内建的dir函数来列出模块定义的标识符。标识符有函数、类和变量。 当你为dir()提供一个模块名的时候,它返回模块定义的名称列表。如果不提供参数,它返回当前模块中定义的名称列表。 关于del的一点注释——这个语句在运行后被用来 删除 一个变量/名称。在这个例子中,del a,你将无法再使用变量a——它就好像从来没有存在过一样。 ''' 高级数据结构:列表、元组、字典 #列表,是可以修改的 shoplist=['apple', 'bread', 'rice'] print(len(shoplist)) print(shoplist) shoplist.append('1') print(shoplist) print(shoplist[2]) for item in shoplist: print(item) del shoplist[3] #原组是不可以修改的,len函数可以用来获取元组的长度,可用索引访问元组中的数据 ##含有0个或1个项目的元组。一个空的元组由一对空的圆括号组成,如myempty = ()。 #然而,含有单个元素的元组就不那么简单了。你必须在第一个(唯一一个)项目后跟一个逗号, #这样Python才能区分元组和表达式中一个带圆括号的对象。即如果你想要的是一个包含项目2的元组的时候,你应该指明singleton = (2 , )。 zoo = ('wolf', 'elephant', 'penguin') print("Number of animals in the zoo is %d" % (len(zoo))) new_zoo = ('monkey', 'dolphin', zoo)#创建新的元组 print("Number of animals in the new zoo is ", len(new_zoo)) print('All animals in new zoo are', new_zoo) print('Animals brought from old zoo are', new_zoo[2]) print('Last animal brought from old zoo is', new_zoo[2][2]) #元组最通常的用法是用在打印语句中 age = 23 name = "wangyang" print("%s is %d year old!" % (name, age)) #******************************************* #字典:把键(名字)和值(详细情况)联系在一起。注意,键必须是唯一的 #注意,你只能使用不可变的对象(比如字符串)来作为字典的键,但是你可以不可变或可变的对象作为字典的值。基本说来就是,你应该只使用简单的对象作为键。 #键值对在字典中以这样的方式标记:d = {key1 : value1, key2 : value2}。注意它们的键/值对用冒号分割,而各个对用逗号分割,所有这些都包括在花括号中。 #记住字典中的键/值对是没有顺序的。如果你想要一个特定的顺序,那么你应该在使用前自己对它们排序。 字典是dict类的实例/对象。 addressbook = {'wangyang' : 'shanghai', 'zhangfei' : 'beijing', 'libai' : 'zhejiang'} print("wangyang's address is" ,addressbook['wangyang']) # Adding a key/value pair addressbook['caocao']='usa' # Deleting a key/value pair del addressbook['wangyang'] print('/nThere are %d contacts in the addressbook/n' % len(addressbook)) for name , address in addressbook.items(): print("%s's address is %s" % (name, address)) if 'libai' in addressbook: print("libai's address is", addressbook['libai']) #**************************** #序列:列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。 #索引操作符让我们可以从序列中抓取一个特定项目。切片操作符让我们能够获取序列的一个切片,即一部分序列。 shoplist = ['apple', 'mango', 'carrot', 'banana'] # Indexing or 'Subscription' operation print ('Item 0 is', shoplist[0]) print ('Item 1 is', shoplist[1]) print ('Item 2 is', shoplist[2]) print ('Item 3 is', shoplist[3]) print ('Item -1 is', shoplist[-1]) print ('Item -2 is', shoplist[-2]) # Slicing on a list print ('Item 1 to 3 is', shoplist[1:3]) print ('Item 2 to end is', shoplist[2:]) print ('Item 1 to -1 is', shoplist[1:-1]) print ('Item start to end is', shoplist[:]) # Slicing on a string name = 'swaroop' print ('characters 1 to 3 is', name[1:3]) print ('characters 2 to end is', name[2:]) print ('characters 1 to -1 is', name[1:-1]) print ('characters start to end is', name[:]) ''' 切片操作符是序列名后跟一个方括号,方括号中有一对可选的数字,并用冒号分割。注意这与你使用的索引操作符十分相似。记住数是可选的,而冒号是必须的。 切片操作符中的第一个数(冒号之前)表示切片开始的位置,第二个数(冒号之后)表示切片到哪里结束。如果不指定第一个数,Python就从序列首开始。如果没有指定第二个数,则Python会停止在序列尾。注意,返回的序列从开始位置 开始 ,刚好在 结束 位置之前结束。即开始位置是包含在序列切片中的,而结束位置被排斥在切片外。 这样,shoplist[1:3]返回从位置1开始,包括位置2,但是停止在位置3的一个序列切片,因此返回一个含有两个项目的切片。类似地,shoplist[:]返回整个序列的拷贝。 你可以用负数做切片。负数用在从序列尾开始计算的位置。例如,shoplist[:-1]会返回除了最后一个项目外包含所有项目的序列切片。 ''' #****************** #你创建一个对象并给它赋一个变量的时候,这个变量仅仅 参考 那个对象,而不是表示这个对象本身!也就是说,变量名指向你计算机中存储那个对象的内存。这被称作名称到对象的绑定。 #一般说来,你不需要担心这个,只是在参考上有些细微的效果需要你注意。这会通过下面这个例子加以说明。 print ('Simple Assignment') shoplist = ['apple', 'mango', 'carrot', 'banana'] mylist = shoplist # mylist is just another name pointing to the same object! del shoplist[0] print ('shoplist is', shoplist) print ('mylist is', mylist) # notice that both shoplist and mylist both print the same list without # the 'apple' confirming that they point to the same object print ('Copy by making a full slice') mylist = shoplist[:] # make a copy by doing a full slice del mylist[0] # remove first item print ('shoplist is', shoplist) print ('mylist is', mylist) # notice that now the two lists are different #记住列表的赋值语句不创建拷贝。你得使用切片操作符来建立序列的拷贝。 #************ #字符串也是对象,同样具有方法。这些方法可以完成包括检验一部分字符串和去除空格在内的各种工作。 #你在程序中使用的字符串都是str类的对象 name = 'Swaroop' # This is a string object if name.startswith('Swa'): print ('Yes, the string starts with "Swa"') if 'a' in name: print ('Yes, it contains the string "a"') if name.find('war') != -1: print ('Yes, it contains the string "war"') delimiter = '_*_' mylist = ['Brazil', 'Russia', 'India', 'China'] print (delimiter.join(mylist))