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

原文地址:https://www.cnblogs.com/wangchongzhangdan/p/9409595.html