1. 程式人生 > 其它 >CMake基礎 第18節 Boost單元測試框架

CMake基礎 第18節 Boost單元測試框架

一、可變不可變型別

如果值改變,但是記憶體地址不變,證明就是在改變原值,即原值可變
如果值改變,但是記憶體地址也變了,證明不是在改變原值,而是產生了新的值,即原值不可變

二、數字型別int與float

1.定義:整型:age=10 # 本質age = int(10)

浮點型:salary=3000.3 # 本質salary=float(3000.3)

2.型別轉換: res = int(" 18 ") res = float(" 1.8 ")
print(res,type(res)) print(res,type(res))

3.使用:數學運算與比較運算

4.存一個值,不可變

三、字串

1.定義:在單引號\雙引號\三引號內包含一串字元

name = "egon" name = str("egon")

2.型別轉換

str資料型別轉換:可以把任意型別都轉換成字串型別

res = str([1,2,3])
print(res,type(res)) # "[1,2,3]"

3.使用

3.1優先掌握的操作

(1)、按索引取值(正向取+反向取) :只能取

msg="hello world"
print(msg[4])
print(msg[-1])

(2)、切片(顧頭不顧尾,步長):從一個大字串中拷貝出一個子字串

msg = “hello world”
res = msg[1:5:2]
res 
= msg[1:5] res = msg[::-1]

(3)、長度len

msg = 'hello world'
print(len(msg))

(4)、成員運算innot in

msg = 'hello world'
print('el' in msg)
print('el' not in msg)  # 推薦
print(not 'el' in msg)

(5)、移除空白strip

 5.1 括號內不指定字元,預設移除首尾空白字元(空格、\n、\t)
str1 = '  life is short!  '
str1.strip()  
life is short!

 5.2 括號內指定字元,移除首尾指定的字元
>>> str2 = '**tony**' >>> str2.strip('*') tony

name = '*****eg on****'
print(name.strip('*').replace(" ",''))

msg = "*(&%&%&_+)**&^e*gon*)(^%$$&$"
print(msg.strip('*()&%_+^%$'))

x = 'a c d e'
print(x.replace(' ','',1))
print(x.replace(' ',''))


案例
 inp_user = input("username>>>: ").strip()  # inp_user = "      egon"
 inp_pwd = input("password>>>: ").strip()
 if inp_user == "egon" and inp_pwd == '123':
    print('ok')
 else:
     print('no')

(6)、切分split:把一個字串按照某種分割符切分一個列表

 msg = "egon:18:male:10"
 res = msg.split(':')
 print(res)

res = msg.split(':',1)
print(res)
(6).1 把列表中的元素按照某種分隔符拼接成字串
info = ['egon', '18', 'male', '10']
msg = ":".join(info)
print(msg)

(7)、迴圈

msg = "hello world"
for i in msg:
     print(i)

3.2 需要掌握的操作

 1、strip,lstrip,rstrip
 print("******egon*****".strip('*'))
 print("******egon*****".lstrip('*'))
 print("******egon*****".rstrip('*'))

 2、lower,upper
 print("AbC".lower())
 print("AbC".upper())

 3、startswith,endswith
 print('hello world'.startswith('he'))
 print('hello world'.endswith('d'))

 print('hello world'.startswith('h'))
 print('hello world'.startswith('e',1,4))


 4、format的三種玩法
 print("my name is %s my age is %s" % ('egon', 18))
 print("my name is {name} my age is {age}".format(age=18,name='egon'))

 print("my name is {} my age is {}".format(18,'egon'))
 print("my name is {1} my age is {0}{0}{0}".format(18,'egon'))


 x = 'egon'
 y = 18
 print(f'my name is {x} ,my age is {y}')

 5、split,rsplit
 msg = 'egon:18:male'
 print(msg.split(':',1))
 print(msg.rsplit(':',1))


 6、join
 7、replace
 8、isdigit
 print("18".isdigit())

 age = input('>>>: ').replace(' ', '')
if age.isdigit():
     age = int(age)
     if age > 18:
         print('too big')
     elif age < 18:
         print('too small')
     else:
         print('you got it')
else:
     print("必須輸入數字,小垃圾")

3.3需要了解的操作

1、find,rfind,index,rindex,count
msg = 'hello egon ahahah egon xxx egon'
print(msg.find('egon1'))   在索引中找egon1,找不到顯示-1(false)
print(msg.index('egon1'))   在索引中找egon1,同find,但在找不到時會報錯

print(msg.rfind('egon'))


2、center,ljust,rjust,zfill
print('egon'.center(50,'*'))
print('egon'.ljust(50,'*'))
print('egon'.rjust(50,'*'))

print('egon'.rjust(50,'0'))
print('egon'.zfill(50))


4、captalize,swapcase,title
 print('abcd'.capitalize())
 print('AbCd'.swapcase())
 print('my name is egon'.title())

5、is數字系列
# print('18'.isdigit())
# print('Ⅳ'.isnumeric())

6、is其他
name='egon'
print(name.isalnum()) #字串由字母或數字組成
print(name.isalpha()) #字串只由字母組成

print(name.islower())#將英文字串全部變小寫
print(name.isupper())#將英文字串全部變大寫

 name=' '
print(name.isspace())

name = 'My Is Egon'
print(name.istitle())  #每個單詞的首字母大寫

該型別總結:存一個值,有序,不可變

四、列表

1.定義方式:在[]用逗號分隔開多個任意型別的元素
l = [111,33.333,"aaaa",[666,7777]] # l = list(...)
print(type(l))
2.list資料型別轉換

res = list("hello")
print(res)

3.常用操作+內建的方法

3.1優先掌握的操作:



1、按索引存取值(正向存取+反向存取):即可存也可以取
  l = [111,222,333,444,555,666,777]
  print(l[0])
  print(l[-1])
  print(id(l))
  l[0] = 999
  print(id(l))

  l[7] = 888  # 如果索引不存在則報錯

 2、切片(顧頭不顧尾,步長)
   l = [111,222,333,444,555,666,777]
   print(l[1:4:1])  # 拷貝

   print(l[:])
   print(l[::-1])

 3、長度
   l = [111,222,333,444,555,666,777]
   print(len(l))

 4、成員運算in和not in
  l = [111,222,333,444,555,666,777]
  print(333 in l)

 5、追加、插入
   l = [111,222,333,444,555,666,777]
   l.append(888)

  l.insert(3,999999999999)
  print(l)


 6、刪除
  l = [111, 222, 333, 444, 555, 666, 777]
 (1)萬能刪除
   del l[1]
   print(l)

  (2) l.remove()
   v = l.remove(333)
   print(l)
   print(v)

 (3) l.pop()
   v = l.pop(2)
   print(l)
   print(v)

  7、迴圈
   l = [111,222,333,444,555,666,777]
   for i in l:
       print(i)
3.2需要掌握


 l = [111,222,333,444,333,555,666,777]
 print(l.index(333))
 print(l.index(9999))

 print(l.count(333))

 l = [111,'a',333,'dddd']
 l.reverse()
 print(l)

 l = [111,'a',333,'dddd']
 l = [111, -3, 99, 27]
 l.sort(reverse=True)
 print(l)

 l = [111, -3, 99, 27]
 nums = [333,444,555]
 l.append(nums)
 l.extend(nums)
 print(l)



 print(len(l))  # l.__len__()

 new_l = l.copy()
 new_l = l[:]

五、元組

1、用途:元組就是一種不可變的列表
2.定義方式:在()內用逗號分割開多個任意型別的元素
 t = (11,11.333,"aaa",[666,777])  # t = tuple()
 print(type(t))

 t[0] = 999

 t = (1111,)
 print(type(t))

3.tuple資料型別轉換
print(tuple("hello"))

4.常用操作+內建的方法

4.1優先掌握的操作:

1、按索引取值(正向取+反向取):只能取
2、切片(顧頭不顧尾,步長)
3、長度
4、成員運算in和not in

4.2需要了解的

t = (111,222,222,222,333)
print(t.count(222))
print(t.index(222))

元組總結:元組是可以存多個值、有序、不可變的