学习04

使用格式化的三种方式输出自己的name、height、weight

第一种占用符方式

name=input('name>>>')
height=input('height>>>')
weight=input('weight>>>')
print('My name is %s,My height is %s,My weight is %s'%(name,height,weight))

name>>> dy
height>>> 160
weight>>> 180

My name is dy,My height is 160,My weight is 180

第二种format格式

name=input('name>>>')
height=input('height>>')
weight=input('weight>>>')
print('My name is {},My height is {},My weight is {}'.format(name,height,weight))

name>>> ybz
height>> 164
weight>>> 100

My name is ybz,My height is 164,My weight is 100

第三种f-string格式

name=input('name>>>')
height=input('height>>')
weight=input('weight>>>')
print(f'My name is {name},My height is {int(height)+10},My weight is {int(weight)-10}')

name>>> cyt
height>> 175
weight>>> 140

My name is cyt,My height is 185,My weight is 130

基本运算符

算术运算符:+ - * /

x=10
y=10
print(x+y)
print(x*y)
print(x/y)
print(x-y)

20
100
1.0
0

比较运算符: > < >= <= ==

c
print(x>y)
print(x==y)

False
True

逻辑运算符:and not or

x=10
y=10
print(x<=y and x==y)
print(x<y or x==y)

True
True

赋值运算符:=

  • 交叉赋值
x=10
y=20
z=x
x=y
y=z
print(x,y)

20 10

x=10
y=20
y,x=x,y
print(x,y)

20 10

  • 链式赋值
x=y=10
print(x,y)

10 10

身份运算符:is

x=257
y=257
print(x==y)
print(id(x)==id(y))

True
False

x=10
y=10
print(x==y)
print(id(x)==id(y))

True
True

dy_hobby_list = ['sleep','dao','mu','c']
_,hobby1,hobby2,_=dy_hobby_list
print(hobby1,hobby2)

dao mu

posted @ 2025-12-03 10:51  LM0T  阅读(9)  评论(0)    收藏  举报