python(6):輸入、變量類型


一、輸入

1.python2中

(1).raw_input()

password = raw_input("請輸入密碼:")
print '您剛剛輸入的密碼是:', password

 

(2).input()

input()函數與raw_input()類似,但其接受的輸入必須是表達式。

>>> a = input() 
123
>>> a
123
>>> type(a)
<type 'int'>
>>> a = input()
abc
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'abc' is not defined
>>> a = input()
"abc"
>>> a
'abc'
>>> type(a)
<type 'str'>
>>> a = input()
1+3
>>> a
4
>>> a = input()
"abc"+"def"
>>> a
'abcdef'
>>> value = 100
>>> a = input()
value
>>> a
100

 

 2.python3中

沒有raw_input()函數,只有input()

並且 python3中的input與python2中的raw_input()功能一樣

 

二、變量類型

1、數字

int(整型)

在32位機器上,整數的位數為32位,取值范圍為-2**31~2**31-1,即-2147483648~2147483647
在64位系統上,整數的位數為64位,取值范圍為-2**63~2**63-1,即-9223372036854775808~9223372036854775807

2、布爾值

真或假,1 或 0。

3、字符串

"hello world"

字符串常用功能:移除空白、分割、長度、索引、切片

4、列表

person = {"name": "mr.wu", 'age': 18}
或
person = dict({"name": "mr.wu", 'age': 18})

常用操作:索引、切片、追加、刪除、長度、切片、循環、包含

5、元祖

創建元祖:

ages = (11, 22, 33, 44, 55)
或
ages = tuple((11, 22, 33, 44, 55))

 常用操作:索引、切片、循環、長度、包含

6、字典(無序)

創建字典:

person = {"name": "mr.wu", 'age': 18}
或
person = dict({"name": "mr.wu", 'age': 18})

常用操作: 索引;新增;刪除;鍵、值、鍵值對;循環;長度。

PS:循環,range,continue 和 break


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM