標籤:empty idt height indicator image table 資料類型 32位 元素
運算子1、算數運算:
2、比較運算:
3、賦值運算:
4、邏輯運算:
5、成員運算:
基礎資料型別 (Elementary Data Type)1、數字
int(整型)
在32位機器上,整數的位元為32位,取值範圍為-2**31~2**31-1,即-2147483648~2147483647
在64位系統上,整數的位元為64位,取值範圍為-2**63~2**63-1,即-9223372036854775808~9223372036854775807 int2、布爾值 真或假 1 或 03、字串
"hello world"
字串常用功能:
str4、列表建立列表:
| 123 |
name_list = [‘alex‘, ‘seven‘, ‘eric‘]或name_list = list([‘alex‘, ‘seven‘, ‘eric‘]) |
基本操作:
list5、元祖建立元祖:
| 123 |
ages = (11, 22, 33, 44, 55)或ages = tuple((11, 22, 33, 44, 55)) |
基本操作:
lass tuple(object): """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable‘s items If the argument is a tuple, the return value is the same object. """ def count(self, value): # real signature unknown; restored from __doc__ """ T.count(value) -> integer -- return number of occurrences of value """ return 0 def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ T.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def __add__(self, y): # real signature unknown; restored from __doc__ """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__(‘name‘) <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __getnewargs__(self, *args, **kwargs): # real signature unknown pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __hash__(self): # real signature unknown; restored from __doc__ """ x.__hash__() <==> hash(x) """ pass def __init__(self, seq=()): # known special case of tuple.__init__ """ tuple() -> empty tuple tuple(iterable) -> tuple initialized from iterable‘s items If the argument is a tuple, the return value is the same object. # (copied from class doc) """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> x<y """ pass def __mul__(self, n): # real signature unknown; restored from __doc__ """ x.__mul__(n) <==> x*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __rmul__(self, n): # real signature unknown; restored from __doc__ """ x.__rmul__(n) <==> n*x """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ T.__sizeof__() -- size of T in memory, in bytes """ pass
6、字典(無序)建立字典:
| 123 |
person = {"name": "mr.wu", ‘age‘: 18}或person = dict({"name": "mr.wu", ‘age‘: 18}) |
常用操作:
dictPS:迴圈,range,continue 和 break其他1、for迴圈使用者按照順序迴圈可迭代對象中的內容,PS:break、continue
| 123 |
li = [11,22,33,44]for item in li: print item |
2、enumrate為可迭代的對象添加序號
| 123 |
li = [11,22,33]for k,v in enumerate(li, 1): print(k,v) |
3、range和xrange指定範圍,產生指定的數字
| 12345678 |
print range(1, 10)# 結果:[1, 2, 3, 4, 5, 6, 7, 8, 9] print range(1, 10, 2)# 結果:[1, 3, 5, 7, 9] print range(30, 0, -2)# 結果:[30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2] |
練習題
一、元素分類
有如下值集合 [11,22,33,44,55,66,77,88,99,90...],將所有大於 66 的值儲存至字典的第一個key中,將小於 66 的值儲存至第二個key的值中。
即: {‘k1‘: 大於66的所有值, ‘k2‘: 小於66的所有值}
二、尋找尋找列表中元素,移除每個元素的空格,並尋找以 a或A開頭 並且以 c 結尾的所有元素。 li = ["alec", " aric", "Alex", "Tony", "rain"] tu = ("alec", " aric", "Alex", "Tony", "rain") dic = {‘k1‘: "alex", ‘k2‘: ‘ aric‘, "k3": "Alex", "k4": "Tony"} 三、輸出商品列表,使用者輸入序號,顯示使用者選中的商品 商品 li = ["手機", "電腦", ‘滑鼠墊‘, ‘遊艇‘] 四、購物車
功能要求:
- 要求使用者輸入總資產,例如:2000
- 顯示商品列表,讓使用者根據序號選擇商品,加入購物車
- 購買,如果商品總額大於總資產,提示賬戶餘額不足,否則,購買成功。
- 附加:可儲值、某商品移除購物車
| 123456 |
goods = [ {"name": "電腦", "price": 1999}, {"name": "滑鼠", "price": 10}, {"name": "遊艇", "price": 20}, {"name": "美女", "price": 998},] |
五、使用者互動,顯示省市縣三級聯動的選擇
| 12345678910111213 |
dic = { "河北": { "石家莊": ["鹿泉", "槁城", "元氏"], "邯鄲": ["永年", "涉縣", "磁縣"], } "河南": { ... } "山西": { ... } } |
Python開發【全棧-DAY-IV】:Python基礎資料型別 (Elementary Data Type)