Python第四天学习笔记
Python的用户交互
input('请插入vip卡')
input('输入密码')
请插入vip卡23333
输入密码6666
'6666'
print('*'*100)
****************************************************************************************************
print('*'*100)
input('请插入vip卡')
print('-'*100)
****************************************************************************************************
请插入vip卡6666
----------------------------------------------------------------------------------------------------
- input只是一个学习交互过渡用的,一般的用户交互是不会用到input的
vip_card = input('请插入vip卡')
print(vip_card)
请插入vip卡88888888
88888888
- 无论你输入的是什么,input接受的都是字符串
vip_card = input('请输入vip卡')
print(vip_card)
print(type(vip_card))
请输入vip卡123456789
123456789
<class 'str'>
age = input('请输入你的年龄')
print(age)
age = int(age) #将age由字符串转变为整型,当然这个字符串必须本来就是个数字
print(type(age))
请输入你的年龄22
22
<class 'int'>
Python2的input和raw_input(了解)
Python2的raw_input就是Python3的input
Python2中的input如果要是输入的是一个字符串,比如yang_height,它是分不清是字符串还是变量名的,此时需要声明数据类型
格式化输出
利用占位符
name = input('name:>>>')
age = input('age:>>>')
age = int(age)
height = input('height:>>>')
# %s就是字符串类型的占位,后面的%()就是对应前面的占位
print('My name is %s,My age is %s,My height is %s'%(name,age,height))
name:>>>yang
age:>>>22
height:>>>180
My name is yang,My age is 22,My height is 180
name = input('name:>>>')
name = int(name)
age = input('age:>>>')
age = int(age)
height = input('height:>>>')
height = int(height)
# %d是整型占位符,后面的%()也应该是整型
print('My name is %d,My age is %d,My height is %d'%(name,age,height))
name:>>>1
age:>>>1
height:>>>1
My name is 1,My age is 1,My height is 1
format格式化(了解)
name = input('name:>>>')
age = input('age:>>>')
height = input('height:>>>')
#print('My name is %s,My age is %s,My height is %s'%(name,age,height)) 占位符占位
print('My name is {},My age is {},My height is {}'.format(name,age,height))
name:>>>yang
age:>>>22
height:>>>180
My name is yang,My age is 22,My height is 180
print('My name is {0},My age is {1},My height is {2}'.format(name,age,height))
My name is yang,My age is 22,My height is 180
print('My name is {0},My age is {0},My height is {0}'.format(name,age,height))
My name is yang,My age is yang,My height is yang
print('My name is {age},My age is {name},My height is {height}'.format(name=name,age=age,height=height))
My name is 22,My age is yang,My height is 180
f-string格式化
name = input('name:>>>')
age = input('age:>>>')
height = input('height:>>>')
#print('My name is %s,My age is %s,My height is %s'%(name,age,height)) 占位符占位
#print('My name is {},My age is {},My height is {}'.format(name,age,height)) format格式化
print(f'My name is {age},My age is {name},My height is {height}')
name:>>>yang
age:>>>22
height:>>>180
My name is 22,My age is yang,My height is 180
f-string的内部占位还可以进行运算,更加方便
print(f'My name is {name},My age is{int(age)+1},My height is {height*10}')
My name is yang,My age is23,My height is 180180180180180180180180180180
print(f'My name is {name},My age is{int(age)+1},My height is {int(height)*10}')
My name is yang,My age is23,My height is 1800
print(f'My name is {name},My age is{int(age)+1:.5f},My height is {int(height)*10}')
My name is yang,My age is23.00000,My height is 1800
注::.3f是保留三位小数
基本运算符
算数运算符
1 + 1
2
1 - 1
0
1*1
1
1/1
1.0
1%1 #取余数
0
72//6 #向下取整
12
比较运算符
1 > 1
False
1 < 1
False
1 >= 1
True
1 <= 1
True
1 == 1 #注意这里是双等号
True
1 = 1 #单等号是赋值,会报错
Cell In[36], line 1
1 = 1 #单等号是赋值,会报错
^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
1 != 1
False
逻辑运算符
- and
- or
- not
1 <= 1 and 1 >= 1
True
1 > 1 and 1 <= 1
False
1 > 1 or 1 <= 1
True
not'张洋很帅'
False
身份运算符
is 比较的是id
x = 10
y = 10
print(x is y) # 因为小整数池的原因,结果是True
print(x == y)
True
True
x = 257
y = 257
print(x is y)
print(x == y)
False
True
id相同的值一定相同,值相同的id不一定相同,除了小整数池的除外
Python运算符优先级
想优先直接加括号()就OK
链式赋值
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
# Python中特有的方式
x = 10
y = 20
x,y =y,x
print(x,y)
20 10
解压缩
hobby_list = ['run','read','swimming','music','go']
print(hobby_list(3)) #记住是[3],不是(3)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[48], line 2
1 hobby_list = ['run','read','swimming','music','go']
----> 2 print(hobby_list(3))
TypeError: 'list' object is not callable
hobby_list = ['run','read','swimming','music','go']
# 0 1 2 3 4
print(hobby_list[3])
music
hobby_list = ['run','read']
hobby1,hobby2 = hobby_list
print(hobby1)
print(hobby2)
run
read
hobby_list = ['run','read','swimming','music','go']
_,hobby1,_,_,hobby2 = hobby_list
print(hobby1)
print(hobby2)
read
go
hobby_list = ['piao','666',2333,999,'handsome','read','piao','666',2333,999,'handsome','read']
# *_相当于把后面都占了
_,hobby1,_,hobby2,*_ = hobby_list
print(hobby1)
print(hobby2)
666
999
hobby_list = ['piao','666',2333,999,'handsome','read','piao','666',2333,999,'handsome','read']
hobby1,*_,hobby2 = hobby_list # *_把中间都吃了
print(hobby1)
print(hobby2)
piao
read
结束,睡觉

浙公网安备 33010602011771号