python 的与用户交互
input('请插入银行卡:')
input('输入密码')
但是输入并不是我们的目的,保存数据才是我们的目的
get_cardnumber = input('请插入银行卡')
get_pswd = input('请输入密码')
此时我们就把账号和密码保存到两个变量中了
无论我们输入什么 ,input只返回一个str类型 如果需要可以转换
get_cardnumber = input('请插入银行卡')
print(get_cardnumber,type(get_cardnumber)) #<class 'str'>
但是需要int类型的账号
get_cardnumber = int(getcardnumber)
print(getcardnumber,type(get_cardnumber))
此时 将会返回给你账号类型<calss 'int'> ,是合理的,同理,密码也是数字,也可以用这种方法处理
格式化输出
我们需要对格式的输出进行规范,达到简单方便的目的
普通演示
age = input('你的年龄: ')
print('你的年龄是',age)
逗号可以给一个空格
age = input('你的年龄: ')
print('你的年龄 '+age)
使用占位符
name = input('your name :')
age = int(input('your age :'))
height = input("your height : ")
print("my name is %s , my age is %d ,my height is %s"%(name .age .height))
看起来的确规范了许多
format 格式化(了解)
name = input('your name :')
age = int(input('your age :'))
height = input("your height : ")
#使用format格式化,感觉比占位符还像占位符
print('my name is {},my age is {},my height is {}'.format(name ,age ,height)) #是点 不是句号
可以为{}提供序号,但是index不能超过 否则会报错
print('my name is {1},my age is {0},my height is {2}'.format(name ,age ,height))
#my name is 22,my age is wuqiyu,my height is 183
print('my name is {name},my age is {age},my height is {height}'.format(name = name .age = age ,height = height))
f-string 格式化
f-string 可以在字符串中识别变量 ,很是方便
name = input('your name is :')
age = int(input('your age is :'))
height = input('your height is : ')
#____开始用f-string了!!!
print(f'my name is {name} ,my age is{age} and my height is {height}')
还可以进行运算
print(f'my name is {name} ,my age is{age+3} and my height is {height+3:.2f}')
算术运算符
1+1 加
1-1 减
1*1 乘
1/1 除
1//1 整除
1 %1 取余
1**1 幂运算
比较运算符
1>1 大于
1<1 小于
1>=1 大于等于
1<=1 小于等于
#1=1 错误用法 = 代表赋值
1==1 判断相等
1!=1 判断不等
比较运算符 的结果都是True 或者False
赋值运算符
a = 1
逻辑运算符
- and 两边都是True ,结果才返回True
- or 其中一个返回True就返回True
- not 把True变成False,False变成True
print(2>1 and 2<3) #True
print(3>1 or 3>4) #true
print(not(3>1 or 3>4)) #False
身份运算符
is 比较的是id
x=10
y=10
print(x is y) #True
print(x == y) #True
x=512
y=512
print(x is y) #False
print(x == y) #True
python运算符 (了解)
1*(3>2)*1 #1
反正括号最优先 不知道怎么做就加括号
交叉赋值(考试必考)
x=10
y=20
#调换两个变量的值
z=x
x=y
y=z
print(x,y)
x=10
y=20
x,y = y,x
print(x,y)
#同样可以交换两个变量的值
解压缩 (必考)
把一个列表的数据解压给一堆变量
#先创建一个list
hobbies_list = ['666',2333]
hobby1,hobby2 = hobbies_list
#此时hobbies_list 的两个值已经赋值给了hobby1和hobby2
print(hobby1,hobby2) #'666',2333
进阶用法
#创建num_list
num_list = [1,2,3,4.5,5,6]
get_n1,get_n2,get_n3,_,get_n4,get_n5=num_list #此时把4.5赋值给了_
print(get_n1,get_n2,get_n3,get_n4,get_n5) # 1 2 3 5 6 去掉那个带小数的 也就是 _
print(_) #4.5
进一步进阶
就想要第一个和最后两个
num_list = [1,2,3,4.5,5,6]
get_n1,*_,get_nf2,get_nf1 = num_list #
print(get_n1,get_nf2,get_nf1) #1 5 6
其他的值被存在*_中
print(*_) #2 3 4.5