In [ ]:
if condition:
do somthing
else:
do somthing
In [2]:
total_cost = 32.5
if total_cost > 30:
discount = 0.9
else:
discount = 1
total_cost *= discount
print('小姐姐的实际花费为: {}元'.format(total_cost))
In [7]:
total_cost = 32.5
is_vip = True
if total_cost > 50:
if is_vip:
discount = 0.8
else:
discount = 1
elif total_cost > 30:
discount = 0.9
else:
discount = 1
total_cost *= discount
print("总花费为: {} 元".format(total_cost))
1、条件判断可以任意组合
第一层意思:elif可以有0到任意多个,else可有可无
第二层意思:条件判断可以进行嵌套
2、着重来看一下condition
In [10]:
bool(''), bool({}), bool([])
Out[10]:
In [14]:
condition = {}
if condition:
print('True')
else:
print('False')
and or not
In [19]:
a = True
b = False
print('a and b is {}'.format(a and b))
print('a or b is {}'.format(a or b))
In [25]:
a = ''
b = []
print(bool(b))
print('a and b is {}'.format(a and b))
print('a or b is {}'.format(a or b))
In [28]:
# 非布尔型变量 and 运算
a = [1,2,3]
b = 10
print(b and a)
# 非布尔型变量 or 运算
a = 'ni hao'
b = {'apple': 100}
print(a or b)
# 非布尔型变量 not 运算,永远返回True 或者 False
print(not b)
In [33]:
if not condition:
crash program
# 它的意思就是说:我断言它肯定是这样的,如果不这样,那我就崩溃
In [36]:
age = 19
assert age == 18, '她竟然不是18岁'
for 循环 - 遍历循环
while 循环 - 条件
In [39]:
costs = [3,4,12,23,43,100]
for cost in costs:
print('消费 {} 元'.format(str(cost).center(10)))
In [82]:
import random
random_numbers = []
while len(random_numbers) < 20:
random_numbers.append(random.randint(1,10))
print(random_numbers, len(random_numbers))
In [87]:
random_numbers = []
for i in range(20):
random_numbers.append(random.randint(1,10))
print(random_numbers, len(random_numbers))
In [101]:
random_numbers = []
while (9 not in random_numbers):
random_numbers.append(random.randint(1,10))
print(random_numbers, len(random_numbers))
In [102]:
# 死循环演示
import time
number = 0
while True:
time.sleep(1)
number += 1
print('hello world. {} '.format(number), end='\r')
In [109]:
a = [1]
b = (1,)
type(a), type(b), b, len(b)
Out[109]:
In [110]:
random_numbers
Out[110]:
In [120]:
random_numbers = [2,3,5,6,8]
In [121]:
for number in random_numbers:
if number % 2 == 0:
print('{} is 偶数'.format(number))
else:
continue
print('没有跳过')
In [122]:
for number in random_numbers:
if number % 2 == 0:
print('{} is 偶数'.format(number))
else:
break
print('没有结束')
In [127]:
random_numbers = [4,2,4]
for number in random_numbers:
if number % 2 == 0:
print('{} is 偶数'.format(number))
else:
break
print('没有结束')
else:
print("全是偶数")
In [128]:
# 所谓推导式,就是一种从一个数据序列构建另一个数据序列的方法。
In [129]:
random_numbers = list(range(10))
random_numbers
Out[129]:
In [130]:
new_numbers = []
for number in random_numbers:
new_numbers.append(number*10)
new_numbers
Out[130]:
In [131]:
new_numbers = [number*10 for number in random_numbers]
new_numbers
Out[131]:
In [133]:
dict_numbers = {number: 'A' for number in random_numbers}
dict_numbers
Out[133]:
In [147]:
tuple_numbers = (number*10 for number in random_numbers)
tuple_numbers
Out[147]:
In [146]:
tuple(tuple_numbers)
Out[146]:
In [148]:
varibal = {
'a': 100,
'b': 100,
'c': 200
}
In [149]:
varibal['a']
Out[149]:
In [150]:
varibal.items()
Out[150]:
In [151]:
[key for key, value in varibal.items() if value == 100]
Out[151]:
In [157]:
def get_keys(dict_varibal, value):
return [k for k, v in dict_varibal.items() if v == value]
In [158]:
get_keys(varibal, 200)
Out[158]:
In [161]:
get_keys({'a': 40}, 40)
Out[161]:
In [165]:
get_keys(40, {'a': 40})
In [162]:
def get_keys(dict_varibal, value):
return [k for k, v in dict_varibal.items() if v == value]
1、位置参数
2、关键字参数, 可以不按照顺序去写
In [163]:
get_keys(dict_varibal={'a': 40}, value=40)
Out[163]:
In [164]:
get_keys(value=40, dict_varibal={'a': 40})
Out[164]:
In [166]:
def test(varibal):
varibal = 100
return varibal
In [167]:
var = 1
test(var)
Out[167]:
In [168]:
print(var)
In [170]:
def test(varibal):
varibal.append(100)
return varibal
In [171]:
var = []
test(var)
Out[171]:
In [172]:
var
Out[172]:
In [174]:
def test(varibal):
temp = varibal.copy()
temp.append(100)
return temp
In [179]:
var = []
var = test(var)
In [178]:
var
Out[178]:
In [196]:
def test(name, age, *args, **kwargs):
print(name, age, *args, **kwargs)
In [184]:
test('wong', 12)
In [187]:
test('wong', 12, 23, 'lkl', [23,34])
In [199]:
dict_varibals = {
'weight': 120,
'height': 175
}
test('wong', 12, dict_varibals)
In [200]:
a = 10
b = [12,12]
def test():
print('test')
c = test
In [202]:
c.__name__
Out[202]:
In [206]:
def test(func):
return func
def func():
print('func run')
f = test(func)
f.__name__
f()
In [211]:
# 返回一个从0到1的浮点值
def test():
return round(random.random(), 1)
In [210]:
# 函数返回的浮点值保留三个有效数字
In [212]:
test()
Out[212]:
In [241]:
# 返回一个从0到1的浮点值
@decorator
def test():
return random.random()
@decorator
def test_two():
return random.random()*10
In [242]:
def decorator(func):
def wrapper(*args, **kwargs):
# do something
return round(func(*args, **kwargs), 2)
return wrapper
In [246]:
# f = decorator(test) 完全等价于装饰器@的写法
In [244]:
test()
Out[244]:
In [245]:
test_two()
Out[245]:
In [284]:
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
def get_name(self):
return self._name
def rename(self, new_name):
self._name = new_name
In [248]:
s = 'hello world'
s.center(12)
Out[248]:
In [285]:
p = Person('wong', 12)
In [261]:
p.get_name()
Out[261]:
In [262]:
p.rename('wong lei')
In [263]:
p.get_name()
Out[263]:
In [258]:
p_2 = Person('li', 11)
p_2.get_name()
Out[258]:
In [276]:
class Student(Person):
def set_score(self, score):
self._score = score
def get_score(self):
return self._score
In [277]:
s = Student('liu', 24)
s.get_name()
Out[277]:
In [278]:
s.set_score(100)
In [279]:
s.get_score()
Out[279]:
In [292]:
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
def rename(self, new_name):
self._name = new_name
In [293]:
p = Person('liu', 24)
p.name
Out[293]: