Python 基礎資料型別 (Elementary Data Type)

來源:互聯網
上載者:User

標籤:swap   邏輯   報錯   ali   32位   turn   支援   算數運算   最長公用子序列   

1、算數運算:

2、比較運算:

3、賦值運算:

4、邏輯運算:

5、成員運算:

   1、int

 

  Python2 int 有範圍,還有長整型
     Python3 所有的數字都是int 不管多長

  在32位機器上,整數的位元為32位,取值範圍為-2**31~2**31-1,即-2147483648~2147483647
  在64位系統上,整數的位元為64位,取值範圍為-2**63~2**63-1,即-9223372036854775808~9223372036854775807 
  • int  轉換成int類型,可以轉換的才可以如"123a",預設10進位
           a = "123"
           b = int(a)
           b = b + 1000

           num = ("0011")
           int(num,base=2) 2進位轉換

           num = "a"
           int(num, base=16) 16進位轉換
  • int.bit_length 當前數位二進位至少用幾位表示
  • Range(0,100)
          Python 2.7 立即在記憶體中建立
          Python 3 最佳化機制,for迴圈時,在記憶體中建立  2、布爾值 bool
  True or False  1 或 0 3、字串 str   字串一旦建立不可修改,任何修改都會建立新的字串副本
      需要記住的方法:  
  1. join() ‘ ’.join(字串)
  2. split()
  3. find()
  4. strip() 按照最長公用子序列
  5. upper()
  6. lower()
  7. 切片 按照索引取字元 字串[0],字串[0:2]
  8. len(字串) 字串有幾個字元
  9. 迭代 for 變數名 in 字串
  10. replace 替換 可以帶數字參數 替換幾次 字串.replace(老字串中字串,目標子字串,次數)
  
test = "zzHou"# 首字母大寫print(test.capitalize())
# 變小寫,casefold更厲害可以出來未知對應關係print(test.casefold())print(test.lower())
#center 設定寬度,並將內容置中 20代指寬度,*代指填充字元只能有一個#還有 左邊填充 ljust 右邊填充 rjust#zfill 只能用0填充print(test.center(20, "*"))
#去字串中尋找子序列出現的個數,起始位置5 結束位置6都是可選的print(test.count("zz",5,6))
#以什麼什麼結尾起始位置5 結束位置6都是可選的print(test.endswith("ou",5,6))
#以什麼什麼開頭起始位置5 結束位置6都是可選的print(test.startswith("zz",5,6))
#將tab符轉換成空格,預設是8個,返回當前字串的副本,先斷句,不足位的從前一位補足print("z\thou".expandtabs())print("123456\t789".expandtabs(6))print("1\t3456789".expandtabs(6))
#find 從頭開始找,找到返回索引,找不到返回-1.[2,8)之間且只找一次#index 找不到會報錯print(test.find("H",2,8))
#format 格式化,按順序或者名稱替換預留位置print("我是{name},age{a}".format(name="zhou", a=19))print("我是{0},age{1}".format("zhou", 19))
#format_map#format 使用字典進行格式化print("我是{name},age{a}".format_map({"name": ‘周航‘, "a": 19}))
#isalnum 是否是字母和數字print(test.isalnum())
#isalpha 是否是字母print("abc".isalnum())
isdecimal isdigit isnumeric是否是數字 isdigit更厲害比如“②” isnumeric 支援中文test = "123"v1 = test.isdecimal()v2 = test.isdigit()v3 = test.isnumeric()print(v1,v2)
isidentifier 是否是合法標示符a = "str"print(a.isidentifier())
# isprintable 是否存在不可顯示的字元 比如字串中的\tprint("org\tsss".isprintable())
print(" ".isprintable())
#isspace() 是否全部是空格#istitle() 是否是標題 每個單詞首字母都是大寫 可以跟title()連用print("ni shif eng re".title()) #Ni Shif Eng Reprint("Ni Shif Eng Re".istitle()) #True#*****************join() 字串每個字元按照指定分隔字元拼接******************print(" ".join("我我我歐文")) #我 我 我 歐 文print("&".join("我我我歐文")) #我&我&我&歐&文#islower() lower() 判斷是小寫 轉換成小寫test = "Zhou"v1 = test.lower()v2 = test.islower()print(v1,v2)#isupper() upper() 判斷是大寫 轉換成大寫#lstrip() rstrip() strip() 去除左(\t,\r)、右,全部空格 還可以加入參數,盡量多的匹配需要去除的字元


#S.strip([chars]) -> str

#Return a copy of the string S with leading and trailing
#whitespace removed.
#If chars is given and not None, remove characters in chars instead.

test = "9lexxexa"v = test.rstrip(‘xa‘)print(v)#str.maketrans("abc","123") abc和123 映射替換test = "testaasa"#partition() 按照字串分割成三份 test.partition("s") te s taasa#rpartition() 按照字串分割成三份 test.rpartition("s") testaa s a#split() test.split(‘s‘) te taa a 可以加入一個參數 指定分割次數,但是s不會留下 後面有Regex#rsplit()
#splitlines 按照分行符號分割print("a\nb\nc".splitlines(False)) # => ‘a‘, ‘b‘, ‘c‘print("a\nb\nc".splitlines(True)) # => ‘a\n‘, ‘b\n‘, ‘c‘#swapcase() 大小寫轉換

  

  

Python 基礎資料型別 (Elementary Data Type)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.