赞
踩
在 python 中定义变量是不需要指定类型(在其他很多高级语言中都需要)
数据类型可以分为数字型、非数字型
- 数字型:整型(int)、浮点型(float)、 布尔型(bool): 真 True 、假 False,总之非 0 即真
- 非数字型:字符串、 列表、 元组、 字典
使用 type 函数可以查看一个变量的类型 在使用交互式终端时,也可以知道每个变量的准确类型
数字变量之间可以直接计算
在 python 中,两个数字型变量是可以直接进行算术运算的
如果变量是 bool 型,在计算时:
True 对应的数字是 1
Fals 对应的数字是 0
字符串变量之间使用 + 拼接字符串
在 python 中,字符串之间可以使用 + 拼接生成新的字符串,字符串变量可以和整数使用 * 重复拼接相同的字符串
注意:字符串变量和字符串之间不能进行其他计算
[kiosk@foundation24 Desktop]$ ipython
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: name = 'tutu'
In [2]: name
Out[2]: 'tutu'
In [3]: type(name)
Out[3]: str
In [4]: age = 18
In [5]: type(age)
Out[5]: int
In [6]: gender = True
In [7]: type(gender)
Out[7]: bool
In [8]: height = 1.75
In [9]: type(height)
Out[9]: float
In [10]: 2 ** 32
Out[10]: 4294967296
In [11]: type(2 ** 32)
Out[11]: int
In [12]: 2 ** 64
Out[12]: 18446744073709551616L
In [13]: type(2 ** 64)
Out[13]: long
In [14]: i = 10
In [15]: f = 10.5
In [16]: b = True
In [17]: i + f
Out[17]: 20.5
In [18]: f - b
Out[18]: 9.5
In [19]: i * f
Out[19]: 105.0
In [20]: first_name = 'lily'
In [21]: first_name = 'tutu'
In [22]: last_name = 'hu'
In [23]: first_name
Out[23]: 'tutu'
In [24]: last_name
Out[24]: 'hu'
In [25]: first_name + last_name
Out[25]: 'tutuhu'
In [26]: first_name * 10
Out[26]: 'tutututututututututututututututututututu'
In [27]: last_name * 5
Out[27]: 'huhuhuhuhu'
In [28]: (first_name + last_name) * 5
Out[28]: 'tutuhututuhututuhututuhututuhu'
In [29]: first_name = 'tom'
In [30]: first_name + 10
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-31-0198723420d2> in <module>()
----> 1 first_name + 10
TypeError: cannot concatenate 'str' and 'int' objects
In [31]:
Do you really want to exit ([y]/n)? y
[kiosk@foundation24 Desktop]$
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
例如:去银行取钱,在 ATM 上输入密码 在 python 中,如果要获取用户在键盘上的输入信息,需要使用
raw_input 函数 关于函数: 一个提前准备好的功能(别人或者自己写的代码),可以直接使用,而不用关心细节 raw_input
函数实现键盘输入 在 python 中可以使用 raw_input 函数从键盘等待用户的输入 用户输入的任何内容 python
都认为是一个字符串
[root@foundation50 kiosk]# ipython
Python 2.7.5 (default, Oct 11 2015, 17:47:16)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: raw_input()
123
Out[1]: '123'
In [2]: raw_input('请输入银行卡密码:')
请输入银行卡密码:123
Out[2]: '123'
In [3]: passwd = raw_input('请输入银行卡密码:')
请输入银行卡密码:123
In [4]: passwd
Out[4]: '123'
In [5]: type(passwd)
Out[5]: str
In [6]: age = raw_input('请输入您的年龄:')
请输入您的年龄:18
In [7]: age
Out[7]: '18'
In [8]: type(age)
Out[8]: str
In [9]: int('123')
Out[9]: 123
In [10]: type(int('123'))
Out[10]: int
In [11]: float('12.3')
Out[11]: 12.3
In [12]: type(float('12.3'))
Out[12]: float
In [13]: age = int(raw_input('请输入您的年龄:'))
请输入您的年龄:18
In [14]: age
Out[14]: 18
In [15]: type(age)
Out[15]: int
In [16]: exit
[root@foundation50 kiosk]#
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在 python 中可以使用 print 函数将信息输出到控制台,如果希望输出文字信息的同时,一起输出数据,就需要使用到格式化操作符
%:被称为格式化操作符,专门用于处理字符串中的格式
包含%的字符串,被称为格式化字符串
格式化字符串 含义 %s 字符串 %d 符号十进制整数,%06d 表示输出的整数显示位数字,不足的地方使用0 补全 %f 浮点数,%.02f 表示小数点后只显示两位 %% 输出% 语法格式:
print '格式化操作符' % 变量
print '格式化操作符' % (变量1,变量2...)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。