第二天Python输入、if语句等

操作:鼠标能拖动代码和注释

print不换行
print("你好,end="----"")
print("鹿晗,end="----"")
print(i,end="--")

\n加字符串前面

字符串 %变量 格式化输出

names = "鹿晗"
print("姓名是:%s"%names)

多个输出用()

edu = "本科"
address = "顺义"
print("学历是:%s 住在%s"%(edu,address))

%5d %05d 占位
student_no = 1
print("学号是%5d"%student_no) #占5位
print("学号是%05d"%student_no) #占5位,前面用0来补

%.3f 保留小数
price = 30.666
print("价格是:%.3f"%price) # 默认6位小数 %.3f是%0.3f的简写,保留三位小数

%% 输出%
scale = 10
print("数据的比例是%.2f%%"%scale)

input输入: input(提示信息) 输入的都是字符串型的

转换:int(变量) float(变量) str(变量)

字符串小数不能转换成整型
b = "12.33" b = int(b) 报错
浮点型可转换成整型
b = 12.33 b = int(b) 没问题

扫描二维码关注公众号,回复: 2469929 查看本文章

两个数互换
a = 10
b = 20
a,b = b,a

// 取商 ** 幂

python异或、按位与

优先级:()>算数>比较>逻辑>赋值 +=、...、%=、//=算赋值
result = 3 * 5 > 12 and 4 - 2 < 1

if语句判断,两个数比较不用浮点型float

if、if else、if elif if

random生成随机数
import random
n = random.randint(0,2) # [0,2] 0-2生成随机数

猜你喜欢

转载自blog.51cto.com/13901400/2152366