A, python operator
- Arithmetic operators:
2. Comparison operators
3. assignment operator:
4. Logical Operators:
The Operator members:
Second, the basic data types
-
int (integer)
on a 32-bit machine, an integer number of bits is 32 bits, in the range of -2 31 to 2 31 - 1 ~ -2147483648 2147483647 i.e.
on the 64-bit system, the number of bits is 64-bit integer , in the range of -2 63 to 2 63-1 ~ 9223372036854775807 i.e. -9223372036854775808 -
Boolean value boolean
true or false
1 or 0 -
String 'str'
"hello world"
String common functions:
removing a blank
segmentation
length
index
sections
- List (list)
name_list = ['alex', 'seven', 'eric']
或
name_list = list(['alex', 'seven', 'eric'])
Basic operation:
Index
slice
an additional
deletion
length
slice
loop
contains
- Ganso (tuple)
创建Ganso:
ages = (11, 22, 33, 44, 55)
或
ages = tuple((11, 22, 33, 44, 55))
Basic operation:
index
slicing
cycle
length
comprising
- Dictionary (disorder) (dict) PS: circulation, range, continue and break
create a dictionary:
person = {"name": "mr.wu", 'age': 18}
或
person = dict({"name": "mr.wu", 'age': 18})
Common operations:
Index
added
delete
key, value, key to
cycle
length
- Other block
1) for loop: loop order users according to the content object may be iterative,
the PS: BREAK, Continue
a = [11,22,33,44]
for item in a:
print item
2) enumrate: adding the number of iterations objects
a= [11,22,33]
for k,v in enumerate(a, 1):
print(k,v)```
3)range和xrange:指定范围,生成指定的数字
(1, 10) Print Range
# Results: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Range Print (1, 10, 2)
# Results: [1, 3, 5, 7, 9]
Range Print (30, 0, -2)
# Results: [30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]
**课后练习:**
一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
二、查找
查找列表中元素,移除每个元素的空格,并查找以 a或A开头 并且以 c 结尾的所有元素。
li = ["alec", " aric", "Alex", "Tony", "rain"]
tu = ("alec", " aric", "Alex", "Tony", "rain")
dic = {'k1': "alex", 'k2': ' aric', "k3": "Alex", "k4": "Tony"}
三、输出商品列表,用户输入序号,显示用户选中的商品
商品 li = ["手机", "电脑", '鼠标垫', '游艇']
四、购物车
功能要求:
要求用户输入总资产,例如:2000
显示商品列表,让用户根据序号选择商品,加入购物车
购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
附加:可充值、某商品移除购物车
Goods = [
{ "name": "computer", ". price": 1999},
{ "name": "mouse", ". price": 10},
{ "name": "yacht", "price": 20} ,
{ "name": "beauty", ". price": 998},
]
五、用户交互,显示省市县三级联动的选择
dic = {
"河北": {
"石家庄": ["鹿泉", "藁城", "元氏"],
"邯郸": ["永年", "涉县", "磁县"],
}
"河南": {
...
}
"山西": {
...
}
}