溫馨提示×

python命令行交互如何進行變量操作

小樊
93
2024-12-11 12:27:20
欄目: 編程語言

在Python的命令行交互式環境中,可以使用以下方法進行變量操作:

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

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女