python的一些语法糖

# 1."else" after the "while","for":
#   the "else" object will be executed when no "break" was executed in "while","for" object before
# num = [1, 2, 3, 4]
# mark = 0
# while mark < len(num):
#     n = num[mark]
#     if n % 2 == 0:
#         print(n)
#         # break
#     mark += 1
# else:
#     print("done")


# 2."zip" to zip two lists
# a = [1, 2, 3]
# b = ['one', 'two', 'three']
# list(zip(a, b))


# 3.推导式

#     3.1.列表推导式:[ expr for value in collection if condition ]
# strings = ['Bob', 'Tom', 'alice', 'Jerry', 'Wendy', 'Smith']
# result = [name.upper() for name in strings if len(name) > 3]

#     3.2.字典推导式:{ key_exp : value_exp for expression in iterable }
# strings = ['import', 'is', 'with', 'if', 'file', 'exception']
# result = {key: val for val, key in enumerate(strings)}

#     3.3.集合推导式:{ expression for expression in iterable }
# strings = ['a', 'is', 'with', 'if', 'file', 'exception']
# result = {len(s) for s in strings}


# 4.函数

#     4.1.默认参数值
#         需要同时设定所有参数的默认值,不能单独设定部分参数,
#         否则会报错:SyntaxError: non-default argument follows default argument
#         同时列表字典等可变数据类型不能作为默认参数值
# def do(a=1, b=1, c=1):
#     print(a, b, c)
#
#
# do()
# do(3)
# do(3, 3)
# do(3, 3, 3)

#     4.2.lambda匿名函数
# do = lambda x: x * x
# print(do(4))


# 5.super()调用父类方法
# class Father():
#     def __init__(self, name):
#         self.name = name
#         print("Father is here")
#
#
# class Son(Father):
#     def __init__(self, name, age):
#         super().__init__(name)
#         self.age = age
#
#
# a = Son('Jack', 15)
# print(a.name, a.age)
#
#
# 6.实例方法:
#       实例方法,以self作为第一个参数,当它被调用时,Python会把调用该方法的的对象作为self参数传入。
# class Person():
#     age = 15
#
#     def __init__(self, Age):
#         self.age = Age
#         print(Person.age)  # Person.age为类特征
#         print(self.age)  # self.age为对象特征
#
#
# Jack = Person(20)


# 7.{} & ".format"
#     usually used to change the format of output
# print('{} {} {}'.format(11, 22, 33))
# print('{2:2d} {0:-10d} {1:10d}'.format(11, 22, 33))
# print('{a} {b} {c}'.format(a=11, b=22, c=33))



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值