1. 程式人生 > >Python基本輸出語句/輸入語句/變量解析

Python基本輸出語句/輸入語句/變量解析

精度 put 轉化 結果 浮點 解析 pri 體重 2.3

print格式化輸出

# -*- coding: utf-8 -*-
# print (format(val, ‘m,n‘))
# val:值 format_modifier:輸出占位m,精度n
print (format(12.3456,6.2f ))
 12.35
print (format(12.3456,6.3f ))
12.346
print (format(12.3456,6.6f ))
12.345600
print (format(12.3456,6.0f ))
    12

print輸入語句

input接收的值會轉化為字符型,int()可將值轉化為整型數據,float()將值轉化為浮點型數據

# -*- coding: utf-8 -*-

name = raw_input(請輸入姓名: )
print name
print type(name)

age = raw_input(請輸入年齡: )
age = int(age)
print type(age)
age = age + 1
print age

weight = float(raw_input(請輸入體重: ))
print weight
print type(weight)

"""運行結果"""
請輸入姓名: lee
lee
<type str>
請輸入年齡: 13
<type 
int> 14 請輸入體重: 40.22 40.22 <type float>

變量解析

變量改變只改變指向地址不改變指向內容

# -*- coding: utf-8 -*-

x = 13
print x, id(x)
y = 12
print y, id(y)
x = y
print x, id(x)
z = 13
print z, id(z)

"""運行結果"""

13 73496064
12 73496076
12 73496076
13 73496064

Python基本輸出語句/輸入語句/變量解析