简单入门

lists

所有对象(数字、字符串、布尔等等)的有序集合序列。

>>> a = []
>>> a.append(1)
>>> a.append("abc")
>>> a.append(False)
>>> a
[1, 'abc', False]

dictionaries

字典 键值对

>>> dictionaries = {}
>>> dictionaries['first'] = 'a'
>>> dictionaries[1] = 2
>>> dictionaries
{'first': 'a', 1: 2}

sets

集合,可以从list生成,做交运算 &

>>> b = [1,2,3.4,5]
>>> setB = set(b)
>>> setB
{1, 2, 5, 3.4}
>> c = [1,2,6,7]
>>> setC = set(c)
>>> setC
{1, 2, 6, 7}
>>> setD = setB - setC
>>> setD
{5, 3.4}

缩进的作用

不需要括号,用缩进表示这是代码块。

>>> if 3 < 4 :
...     print("hello world")
...     print(1+1)
...     
hello world
2

集合中的循环

>>> sA = set([8,7,6,5])
>>> for item in sA:
...     print(item)
...     
8
5
6
7

字典中的循环

>>> for item in dA:
...     print(item, dA[item])
...     
first a
1 2

第一个列表的2倍赋给另一个列表 列表表达式

>>> la = [1,1,2,3,5,8]
>>> lb = [item * 4 for item in la]
>>> lb
[4, 4, 8, 12, 20, 32]
>>> lc = []
>>> for item in la:
...     lc.append(item * 4)
...     
>>> lc
[4, 4, 8, 12, 20, 32]
>>> le = [item * 4 for item in la if item < 4]
>>> le
[4, 4, 8, 12]

你可能感兴趣的:(简单入门)