数值类型
1.整形
Python 2.7.5 (default, Feb 11 2014, 07:46:25)
>>> aint=3
>>> type(aint)
Python 3.6.4 (default, Aug 6 2018, 22:54:20)
>>> aint=3
>>> type(aint)
2.长整形
python2#有长整形
>>> along=1728219876932764873264934692
>>> type(along)
python3#没有长整形
>>> along=1728219876932764873264934692
>>> type(along)
3.浮点型
1.344,12000000=12e6=1.2e7=0.12e7,0.012=1.2e-2
python2
>>> afloat=12e3
>>> afloat
12000.0
>>> type(afloat)
python3
>>> afloat=12e3
>>> type(afloat)
4.复数类型
acomplex=3j+8
type(acomplex)
查看帮助:可以使用什么方法,实现什么功能?
help(acomplex)
dir(acomplex)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']
>>> acomplex.conjugate() #共轭
(8-3j)
>>> acomplex.imag #虚部
3.0
>>> acomplex.real #实部
8.0
初学阶段,避免使用"__"开头的内置功能
5.字符串数据类型
>>> astring="hello"
>>> type(astring)
>>> dir(astring)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> help(astring.center)
>>> astring.center(40,"*")
'*****************hello******************'
>>> print("学生管理系统".center(40,'*'))
***********学生管理系统***********
数据类型的转换
-在python中,所有的数据类型都可以作为内置函数,用来转换数据类型:
>>> int(3.10)
3
>>> str(3)
'3'
>>> type(str(3))
>>> float(3)
3.0
>>> complex(2)
(2+0j)
如何删除内存中的变量
>>> afloat=2e12
>>> del afloat
>>> afloat
Traceback (most recent call last):
File "", line 1, in
NameError: name 'afloat' is not defined
布尔数据类型
bool:只有两个值(True,Flase)
>>>a = 1
>>> bool(a)
True
>>> bool(0)
False
>>> name = 'we'
>>> bool(a)
True
运算符
1.算术运算符
+,-,,*,/,%,//
2.赋值运算符
=,+=,-=,/=,%=,*=
3.关系运算符
,>=,
4.逻辑运算符
逻辑与and
逻辑或or
逻辑非not
python2
>>> 5/2
2
>>> 100/300
0
>>> 5/2.0
2.5
>>> 100/300.0
0.3333333333333333
>>> from __future__ import division
>>> 5/3
1.6666666666666667
python3
vim empty.py
#判断输入为空时,输出"Error"
value=input("请输入:")
if not value:
print("Erros")