02-Python输入输出及运算符

一、输入

1、python3中的input

 inp_username=input("请输入您的密码:") # "18"
 print(inp_username)
 print(type(inp_username))
input赋值为字符串型,如上数据类型为字符串型

 age=input('your age: ') # age="18"
 age=int(age) # 前提是:字符串中包含的必须是纯数字
 print(type(age))
 print(age > 10) # "18" > 10

 int('123123123asdf') # 报错
2、在python2中有一个input:要求程序的使用者必须输入一个明确的数据类型(了解)
# 特点是:输入什么类型,就会被直接存成什么类型,程序中无需转换直接使用就好


在python2中有一个raw_input与python3的input是一模一样

二、输出

1、格式化输出

#1、%形式(速度最慢)
print("My name is %s and my age is %s" %(name,age))
#print("My name is %s and my age is %s" %(name))            #TypeError: not enough arguments for format string
#print("My name is %s and my age is %s" %(name,age,age))    #TypeError: not all arguments converted during string formatting

print("成功率为:%s%%"%(97,))                #在python中,%后必须跟s d的数据类型,想单独输出百分号,要输入两个百分号来转义



#%s是可以接受任意类型的(本要求是字符串类型)
print("my age is %s"  %18)
print("my age is %s" %[1, 2, 3])

#以字典形式传值,打破位置限制,无须记位置来一一对应
res = "我的名字是 %(name)s,我的年龄是 %(age)s"%{"name":"Jil", "age": 18}
#%s是可以接受任意类型,此处也可写成%(age)d
print(res)                      #结果为:我的名字是 Jil,我的年龄是 18


#了解
print("my age is %d" %18)
#print("my age is %d" %"18")#报错,字符串类型不能对应数字类型

print(name.isdigit()) #"字符串".isdigit() 判断字符串是否是纯数字



#2、format的三种玩法:兼容性好
#format():把传统的%替换为{}来实现格式化输出
#其实就是format()后面的内容,填入大括号中(可以按位置,或者按变量)
res='个人资料:{} {} {}'.format('egon',18,'male')         #按顺序填入
print(res)

#这里注意有两层大括号,输出的结果只有一层大括号
print('数字{{{1}{2}}}和{0}'.format("123",456,'789'))

res='个人资料:{1} {0} {2}'.format('egon',18,'male')     #按括号中元素编号索引,0代表第一个数据元素,1代表第二个
print(res)

#打破位置限制,按照key=value取值
res='个人资料:{name} {age} {sex}'.format(sex='male',name='egon',age=18)      #按变量填入
print(res)

#format的格式化填充
print("{0:*<10}".format(name))       #左对齐,若不足10个字符,则用*填充    Jil*******
print("{0:*>10}".format(name))      #右对齐,若不足10个字符,则用*填充     *******Jil
print("{0:*^10}".format(name))      #居中,若不足10个字符,则用*填充       ***Jil****

x = print("a")              #此处x为空,print无返回值
print(x)                    #结果为None

print("{0:->10}".format(15))          #--------15
print("你妹{0:*^10}".format("name"))  #你妹***name***,只是在{}这十个字符中居中


#format的精度与进制
print("{salaries:.3f}".format(salaries = 125831545.5555))              ##精确到小数点后3位,四舍五入,结果为:125831545.556
print('{0:b}'.format(123))          # 转成二进制,结果为:1111011
print("{0:o}".format(9))            #转换成八进制,结果为:11
print("{0:x}".format(15))           #转换成十六进制,结果为:f
print("{0:,}".format(49494897))     #转换为千分位格式化,结果为:49,494,897

#3、f:python3.5后才支持  (速度最快,但兼容性较差)
print(f"name is {name},age is {age}")

print(f"{10+3}")                    # 打印13
f"{print('dd')}"                    #打印输出 相当于{}中的匿名变量被赋值  相当于x = print("dd")

2、输出不换行

输出不换行如下输出,即让打印结果以" "结尾,不再以"
"结尾
print("aaa", end= " ")

三、运算符

#1、算数运算符
print(10 + 3)
print(10 - 3)
print(10 * 3)
print(10 / 3) # 保留小数部分
print(10 // 3) # 只保留整数部分,相当于C语言中的两个整形变量的/
print(10 % 3) # 取余数,取模
print(10 ** 3)

#2、比较运算符:
x=10
y=10
print(x == y) # =一个等号代表的是赋值

x=3
y=4
print(x != y) # 不等于

x=3
y=4
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True

print(10 <= 10) # True

#3、赋值运算符
age=18
age=age + 1 # 赋值运算
age+=1 # 赋值运算符,age=age+1
print(age)

age*=10 # age=age*10
age**=10 # age=age**10
age/=10 # age=age/10
age//=10 # age=age//10
age-=10 # age=age-10
print(age)

#4、逻辑运算符
# and: 逻辑与,and是用来连接左右两个条件,只有在左右两个条件同时为True,最终结果才为True,但凡有一个为False,最终结果就为False

print(10 > 3 and True)
print(10 < 3 and True and 3 > 2 and 1==1)

# or:逻辑或,or是用来连接左右两个条件,但凡有一个条件为True,最终结果就为True,除非二者都为False,最终结果才为False
print(True or 10 > 11 or 3 > 4)
print(False or 10 > 11 or 3 > 4)
print(False or 10 > 9 or 3 > 4)

        # False      or     (True and True)
        # False      or      True
res=(True and False) or (10 > 3 and (3 < 4 or 4==3))
print(res)

# not:把紧跟其后那个条件运算的结果取反
print(not 10 > 3)


#         False      or     (False and False)
#         False      or     False
res=(True and False) or (not 10 > 3 and (not 3 < 4 or 4==3))
print(res)

逻辑运算符补充(短路运算):https://www.cnblogs.com/zhubincheng/p/12341470.html
 

 赋值运算符补充:

# 交叉赋值
a = 10
b = 20
print(a,b)

temp=b # temp=20
b=a # b = 10
a=temp

# python一行代码搞定
a, b = b, a
print(a,b)


# 链式赋值
a=7
b=a
c=b
d=c

a = b = c = d = 7
print(a,b,c,d)


# 解压赋值: 取开头和结尾的几个值
salaries=[33,44,55,66,77]
x=salaries[0]
y=salaries[1]
z=salaries[2]
a=salaries[3]
b=salaries[4]

# 左边变量名的个数与右面包含值的个数相同,多一个不行,少一个也不行
x,y,z,a,b=salaries
print(x,y,z,a,b)


salaries=[33,44,55,66,77,88,99]
x,y,z,*abc=salaries
'''
x,y,z会对应列表salaries的前三个值
然后*会把剩余的值存放一个列表,然后赋值给abc
'''
print(x,y,z)
print(abc)

#_当变量名,代表该变量值是无用的
x,y,z,*_=salaries # 取前三个值
print(x,y,z)
print(_)


salaries=[33,44,55,66,77,88,99]
*_,m,n=salaries # 取后两个值
print(_)
print(m,n)


salaries=[33,44,55,66,77,88,99]
x,y,z,*_,m=salaries # 取后两个值
print(x,y,z)
print(m)


# d={'a':1,'b':2,'c':3}
# x,y,z=d
# print(x,y,z)
 
原文地址:https://www.cnblogs.com/zhubincheng/p/12341578.html