公共方法

公共方法

运算符

 +

>>> "hello " + "itcast"
'hello itcast'
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
>>> ('a', 'b') + ('c', 'd')
('a', 'b', 'c', 'd')

*

>>> 'ab' * 4
'ababab'
>>> [1, 2] * 4
[1, 2, 1, 2, 1, 2, 1, 2]
>>> ('a', 'b') * 4
('a', 'b', 'a', 'b', 'a', 'b', 'a', 'b')

in

>>> 'itc' in 'hello itcast'
True
>>> 3 in [1, 2]
False
>>> 4 in (1, 2, 3, 4)
True
>>> "name" in {"name":"Delron", "age":24}
True

注意,in在对字典操作时,判断的是字典的键

python内置函数

Python包含了以下内置函数

 len

>>> len("hello itcast")
12
>>> len([1, 2, 3, 4])
4
>>> len((3,4))
2
>>> len({"a":1, "b":2})
2

注意:len在操作字典数据时,返回的是键值对个数。

max

>>> max("hello itcast")
't'
>>> max([1,4,522,3,4])
522
>>> max({"a":1, "b":2})
'b'
>>> max({"a":10, "b":2})
'b'
>>> max({"c":10, "b":2})
'c'

del

del有两种用法,一种是del加空格,另一种是del()

>>> a = 1
>>> a
1
>>> del a
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> a = ['a', 'b']
>>> del a[0]
>>> a
['b']
>>> del(a)
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

多维列表/元祖访问的示例

>>> tuple1 = [(2,3),(4,5)]
>>> tuple1[0]
(2, 3)
>>> tuple1[0][0]
2
>>> tuple1[0][2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range


>>> tuple1[0][1]
3
>>> tuple1[2][2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range


>>> tuple2 = tuple1+[(3)]
>>> tuple2
[(2, 3), (4, 5), 3]
>>> tuple2[2]
3
>>> tuple2[2][0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable

例子1:

# +
my_list1 = [1, 2]
my_list2 = [3, 4]
ret1 = my_list2 + my_list1
print(ret1)

运行结果:

[3, 4, 1, 2]

例子2:

# *
my_list = ["hello"] * 3
print(my_list)

运行结果:

['hello', 'hello', 'hello']

例子3:

for i in range(1, 6):
    print("*"*i)

运行结果:

*
**
***
****
*****

例子4:

# in not in 支持集合
my_set = {1, 3, 5}
flag = 3 in my_set
print(flag)

运行结果:

True

例子5:

# max(item)
my_list = [10, 100, 1, 3, 5]
print(max(my_list))

# min(item)
print(min(my_list))

运行结果:

100
1

例子6:

# del
# 应用于列表 -> 通过下标索引删除元素 del list[index]
# 应用于字典 -> 通过键值删除对应的键值对 -> del dict[key]
# 应用于手动销毁一个对象 (了解)

例子7:

# 多维列表/元祖访问的示例
my_list = [1, 3, 5, {"name": "小明", "list": [2, 4, 6, (3, 6, 9, {"title": "终于等到你"})]}, 7]

# 获取终于等到你
# {'list': [2, 4, 6, (3, 6, 9, {'title': '终于等到你'})], 'name': '小明'}
# [2, 4, 6, (3, 6, 9, {'title': '终于等到你'})]
# (3, 6, 9, {'title': '终于等到你'})
desc = my_list[3]["list"][3][3]["title"]
print(desc)

运行结果:

终于等到你

例子8:

# 根据以下信息提示,请帮小明计算,他每月乘坐地铁支出的总费用
# 提示信息:
# 北京公交地铁新票价确定
# 据北京市发改委网站消息称,
# 北京市将从2015年12月28起实施公共交通新票价:
# 地铁6公里(含)内3元,公交车10公里(含)内2元,
# 使用市政交通一卡通刷卡乘公交车普通卡5折,学生卡2.5折。
# 具体实施方案如下:
# 一、城市公共电汽车价格调整为:
# 10公里(含)内2元,
# 10公里以上部分,每增加1元可乘坐5公里。
# 使用市政交通一卡通刷卡乘坐城市公共电汽车,市域内路段给予普通卡5折,学生卡2.5折优惠;
# 市域外路段维持现行折扣优惠不变。享受公交政策的郊区客运价格,由各区、县政府按照城市公共电汽车价格制定。

# 二、轨道交通价格调整为:
# 6公里(含)内3元;
# 6公里至12公里(含)4元;
# 12公里至22公里(含)5元;
# 22公里至32公里(含)6元;
# 32公里以上部分,每增加1元可乘坐20公里。
# 使用市政交通一卡通刷卡乘坐轨道交通,
# 每自然月内每张卡支出累计满100元以后的乘次,价格给予8折优惠;
# 满150元以后的乘次,价格给予5折优惠;
# 支出累计达到400元以后的乘次,不再享受打折优惠。
#
# 假设每个月,小明都需要上20天班,每次上班需要来回1次,即每天需要乘坐2次同样路线的公交;编写程序,帮小明完成每月乘坐公交需要的总费用
#
# 假设每个月,小明都需要上20天班,每次上班需要来回1次,即每天需要乘坐2次同样路线的地铁;编写程序,帮小明完成每月乘坐地铁需要的总费用
# 一、城市公共电汽车价格调整为:
# 10公里(含)内2元,
# 10公里以上部分,每增加1元可乘坐5公里。
# 使用市政交通一卡通刷卡乘坐城市公共电汽车,市域内路段给予普通卡5折,学生卡2.5折优惠;
# 市域外路段维持现行折扣优惠不变。享受公交政策的郊区客运价格,由各区、县政府按照城市公共电汽车价格制定.

# 定义一个变量判断是否是学生
is_student = False
# # 计算单程票价
# 定义一个变量记录单程的距离
km = 21
# 定义一个变量 保存单程票价
unit_price = 2
# 判断公里数
# 10公里(含)内2元,
if km <= 10:
    pass
# 10公里以上部分,每增加1元可乘坐5公里。
else:
    # 超出部分可以被5除尽
    # 如果坐了 15公里 -> (15 - 10 )/5 + 2
    # 超出部分不能被5除尽
    # 如果坐了14公里 -> int((16 - 10 )/5 ) + 2 + 1
    # 计算超出的部分
    temp_km = km - 10
    # 判断超出的部分是否可以除尽
    if temp_km % 5 == 0:
        unit_price += int(temp_km / 5)
    else:
        unit_price += int(temp_km / 5) + 1

print("单程票价:%d" % unit_price)

# 是否是学生
if is_student:
    unit_price *= 0.25
else:
    unit_price *= 0.5


print("最终票价:%.2f" % unit_price)

# 计算一个月的总花销

# 假设每个月,小明都需要上20天班,每次上班需要来回1次,即每天需要乘坐2次同样路线的公交;编写程序,帮小明完成每月乘坐公交需要的总费用

total_price = 20 * 2 * unit_price
print(total_price)
# 二、轨道交通价格调整为:
# 6公里(含)内3元;
# 6公里至12公里(含)4元;
# 12公里至22公里(含)5元;
# 22公里至32公里(含)6元;
# 32公里以上部分,每增加1元可乘坐20公里。

# 使用市政交通一卡通刷卡乘坐轨道交通,
# 每自然月内每张卡支出累计满100元以后的乘次,价格给予8折优惠;
# 满150元以后的乘次,价格给予5折优惠;
# 支出累计达到400元以后的乘次,不再享受打折优惠。

# 定义一个变量 保存单程距离
km = 53
# 定义一个变量 保存单程票价
unit_price = 0

# 6公里(含)内3元;
if km <= 6:
    unit_price = 3
# 6公里至12公里(含)4元;
elif km <= 12:
    unit_price = 4
# 12公里至22公里(含)5元;
elif km <= 22:
    unit_price = 5
# 22公里至32公里(含)6元;
elif km <= 32:
    unit_price = 6
# 32公里以上部分,每增加1元可乘坐20公里。
else:
    # 计算公里差值
    temp_km = km - 32
    # 判断差值是否可以除尽
    if temp_km % 20 == 0:
        # unit_price = 6 + int(temp_km / 20)
        unit_price = 6 + temp_km // 20
    else:
        unit_price = 6 + temp_km // 20 + 1

print("实际单程票价:%d" % unit_price)

# 定义一个变量 保存小明这个月的总消费
total_price = 0
# 假设每个月,小明都需要上20天班,每次上班需要来回1次,
# 即每天需要乘坐2次同样路线的地铁;
# 编写程序,帮小明完成每月乘坐地铁需要的总费用
# 使用市政交通一卡通刷卡乘坐轨道交通,
# 每自然月内每张卡支出累计满100元以后的乘次,价格给予8折优惠;
# 满150元以后的乘次,价格给予5折优惠;
# 支出累计达到400元以后的乘次,不再享受打折优惠。
for _ in range(40):
    # 判断
    if total_price <= 100:
        total_price += unit_price
    # 每自然月内每张卡支出累计满100元以后的乘次,价格给予8折优惠;
    elif total_price <= 150:
        total_price += unit_price * 0.8
    # 满150元以后的乘次,价格给予5折优惠;
    elif total_price <= 400:
        total_price += unit_price * 0.5
    # 支出累计达到400元以后的乘次,不再享受打折优惠。
    else:
        total_price += unit_price

print("小明这个月的总消费:%.2f" % total_price)

猜你喜欢

转载自www.cnblogs.com/kangwenju/p/12791464.html
今日推荐