格式化输出
a = 'Tom'
b = 'is'
c = 'a'
d = 'good'
e = 'boy'
print(a + b)
Tomis
print(a, b, c, d, e)
print(a + ' ' + b + '' + c)
Tom is a good boy
Tom isa
%占位符(了解)
print('%s %s %s %s %s' % (a, b, c, d, e))
Tom is a good boy
print('%s %s %s %s' % (a, b, c, d, e))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-07e6d91630cb> in <module>
----> 1 print('%s %s %s %s' % (a, b, c, d, e))
TypeError: not all arguments converted during string formatting
print('%s %s %s' % (a, b,))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-b0716021e3eb> in <module>
----> 1 print('%s %s %s' % (a, b,))
TypeError: not enough arguments for format string
需要几个就传几个
format占位符(了解)
print('{} {} {} '.format(a, b, c))
Tom is a
print('{:.2f}'.format(15.3231))
15.32
f-string格式化(推荐使用)
print(f'{a} {b} {c} {d} {e}')
Tom is a good boy
print(F'{a} {b} {c} : {d} {e}')
Tom is a : good boy
salary = 15000.6842
print(f'{salary:.3f}')
15000.684
name = 'Tom'
height = 185
weight = 150
print('name:%s height:%s weight:%s' % (name, height, weight))
print('name:{} height:{} weight:{}'.format(name, height, weight))
print(f'name:{name} height:{height} weight:{weight}')
name:Tom height:185 weight:150
name:Tom height:185 weight:150
name:Tom height:185 weight:150
基本运算符
- 算数运算符(掌握)
- 比较运算符(熟悉)
- 赋值运算符(掌握)
- 逻辑运算符(掌握)
- 身份运算符(掌握)
算数运算符
a = 10 b = 20
+ | 加 | a+b=30 |
---|---|---|
- | 减 | a-b=-10 |
* | 乘 | a*b=200 |
/ | 除 | a/b=0.5 |
** | 幂 | a**b = 1020 |
// | 整除 | a//b =0 |
% | 取模 | a%b=10 |
x = 1
y = 2
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x ** y) # 幂
print(x // y) # 整除
print(x % y) # 取余
3
-1
2
0.5
1
0
1
比较运算符
a = 10 b = 20
运算符 | 描述 | 实例 |
---|---|---|
== | 等于 | (a==b) 返回False |
!= | 不等于 | (a!=b) 返回True |
> | 大于 | (a>b)返回False |
< | 小于 | (a<b)返回True |
>= | 大于等于 | (a>=b)返回False |
<= | 小于等于 | (a<=b)返回True |
x = 100
print(x == 100)
print(x != 100)
print(x > 90)
print(x < 90)
print(x >= 90)
print(x <= 90)
type(x < 120)
True
False
True
False
True
False
bool
赋值运算符
a = 10 b = 20
运算符 | 描述 | 实例 |
---|---|---|
= | 赋值 | c=a+b即a+b的结果赋值给c |
+= | 加法赋值 | c+=a即c=c+a |
-= | 减法赋值 | c-=a即c=c-a |
*= | 乘法赋值 | c*=a 即c=c * a |
/= | 除法赋值 | c/=a即c=c/a |
**= | 幂赋值 | c** a=a 即c=c **a |
//= | 取整除赋值 | c//a = a即c=c//a |
%= | 取模赋值 | c%a=a 即c=c%a |
y = 500
print(y)
id(y)
500
1638542637552
y += 100
print(y)
id(y)
600
1638542637616
y -= 100
print(y)
id(y)
500
1638542637584
y *= 10
print(y)
id(y)
5000
1638542638832
y /= 100
print(y)
id(y)
50.0
1638541796312
逻辑运算符
a = 10 b = 20
运算符 | 描述 | 实例 |
---|---|---|
and | 布尔“与”-如果a为False,则返回False,否则返回b的计算值 | (a andb)返回20 |
or | 布尔“或”-如果a为非0,则返回a,否则返回b的计算值 | (a or b)返回 10 |
not | 布尔“非”-如果a为True,则返回False | not(a and b)返回False |
1 > 2 and 4 > 3
False
1 > 2 or 4 > 3
True
not 1 > 2 and 4 > 3
True
身份运算符(纯了解)
x = 257
y = 257
print(id(x) == id(y))
False
print(x is y)
False
补充
- python运算符优先级(掌握)
- 链式赋值(掌握)
- 交叉赋值(掌握)
- 解压缩(掌握)
链式运算符
x = 10
y = 10
z = 10
print(x, y, z)
10 10 10
x = y = z = 10
print(x, y, z)
10 10 10
交叉运算符
x = 10
y = 20
z = x
x = y
y = z
print(x, y)
20 10
x = 10
y = 20
x, y = y, x
print(x, y)
20 10
解压缩
tom_hobbies_list = ['fishing', 'reading', 'running']
other_hobbies_list = ['sleeping', 'eating', 'drinking', 'playing']
hobby1 = tom_hobbies_list[2]
hobby2 = tom_hobbies_list[0]
print(hobby1, hobby2)
running fishing
hobby1, hobby2, hobby3 = tom_hobbies_list
print(hobby1, hobby2, hobby3)
fishing reading running
hobby1, hobby2 = tom_hobbies_list
print(hobby1, hobby2)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-37-9ca5877dad71> in <module>
----> 1 hobby1, hobby2 = tom_hobbies_list
2 print(hobby1, hobby2)
ValueError: too many values to unpack (expected 2)
hobby1, hobby2, hobby3 = tom_hobbies_list
print(hobby1, hobby2)
fishing reading
hobby1, _, hobby3, _ = other_hobbies_list
print(hobby1, hobby3)
sleeping drinking
hobby1, hobby2, *_ = other_hobbies_list
print(hobby1, hobby2)
sleeping eating
if判断
if ...
if 条件 :
代码块
dog1 = '疯狗'
dog2 = '正常狗'
if dog1 == '疯狗' :
print('远离它')
远离它
if...else...
if 条件 :
代码块
else :
代码块
if dog1 == '疯狗' :
print('远离它')
else :
print('抚摸它')
远离它
if...elif...else..
if 条件:
代码块
elif 条件:
代码块
else :
代码块
# 180以上全票,140-170半票,120以下免票
height = int(input('>>>请输入您的身高:'))
if height > 180 :
print('全票')
elif height < 120 :
print('免票')
elif height <170 and height > 140 :
print('半票')
else :
print('身高不符')
>>>请输入您的身高:185
全票
if 嵌套
if 条件 :
代码
else :
if 条件 :
代码
else :
代码
while True :
age = int(input('>>>请输入年龄:'))
if age < 50 :
print('too small')
elif age > 50 :
print('too big')
else :
print('congratulations')
break
>>>请输入年龄:39
too small
>>>请输入年龄:59
too big
>>>请输入年龄:50
congratulations