# coding=utf-8
"""
多行注釋
"""
from collectionsimport defaultdict
# 基本數(shù)據(jù)類型**************************************************
counter =100? # 整型
miles =1000.0? # 浮點(diǎn)型
integer00 =int(0)
integer11 =2/3? ? # int:0(整除) (python2) float:0.666666 (python3)
float00 =2/3.0? ? # float:0.666666
integer22 =9//2? ? # int:4(整除脖捻,向下取整满哪,4.5 -> 4)
integer33 = -9//2? # int:-5(整除,向下取整赞季,-4.5 -> -5)
integer44 =9 %7? # int:2(余數(shù))
integer55 =2 **4? # int:16(2的4次方)
integer66 =33? ? ? # 33 原來是integer (變量可以改變類型)
integer66 =33.3? ? # 33.3 變成float
isBool1 =True and False? # False
isBool2 =True or False? # True
isBool3 =not True? ? ? ? # False
isBool4 =not 10? # bool False( 非0 相當(dāng)于 True)
isBool5 =not 0? ? # bool True ( 0 相當(dāng)于 False)
isBool55 =not 1? # bool False
isBool56 =not -1? # bool False
isBool57 =not 23? # bool False
isBool58 =not -15 # bool False
x, y =10, 20
iValue4 = xand y# int 20? 如果 x 為 False,x and y 返回 False奶段,否則它返回 y 的計(jì)算值微峰。
iValue5 =0 and y# int 0
iValue6 = xor y# int 10? 如果 x 是非 0,它返回 x 的值氓奈,否則它返回 y 的計(jì)算值翘魄。
iValue7 =0 or y# int 20
isBool66 =not "yyy"? # bool False
isBool67 =not " "? ? # bool False
isBool68 =not "\n"? # bool False
isBool69 =not "\t"? # bool False
isBool70 =not ""? ? # bool True
str11 ="xxx" and "yyy"? # 'yyy'? # 串1 and 串2 :串1=空,返回空舀奶,否則返回串2
str22 ="" and "yyy"? ? # ''
str33 ="xxx" or "yyy"? # 'xxx'? # 串1 or 串2 :串1=空暑竟,返回串2,否則返回串1
str44 ="" or "yyy"? ? ? # 'yyy'
isBool88 =type(counter) ==int? # bool True, 判斷變量類型
integer5 =16
isBool99 =10 < integer5 <20? ? # bool True
print(int(1.0 +2))
print(float(1 +2))
# 字符串**************************************************
name11 ="Jason\""
name12 ='Jason\"'? # '' === "" 單引號(hào)雙引號(hào)無區(qū)別
name22 ='Jason' +"Xu"
name33 ='zh' *5
iVar =int("99")# string -> int
strVar =str(88)# int -> string
isBool9 = strVar.isdigit()# True
len11 =len('a')# 1
len12 =len('aa')# 2
len13 =len("aaa")# 3
str1 ='012345'
h11 = str1[1]# h11 = 1
h21 = str1[-1]# h21 = 5? ? #-1代表字符串末尾:3=-3 4=-2 5=-1
h31 = str1[1:4]# h31 = 123? #不包括 4
h41 = str1[-3:-1]# h41 = 34? ? #不包括 -1
h42 = str1[-3:]# h42 = 345
h51 = str1[:-1]# h51 = 01234 #不包括 -1
h61 = str1[:5]# h61 = 01234 #不包括 5
h71 = str1[4:]# h71 = 45
h81 = str1[-3:]# h81 = 345
# [beg:end:step]
h11 = str1[1:5:2]# h11 = 13? ? ? #不包括 5
h22 = str1[1::2]# h22 = 135
h33 = str1[::1]# h33 = 012345
h44 = str1[::-1]# h44 = 543210? #順序顛倒
h55 = str1[::2]# h55 = 024
h66 = str1[::-2]# h66 = 531? ? #順序顛倒 間隔為2
str222 ="str"
str333 ="str1"
str444 ="str"
isEqual1 = str222 == str333# False
isEqual2 = str222 == str444# True
text ="Game #Over #Game Over "? # 默認(rèn)以空字符分割,包括空格但荤、換行(\n)罗岖、制表符(\t)等
wordList1 = text.split()# wordList1 = : ['Game', '#Over', '#Game', 'Over']
wordList2 = text.split("#")# wordList2 = : ['Game ', 'Over ', 'Game Over ']
oneOne =11
twoTwo =22
pi =3.141592653
str111 =f"{oneOne} one, {twoTwo} two"? ? ? ? ? # '11 one, 22 two'
str222 =f"{oneOne:.1f} one, {twoTwo:.1f} two"? # '11.0 one, 22.0 two'
str333 =f"pi = {pi:.6f} != {22/7:.5f}"? ? ? ? # 'pi = 3.141593 != 3.14286'? 4舍5入
str777 ="{}".format("swedish"[-2:])# sh
str888 ="{}{}".format("swedish"[-2:], "beet"[1:3])# shee
str999 ="{}{}{}".format("swedish"[-2:], "beet"[1:3], "swedish"[-2:])# sheesh
# 元組 一旦創(chuàng)建不可以修改其中元素,也不能單獨(dú)刪除一個(gè)元素 **************************************************
tuple1 = ()
tuple1 = (50,)# 元組中只包含一個(gè)元素時(shí)腹躁,需要在元素后面添加逗號(hào)
tuple1 = ('runOob', 786, 2.23, True)# 可以改變整個(gè) tuple1 的值
tuple1 = tuple1 + (60,)# 可以增加一個(gè)元素
# tuple1[0] = 456 錯(cuò)誤 # 不能改變?cè)氐闹?/p>
intTuple0 = tuple1[1]# 786, 訪問元組元素
tuple1 = (123, ('runOob', "786"), 456)
strTuple = tuple1[1][1]# "786" 訪問元組中桑包,元素為元組中的元素
# 列表钢猛,可以有重復(fù)值赌躺,每個(gè)元素類型可以不一致, 是一種可變序列 **************************************************
list00 =list()
list00.append('item0')
list11 =list()
list11.append((4, 'item0'))# (4, 'item0') 是一個(gè)元組類型的元素
list11.append('item1')
list11.append('item2')
del list11[1]# 刪除第 1 個(gè)元素
valueList = list11.pop()# item1 刪除最后一個(gè)并返回給 valueList
list11.clear()# 刪除所有
list1 = ['runOob', 786, 2.23, 'john', 70.2]
tinyList = [123, 'john']
print(list1)# 輸出完整列表
print(list1[0])# 輸出列表的第 0 個(gè)元素
print(list1[1:3])# 輸出第 1 個(gè)至第 2 個(gè)元素谦絮,不包括第 3 個(gè)元素
print(list1[2:])# 輸出從第 2 個(gè)開始至列表末尾的所有元素
print(tinyList *2)# 輸出列表兩次
print(list1 + tinyList)# 打印組合的列表
list44 = [786, 2.2, 70.3]
list55 =sorted(list44)# list55 = [2.2, 70.3, 786] 排序
list66 = ((786, 2), (2.2, 1), (70.3, 3))
list77 =sorted(list66)# list77 = [(2.2, 1), (70.3, 3), (786, 2)] 按照元組第 0 個(gè)元素排序
list88 =sorted(list66, reverse=True)# list88 = [(786, 2), (70.3, 3), (2.2, 1)] 倒序