python_bomb----數據類型

數值類型

1.整形

Python 2.7.5 (default, Feb 11 2014, 07:46:25) 
>>> aint=3
>>> type(aint)
<type 'int'>


Python 3.6.4 (default, Aug  6 2018, 22:54:20) 
>>> aint=3
>>> type(aint)    
<class 'int'>

2.長整形

python2#有長整形
>>> along=1728219876932764873264934692
>>> type(along)
<type 'long'>


python3#沒有長整形
>>> along=1728219876932764873264934692
>>> type(along)
<class 'int'>

3.浮點型

1.344,12000000=12e6=1.2e7=0.12e7,0.012=1.2e-2python

python2
>>> afloat=12e3
>>> afloat
12000.0
>>> type(afloat)
<type 'float'>

python3
>>> afloat=12e3
>>> type(afloat)
<class 'float'>

4.複數類型

acomplex=3j+8
type(acomplex)
<type 'complex'>

查看幫助:可使用什麼方法,實現什麼功能?

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']git

>>> acomplex.conjugate()    #共軛
(8-3j)
>>> acomplex.imag    #虛部
3.0
>>> acomplex.real    #實部
8.0

初學階段,避免使用"__"開頭的內置功能
圖片描述
圖片描述vim

5.字符串數據類型

>>> astring="hello"
>>> type(astring)
<type 'str'>
>>> 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中,全部的數據類型均可以做爲內置函數,用來轉換數據類型:api

>>> int(3.10)
3
>>> str(3)   
'3'
>>> type(str(3))
<class 'str'>
>>> float(3)
3.0
>>> complex(2)
(2+0j)

圖片描述

如何刪除內存中的變量

>>> afloat=2e12
>>> del afloat
>>> afloat
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'afloat' is not defined

圖片描述

布爾數據類型

bool:只有兩個值(True,Flase)ssh

>>>a = 1
>>> bool(a)
True
>>> bool(0)
False
>>> name = 'we'
>>> bool(a)
True

圖片描述

運算符

1.算術運算符

+,-,,*,/,%,//函數

2.賦值運算符

=,+=,-=,/=,%=,*=spa

3.關係運算符

,>=,<,<=,!=,==

4.邏輯運算符

邏輯與and
邏輯或or
邏輯非not3d

python2code

>>> 5/2
2
>>> 100/300
0

>>> 5/2.0
2.5
>>> 100/300.0
0.3333333333333333

>>> from __future__ import division
>>> 5/3
1.6666666666666667

圖片描述

python3orm

vim empty.py

#判斷輸入爲空時,輸出"Error"
value=input("請輸入:")
if not value:
    print("Erros")

圖片描述

相關文章
相關標籤/搜索