python day2:python 初识(二)
2016-04-30 15:22
519 查看
大纲:
一、运算符
1.算数运算符
notice: 除法运算在python2.7和python3.x 的不同
2.比较运算符
3.赋值运算符
4.逻辑运算符
5.成员运算符
二、基本数据类型和方法介绍
1.int
2.str
3.boolean
4.列表
5.元组
6.字典
三、其他
for
enumerate
range,xrange
四、练习
notice: / 在python2.7下默认为 取整除 ,在做浮点运算的时候可以得出浮点值。如果不做浮点运算也想得到精确值,需要导入__future__模块的division方法
python3.x下默认就是精确运算,得到的值是浮点数
2.比较运算符
3.赋值运算
二、基本数据类型和方法介绍
1.int
dict
三、其他
1.for
for i in iteartor:
expr
2.enumerate
为可迭代对象添加序号:
li = [11,22,33]
for item,key in li:
print(item,key)
3.range,xrange
四、练习
一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
五、用户交互,显示省市县三级联动的选择
一、运算符
1.算数运算符
notice: 除法运算在python2.7和python3.x 的不同
2.比较运算符
3.赋值运算符
4.逻辑运算符
5.成员运算符
二、基本数据类型和方法介绍
1.int
2.str
3.boolean
4.列表
5.元组
6.字典
三、其他
for
enumerate
range,xrange
四、练习
一、运算符
1.算数运算符运算符 | 描述 | 示例 |
+ | 加法 | a + b = 30 |
- | 减法 | a - b = -10 |
* | 乘法 | a * b = 200 |
/ | 除法 | b / a = 2 |
% | 取模(取余数) | b % a = 0 |
** | 指数(幂运算) | a**b = 10 的幂 20 |
// | 取整除 | 9//2 = 4 而 9.0//2.0 = 4.0 |
>>> 9/2 4 >>> 9.0/2 4.5 >>> from __future__ import division >>> 9/2 4.5 >>>
python3.x下默认就是精确运算,得到的值是浮点数
>>> 9/2 4.5 >>>
2.比较运算符
运算符 | 描述 | 示例 |
== | 检查,两个操作数的值是否相等,如果是则条件变为真。 | (a == b) 不为 true. |
!= | 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 | (a != b) 为 true. |
<> | 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 | (a <> b) 为 true。这个类似于 != 运算符 |
> | 检查左操作数的值是否大于右操作数的值,如果是,则条件成立。 | (a > b) 不为 true. |
< | 检查左操作数的值是否小于右操作数的值,如果是,则条件成立。 | (a < b) 为 true. |
>= | 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件成立。 | (a >= b) 不为 true. |
<= | 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件成立。 | (a <= b) 为 true. |
运算符 | 描述 | 示例 |
= | 简单赋值运算,将 = 号左边的值或变量对应的值赋给右边的变量 | a = 2 b = 3 c = b (得c=3) |
+= | 加法赋值运算 | a+=3 等价于 a = a + 3 |
-= | 减法赋值运算 | a -= 3 等价于 a = a - 3 |
*= | 乘法赋值运算 | a *= 3 等价于 a = a * 3 |
/= | 除法赋值运算 | a /= 3 等价于 a = a / 3 |
%= | 取余赋值运算 | a %= 3 等价于 a = a % 3 |
//= | 取整赋值运算 | a //= 3 等价于 a = a // 3 |
1.int
class dict(object): """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) """ def clear(self): # real signature unknown; restored from __doc__ """ 清除内容 """ """ D.clear() -> None. Remove all items from D. """ pass def copy(self): # real signature unknown; restored from __doc__ """ 浅拷贝 """ """ D.copy() -> a shallow copy of D """ pass @staticmethod # known case def fromkeys(S, v=None): # real signature unknown; restored from __doc__ """ dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v. v defaults to None. """ pass def get(self, k, d=None): # real signature unknown; restored from __doc__ """ 根据key获取值,d是默认值 """ """ D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. """ pass def has_key(self, k): # real signature unknown; restored from __doc__ """ 是否有key """ """ D.has_key(k) -> True if D has a key k, else False """ return False def items(self): # real signature unknown; restored from __doc__ """ 所有项的列表形式 """ """ D.items() -> list of D's (key, value) pairs, as 2-tuples """ return [] def iteritems(self): # real signature unknown; restored from __doc__ """ 项可迭代 """ """ D.iteritems() -> an iterator over the (key, value) items of D """ pass def iterkeys(self): # real signature unknown; restored from __doc__ """ key可迭代 """ """ D.iterkeys() -> an iterator over the keys of D """ pass def itervalues(self): # real signature unknown; restored from __doc__ """ value可迭代 """ """ D.itervalues() -> an iterator over the values of D """ pass def keys(self): # real signature unknown; restored from __doc__ """ 所有的key列表 """ """ D.keys() -> list of D's keys """ return [] def pop(self, k, d=None): # real signature unknown; restored from __doc__ """ 获取并在字典中移除 """ """ D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised """ pass def popitem(self): # real signature unknown; restored from __doc__ """ 获取并在字典中移除 """ """ D.popitem() -> (k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if D is empty. """ pass def setdefault(self, k, d=None): # real signature unknown; restored from __doc__ """ 如果key不存在,则创建,如果存在,则返回已存在的值且不修改 """ """ D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D """ pass def update(self, E=None, **F): # known special case of dict.update """ 更新 {'name':'alex', 'age': 18000} [('name','sbsbsb'),] """ """ D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k] """ pass def values(self): # real signature unknown; restored from __doc__ """ 所有的值 """ """ D.values() -> list of D's values """ return [] def viewitems(self): # real signature unknown; restored from __doc__ """ 所有项,只是将内容保存至view对象中 """ """ D.viewitems() -> a set-like object providing a view on D's items """ pass def viewkeys(self): # real signature unknown; restored from __doc__ """ D.viewkeys() -> a set-like object providing a view on D's keys """ pass def viewvalues(self): # real signature unknown; restored from __doc__ """ D.viewvalues() -> an object providing a view on D's values """ pass def __cmp__(self, y): # real signature unknown; restored from __doc__ """ x.__cmp__(y) <==> cmp(x,y) """ pass def __contains__(self, k): # real signature unknown; restored from __doc__ """ D.__contains__(k) -> True if D has a key k, else False """ return False def __delitem__(self, y): # real signature unknown; restored from __doc__ """ x.__delitem__(y) <==> del x[y] """ 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 __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 __init__(self, seq=None, **kwargs): # known special case of dict.__init__ """ dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) # (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 @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 __setitem__(self, i, y): # real signature unknown; restored from __doc__ """ x.__setitem__(i, y) <==> x[i]=y """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ D.__sizeof__() -> size of D in memory, in bytes """ pass __hash__ = None
dict
三、其他
1.for
for i in iteartor:
expr
2.enumerate
为可迭代对象添加序号:
li = [11,22,33]
for item,key in li:
print(item,key)
3.range,xrange
四、练习
一、元素分类
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。
即: {'k1': 大于66的所有值, 'k2': 小于66的所有值}
li = [11,22,33,44,55,66,77,88,99] value = [] value1 = [] for i in li: if i <= 66: value.append(i) else : value1.append(i) print({'k1':value,'k2':value1})
asset = int(input("please input asset:")) #总资产 have = [] #直接购买列表 cart = [] #购物车 price = [] #价格列表 goods = [{"name":"电脑","price":1999},{"name":"鼠标","price":10},{"name":"游艇","price":20},{"name":"美女","price":998}] for key,item in enumerate(goods): #打印商品列表 print(key,item) while True: goods_num = input("请输入商品序号,或输入 q 退出:") #选择商品,或退出购买 if goods_num == "q": break de = int(input("直接购买请输入 1,加入购物车请输入 2,重选请输入 3:")) #选择购买方式 goods_num = int(goods_num) if de == 3: continue elif de == 1: asset -= goods[goods_num]["price"] print("您已购买 %s,花费 %d RMB" %(goods[goods_num]["name"],goods[goods_num]["price"]) ) have.append(goods[goods_num]["name"]) elif de == 2: print("商品 %s 已加入购物车,价格 %d RMB" % (goods[goods_num]["name"], goods[goods_num]["price"])) cart.append(goods[goods_num]["name"]) price.append(goods[goods_num]["price"]) elif de == ("q","Q"): break count = 0 if len(have) == 0 and len(cart) == 0: pass else: print("购物车中的商品:%s" % cart) con = input("您是否购买购物车中的商品?yes or no:") for i in price: count += i if con == "yes": if asset < count: print("您一共需要花费%d" % count) chongzhi = int(input("钱不够,请充值:")) asset += chongzhi print("您已购买:%s ,剩余资产:%d" % (cart,asset-count)) asset -= count else: print("您已购买:%s ,剩余资产:%d" % (cart, asset - count)) asset -= count else: pass
五、用户交互,显示省市县三级联动的选择
dic = { "河北": { "石家庄": ["鹿泉", "藁城", "元氏"], "邯郸": ["永年", "涉县", "磁县"], }, "河南": { "郑州":["中牟县","新密市","登封市"], "开封":["龙亭区","顺河区"] }, "山西": { "太原":["清徐县","职曲县"], "大同":["大同县","新荣区"] } } def get_item(value): #获取省或市下的子级 for i in value: print(i) print("省列表:") for i in dic: #打印省名称 print(i) status = None while status != "q": sheng = input("请输入想查看的省名称:") get_item(dic.get(sheng)) #调用函数获取省下的子级 shi = input("请输入想要查看的市名称:") get_item(dic.get(sheng).get(shi)) #调用函数获取市下的子级 status = input("退出请输入q,继续请按其他任意键:") #退出或继续
相关文章推荐
- python学习之旅之python简单命令
- python range()和xrange()
- windows系统下Python3.5.1安装教程
- Python 深浅拷贝
- 用python实现socket通信
- Python collections类
- Python2.7.11安装matplotlib,numpy,scikit-learn等其他包,w7x86x64下无问题
- python检测字符串是否只由数字组成
- python中list的赋值处理
- theano, sklearn--- win10 64 bit 配置anaconda(Python2.7)
- [python]Python Fabric - No hosts found. Please specify (single) host string for connection
- python条件语句嵌套
- Python并行处理——多线程
- IronPython 设置包路径
- EM算法--python代码和注意事项
- python_正则表达式(一)基础标识符
- Python数据存储
- Python变量及数据类型
- python本地文件
- python 导入自定义模块及使用