and or not
全局变量 在函数中可读不可改 局部变量只能在函数中读改
random.randint(1,10)
list: ['apple','orange'] +['Alice']
in list[] : return true or false
del list[0] :del is a statement, not a function or an operator. It does not evaluateto any return value.
string.upper()/lower()/split()默认是空格为分隔符
list.reverse()/append()
wordIndex = random.randint(0, len(wordList) - 1)
The list()function is very similar to thestr()orint()functions. It just convertsthe object it is passed into a list. It's very easy to generate huge lists with therange()function. Try typing inlist(range(10000))into the shell:表达方式 []
To make this change, we will introduce a new data type called adictionary.Adictionary is a collection of other values much like a list, but instead of accessing the itemsin the dictionary with an integer index, you access them with an index of any data type (butmost often strings). 表达方式{:}
myDict={'a':'test a','b':'test b','c':'test c'}
for indexin myDict:
print('===============')
print(index)
print(myDict[index])
print('===============')
****Dictionaries are different from lists because they are unordered.
The choice()functionhas one parameter, a list.
random.randint(0, 9)
random.choice(list(range(0, 10)))
multiple assignment
a, b, c = ['apples', 'cats', 42]
def approximate_size(size,a_kilobyte_is_1024_bytes=True):
通过使用sys.path.insert(0,new_path),你可以插入一个新的目录到sys.path列表的第一项,从而使其出现在Python搜索路径的开头。
当你想使用在导入的模块中定义的函数的时候,你需要包含模块的名称。因此你不能仅仅指明approximate_size,它必须是humansize.approximate_size才行。
raise语句来抛出异常。Java和C++使用try...catch块来处理异常,使用throw语句来抛出异常。
Python使用try...except块来处理异常,使用
你不需要在抛出异常的函数里面去处
理它。如果一个函数没有处理它,这个异常会被传递到它的调用函数,然后那个函数的调用函数,等等“在这个堆栈上面。”如果这个异常从来没有被处理,你的程序将会崩溃,Python将会打印一个“traceback”的标准错误信息,并以此结束。这也可能正是你想要的,它取决于你的程序具体做什么。
Booleans[布尔型]或为True[真]或为False[假]。
Numbers[数值型]可以是Integers[整数](1和2)、Floats[浮点数](1.1和1.2)、Fractions[分数](1/2和2/3);甚至是Complex Number[复数]。
Strings[字符串型]是Unicode字符序列,例如: 一份HTML文档。
Bytes[字节]和Byte Arrays[字节数组],例如: 一份JPEG图像文件。
Lists[列表]是值的有序序列。
Tuples[元组]是有序而不可变的值序列。
Sets[集合]是装满无序值的包裹。
Dictionaries[字典]是键值对的无序包裹。
a_list.extend(['d', 'e', 'f'])
['a', 'b', 'c', 'd', 'e', 'f']
a_list.append(['g', 'h', 'i'])
['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h', 'i']]
a_list.count('new')
a_list.index('mpilgrim')
果在列表中没有找到该值,index()方法将会引发一个例外。
del a_list[1]a_list.remove('new')
如果不带参数调用,pop()列表方法将删除列表中最后的元素,并返回所删除的值。
元组:
元组的速度比列表更快。如果定义了一系列常量值,而所需做的仅是对它进行遍历,那么请使用元组替代列表。
元组可转换成列表反之亦然。内建的tuple()函数接受一个列表参数,并返回一个包含同样元素的元组,而list()函数接受一个元组参数并返回一个列表。从效果上看,tuple()冻结列表,而list()融化元组。
元组和列表在布尔类型上下文环境中,空元组为假值。
内建的range()函数构造了一个整数序列。(从技术上来说,range()函数返回的既不是列表也不是元组,而是一个迭代器,但稍后您将学到它们的区别。)
集合:
a_set = {1}
a_set = set()
a_set.add(4)
a_set.update({2, 4, 6})
{1, 2, 3, 4, 6}
>>>a_set.update({3, 6, 9}, {1, 2, 3, 5, 8, 13})
>>> a_set {1, 2, 3, 4, 5, 6, 8, 9, 13}
区别在这里:如果该值不在集合中,remove() 方法引发一个KeyError例外。
pop() 方法从集合中删除某个值,并返回该值。并没有“最后一个”值的概念,因此无法控制删除的是哪一个值。它基本上是随机的。
clear() 方法删除集合中 所有 的值,留下一个空集合。试图从空集合中弹出某值将会引发KeyError例外。
a_set.union(b_set)
a_set.difference(b_set)
a_set.symmetric_difference(b_set)
union()方法返回一个新集合,其中装着 在两个 集合中出现的元素。
intersection()方法返回一个新集合,其中装着 同时 在两个集合中出现的所有元素。
difference()方法返回的新集合中,装着所有在a_set 出现但未在b_set 中的元素。
symmetric_difference()方法返回一个新集合,其中装着所有 只在其中一个 集合中出现的元素。