Python开发全栈-DAY-IV:Python基本数据类型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python开发全栈-DAY-IV:Python基本数据类型相关的知识,希望对你有一定的参考价值。
运算符
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
int
2、布尔值
真或假
1 或 0
3、字符串
"hello world"
字符串常用功能:
- 移除空白
- 分割
- 长度
- 索引
- 切片
str
4、列表
创建列表:
1
2
3
|
name_list = [ ‘alex‘ , ‘seven‘ , ‘eric‘ ] 或 name_list = list ([ ‘alex‘ , ‘seven‘ , ‘eric‘ ]) |
基本操作:
- 索引
- 切片
- 追加
- 删除
- 长度
- 切片
- 循环
- 包含
list
5、元祖
创建元祖:
1
2
3
|
ages = ( 11 , 22 , 33 , 44 , 55 ) 或 ages = tuple (( 11 , 22 , 33 , 44 , 55 )) |
基本操作:
- 索引
- 切片
- 循环
- 长度
- 包含
lass tuple(object): """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable‘s items If the argument is a tuple, the return value is the same object. """ def count(self, value): # real signature unknown; restored from __doc__ """ T.count(value) -> integer -- return number of occurrences of value """ return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def __add__(self, y): # real signature unknown; restored from __doc__ """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__(‘name‘) <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __hash__(self): # real signature unknown; restored from __doc__ """ x.__hash__() <==> hash(x) """ pass def __init__(self, seq=()): # known special case of tuple.__init__ """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable‘s items If the argument is a tuple, the return value is the same object. # (copied from class doc) """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass def __mul__(self, n): # real signature unknown; restored from __doc__ """ x.__mul__(n) <==> x*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __rmul__(self, n): # real signature unknown; restored from __doc__ """ x.__rmul__(n) <==> n*x """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ T.__sizeof__() -- size of T in memory, in bytes """ pass
6、字典(无序)
创建字典:
1
2
3
|
person = { "name" : "mr.wu" , ‘age‘ : 18 } 或 person = dict ({ "name" : "mr.wu" , ‘age‘ : 18 }) |
常用操作:
- 索引
- 新增
- 删除
- 键、值、键值对
- 循环
- 长度
dict
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 = { "河北" : { "石家庄" : [ "鹿泉" , "藁城" , "元氏" ], "邯郸" : [ "永年" , "涉县" , "磁县" ], } "河南" : { ... } "山西" : { ... } } |
以上是关于Python开发全栈-DAY-IV:Python基本数据类型的主要内容,如果未能解决你的问题,请参考以下文章