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是一模一样


#二、输出
print('asdfjasdfasfasdf')
print("my name is %s my age is" %"egon")

inp_name=input('请输入您的名字:')
inp_age=input('请输入您的年龄:')

print("my name is %s my age is %s" %(inp_name,inp_age))


# %s是可以接收任意类型的
print("my age is %s" %18)
print("my age is %s" %[1,2,3])


# 了解
print("my age is %d" %18)
print("my age is %d" %"18")#报错,字符串类型不能对应数字类型
print(inp_name.isdigit()) #"字符串".isdigit() 判断字符串是否是纯数字
 

#三、运算符

#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)
 

 

猜你喜欢

转载自www.cnblogs.com/zhubincheng/p/12341578.html