在Python的命令行交互式環境中,可以使用以下方法進行變量操作:
>>> x = 10
>>> y = "Hello, World!"
>>> print(x)
10
>>> print(y)
Hello, World!
>>> x = 20
>>> y = "你好,世界!"
>>> z = x + y
>>> print(z)
31
del
命令刪除變量:>>> del x
>>> del y
type()
函數查看變量的類型:>>> type(x)
<class 'int'>
>>> type(y)
<class 'str'>
input()
函數接收用戶輸入并將其賦給變量:>>> name = input("請輸入你的名字:")
請輸入你的名字:John
>>> age = int(input("請輸入你的年齡:"))
請輸入你的年齡:25
if
語句進行條件判斷:>>> if age >= 18:
... print("成年人")
... else:
... print("未成年人")
...
成年人
for
循環遍歷序列(如列表、元組、字符串):>>> for letter in y:
... print(letter)
...
H
e
l
l
o
,
W
o
r
l
d
!
while
循環進行條件判斷:>>> count = 0
>>> while count < 5:
... print(count)
... count += 1
...
0
1
2
3
4