Python Learning #02 - Input, Output and Assignment

输入,输出,赋值

2020-01-02 Mike Lyou

赋值

1
2
3
4
a = 10
b = 3
a += b # 相当于:a = a + b
a *= a + 2 # 相当于:a = a * (a + 2)

输入input

1
2
3
a = int(input('a = ')) #读取为int类型
f = float(input('请输入华氏温度: ')) #显示输入提示
a, b = map(int, input().split()) #读取在同一行的多个数字

输出print

1
2
3
4
print(a)
print(a + b)
print(a,b) #输出结果会以空格隔开
print('%.1f' % a) #输出float型变量a,保留一位小数

Python格式化输出

  • 使用end函数使print不换行
    1
    2
    
    for i in range(1, 5+1):
      print (i, end = " ")  #1 2 3 4 5
    

References



Author: Mike Lyou
Link: https://blog.mikelyou.com/2020/01/02/python-learning-02-input-output-assignment/
Liscense: Copyright Statement: This python learning series are posted only for personal studies and communication. The whole series are not allowed to be reproduced unles otherwise indicated. I do not own copyright of some of the content. If any post accidentally infringes your copyright, it will be removed shortly after being informed. View python-learning-readme for more information.