![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228223523599-1317558551.jpg)
运算符
1、算数运算:
2、比较运算:
3、赋值运算:
4、逻辑运算:(从前到后,先算括号里的)
5、成员运算:
基本数据类型
1、数字
int(整型)
在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228222919015-1721865363.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228223234584-933598331.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228223617194-535553164.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228223256563-389283006.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228225013833-688547500.jpg)
ctrl+? 注释
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228225045053-1191710249.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228225111699-805295838.jpg)
改变字体大小
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228225155823-1006381975.jpg)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
2、布尔值
真或假
1 或 0
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228223645233-1016190926.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228222958324-1445607980.jpg)
3、字符串
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228224029783-965321417.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144436899-1057194773.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228224131003-795073116.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228224154428-1847046090.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144655178-1981788768.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144706722-248886741.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144754163-734409216.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144803473-1307084170.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144816563-2024961829.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144823487-1732703198.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144852343-1590032592.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144903007-1375950704.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144926273-1182906620.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144916433-1122719554.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144941283-1963236831.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229144948832-1892617472.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145002058-1417230921.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145009398-1413118666.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145028448-906009558.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145037722-1268302402.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145049725-705474637.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145101034-789923230.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145117923-801746432.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145124958-1451836952.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145139124-1860819707.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145146284-547823267.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145157403-1032176984.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145204735-1384490608.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145217443-310850953.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145224597-1272827139.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145238818-541162234.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145244024-1844766105.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145259563-944660013.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145307213-727886676.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145316943-822269058.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145335568-1014919276.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145341749-1202458553.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145323598-1110719669.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145401939-1025027717.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145411508-1958852588.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145423133-941111638.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145432944-2094031093.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145442750-1468622505.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145451544-2115141398.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145457348-486970463.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145508198-466950226.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145516698-816994986.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181229145527024-177094441.jpg)
"hello world"
字符串常用功能:4种表达 方式。
- 移除空白
- 分割
- 长度
- 索引
- 切片
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
4、列表
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228224517374-704817145.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181228224540188-200539242.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184848372-200108449.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184854847-1161037867.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184902114-177338196.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184908390-1565164919.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184914240-930591137.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184918873-1407616633.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184926558-1681853665.png)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184932362-762477515.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184938358-280045560.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230184948688-1981873073.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185025863-106733737.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185033224-545130036.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185039197-867666640.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185045887-1875533577.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185052193-749336923.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185057735-1872411459.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185104622-1142613621.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185111238-2056939262.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185118202-1880164464.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185125199-646572652.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185130937-1080517772.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185136870-1322569210.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185147750-459813731.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185154035-708774408.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185200396-861704492.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185208044-1893381505.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185214129-924383139.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185220671-533666289.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185226944-940164117.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185234053-1176825550.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185248901-38446945.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185256598-616244960.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185302389-1997324073.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185308290-1828984346.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185313981-995235840.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185319640-1690188850.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185325079-1296809592.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185330391-1445446862.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185339430-1807843809.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185345497-137873244.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185351072-42536958.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185356458-218059234.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185402552-1062875129.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185408400-322187255.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185414524-185675821.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230185420288-607468332.jpg)
创建列表:
1
2
3
|
name_list = [ 'alex' , 'seven' , 'eric' ] 或 name_list = list ([ 'alex' , 'seven' , 'eric' ]) |
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 切片
- 循环
- 包含
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
5、元祖
创建元祖:
1
2
3
|
ages = ( 11 , 22 , 33 , 44 , 55 ) 或 ages = tuple (( 11 , 22 , 33 , 44 , 55 )) |
基本操作:
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190253304-333489861.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190301013-898464103.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190307716-1186000058.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190314582-2069805098.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190319460-1269923846.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190325639-1981886561.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190335959-59979392.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190341175-1841448053.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190347172-971900633.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190354381-774782501.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190400125-1186754243.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230190406544-1661338502.jpg)
- 索引
- 切片
- 循环
- 长度
- 包含
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
6、字典(无序)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183229674-1114032230.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183250920-617695330.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183306191-1294180991.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183316466-848270189.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183401959-1935320212.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183408275-1645392740.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183413897-2000408338.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183421588-1502927928.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183427552-197201745.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183434793-1617958997.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183442033-535351834.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183448237-167833026.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183500917-1764020003.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183508072-40080275.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183514870-966467303.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183521178-1593445599.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183527659-528289757.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183534220-1511686256.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183541271-2014538044.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183550676-920814313.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183556835-36562801.jpg)
![](https://img2018.cnblogs.com/blog/1560722/201812/1560722-20181230183603701-323323346.jpg)
创建字典:
1
2
3
|
person = { "name" : "mr.wu" , 'age' : 18 } 或 person = dict ({ "name" : "mr.wu" , 'age' : 18 }) |
常用操作:
- 索引
- 新增
- 删除
- 键、值、键值对
- 循环
- 长度
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
PS:循环,range,continue 和 break
其他
1、for循环
用户按照顺序循环可迭代对象中的内容,
PS:break、continue
1
2
3
|
li = [ 11 , 22 , 33 , 44 ] for item in li: print item |
2、enumrate
为可迭代的对象添加序号
1
2
3
|
li = [ 11 , 22 , 33 ] for k,v in enumerate (li, 1 ): print (k,v) |
3、range和xrange
指定范围,生成指定的数字
1
2
3
4
5
6
7
8
|
print range ( 1 , 10 ) # 结果:[1, 2, 3, 4, 5, 6, 7, 8, 9] print range ( 1 , 10 , 2 ) # 结果:[1, 3, 5, 7, 9] print range ( 30 , 0 , - 2 ) # 结果:[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
- 显示商品列表,让用户根据序号选择商品,加入购物车
- 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
- 附加:可充值、某商品移除购物车
1
2
3
4
5
6
|
goods = [ { "name" : "电脑" , "price" : 1999 }, { "name" : "鼠标" , "price" : 10 }, { "name" : "游艇" , "price" : 20 }, { "name" : "美女" , "price" : 998 }, ] |
五、用户交互,显示省市县三级联动的选择
1
2
3
4
5
6
7
8
9
10
11
12
13
|
dic = { "河北" : { "石家庄" : [ "鹿泉" , "藁城" , "元氏" ], "邯郸" : [ "永年" , "涉县" , "磁县" ], } "河南" : { ... } "山西" : { ... } } |