Python开发

python简易教程,python教程哪个好

DocStrings该文档字符串所约定的是一串多行字符串,其中第一行以某一大写字母开始,以句号结束。第二行为空行,后跟的第三行开始是任何详细的解释说明。在此强烈建议你在有关你所有非凡功能的文档字符串中都遵循这一约定。我们可以通过使用函数的 __doc__(注意其中的双下划綫)属性(属于函数的名称)来获取函数 print_max 的文档字符串属性。只消记住 Python 将所有东西都视为一个对象,这其中自然包括函数。我们将在后面的类(Class)章节讨论有关对象的更多细节。+如果你曾使用过 Python 的 help() 函数,那么你应该已经了解了文档字符串的用途了。它所做的便是获取函数的 __doc__ 属性并以一种整洁的方式将其呈现给你。你可以在上方的函数中尝试一下——只需在程序中包含 help(print_max) 就行了。要记住你可以通过按下 q 键来退出 help。自动化工具可以以这种方式检索你的程序中的文档。因此,我强烈推荐你为你编写的所有不平凡的函数配以文档字符串。你的 Python 发行版中附带的 pydoc 命令与 help() 使用文档字符串的方式类似。print_max(3, 5)print(print_max.__doc__)

from math import sqrtprint(“Square root of 16 is”, sqrt(16))因此,我们大都推荐最好去使用 import 语句,尽管这会使你的程序变得稍微长一些。你还可以使用:from mymodule import *这将导入诸如 say_hi 等所有公共名称,但不会导入 __version__ 名称,因为后者以双下划线开头。

dir 函数内置的 dir() 函数能够返回由对象所定义的名称列表。 如果这一对象是一个模块,则该列表会包括函数内所定义的函数、类与变量。dir(sys)

shoplist = [‘apple’, ‘mango’, ‘carrot’, ‘banana’]print(‘I have’, len(shoplist), ‘items to purchase.’)print(‘These items are:’, end=’ ‘)for item in shoplist:print(item, end=’ ‘)print(‘\nI also have to buy rice.’)shoplist.append(‘rice’)print(‘My shopping list is now’, shoplist)print(‘I will sort my list now’)shoplist.sort()print(‘Sorted shopping list is’, shoplist)print(‘The first item I will buy is’, shoplist[0])olditem = shoplist[0]del shoplist[0]print(‘I bought the’, olditem)print(‘My shopping list is now’, shoplist)

zoo = (‘python’, ‘elephant’, ‘penguin’)print(‘Number of animals in the zoo is’, len(zoo))new_zoo = ‘monkey’, ‘camel’, zooprint(‘Number of cages 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])print(‘Number of animals in the new zoo is’,len(new_zoo)-1+len(new_zoo[2]))

shoplist = [‘apple’, ‘mango’, ‘carrot’, ‘banana’]name = ‘swaroop’# Indexing or ‘Subscription’ operation ## 索引或“下标(Subcription)”操作符 #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])print(‘Character 0 is’, name[0])# 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[:])# 从某一字符串中切片 #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 ds_seq.pyItem 0 is appleItem 1 is mangoItem 2 is carrotItem 3 is bananaItem -1 is bananaItem -2 is carrotCharacter 0 is sItem 1 to 3 is [‘mango’, ‘carrot’]Item 2 to end is [‘carrot’, ‘banana’]Item 1 to -1 is [‘mango’, ‘carrot’]Item start to end is [‘apple’, ‘mango’, ‘carrot’, ‘banana’]characters 1 to 3 is wacharacters 2 to end is aroopcharacters 1 to -1 is waroocharacters start to end is swaroop

>>> bri = set([‘brazil’, ‘russia’, ‘india’])>>> ‘india’ in briTrue>>> ‘usa’ in briFalse>>> bric = bri.copy()>>> bric.add(‘china’)>>> bric.issuperset(bri)True>>> bri.remove(‘russia’)>>> bri & bric # OR bri.intersection(bric){‘brazil’, ‘india’}

print(‘Simple Assignment’)shoplist = [‘apple’, ‘mango’, ‘carrot’, ‘banana’]# mylist 只是指向同一对象的另一种名称mylist = shoplist# 我购买了第一项项目,所以我将其从列表中删除del shoplist[0]print(‘shoplist is’, shoplist)print(‘mylist is’, mylist)# 注意到 shoplist 和 mylist 二者都# 打印出了其中都没有 apple 的同样的列表,以此我们确认# 它们指向的是同一个对象print(‘Copy by making a full slice’)# 通过生成一份完整的切片制作一份列表的副本mylist = sho

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注