2.1不同类型的数值
Python中的数值:整型、浮点型、虚数(以j结尾,如:12j;5+12j为复数)
确定数值的分类用内置函数type()
用法 : >>>type(1) ===> <class 'int'>
>>>type(12j) ===> <class 'complex'>
>>>type(12.56) ===> <class 'float'>
2.2.1程序文件
举例:在记事本中写入print("Hello World!"),保存为Test001.py格式,存放到Python安装路径下。在SHELL中->File->Open->Test001.py->点击F5运行。
2.2.2使用不同的类型
(1)内置函数str()可以实现字符串和数字的拼接
(2)在字符串中包含不同的数字
# %%d 连续两个%可以打印出字符串%d
# %0.3f表示浮点数精确到小数点后三位 ,采用四舍五入方式
举例1: >>>"Including an integer works with %%d like this: %d" % 10 ===>Including an integer works with %d like this: 10
举例2: >>>"An integer converted to a float with %%f like this: %f" % 5 ===>An integer converted to a float with %f like this: 5.000000
举例3 >>>"A normal float with %%f: %f" % 1.25 ===>A normal float with %f: 1.250000
举例4:>>>"A large number:%E" % 2.5e10 ===>A large number:2.500000E+10
2.2.3 基本算术
#加法和减法
举例:>>>5+30 ===>35
>>>2.5+10 ===>12.5
>>>12-20 ===>-8
#乘法和除法
>>>200812454 *200812454 ===>40325641681502116
>>>40325641681502116*2e304 ===>inf # (infinity无穷大)
>>>44/11 ===>4.0 #虽然显示为浮点数但是实际上还是整数
#取余数 %
>>>53%11 ===>9
2.3 可能出现的错误
举例1:>>>print("%.03f"% (30.1113,12))
#参数多出
Traceback (most recent call last):
File "<pyshell#63>", line 1, in <module>
print("%.03f"% (30.1113,12))
TypeError: not all arguments converted during string formatting
举例2:>>>print("%f %d %E"% (12.01,12))
#传入参数不够
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
print("%f %d %E"% (12.01,12))
TypeError: not enough arguments for format string
2.4 几个进制装换的例子
print('Octal uses the letter "o" lowercase.%o'%10) ===>Octal uses the letter "o" lowercase.12 #十进制转换为八进制
print('Hex uses the letter "x" or "X" . %x %X'%(10,10)) ===>Hex uses the letter "x" or "X" . a A #十进制转换位十六进制