Python基礎2

來源:互聯網
上載者:User

標籤:long   test   isa   序列   linu   mat   use   執行   center   

安裝pycharm專業版,不要漢化

要想寫的代碼支援linux和2.0版本需要在開頭加上注釋

#/usr/bin/env python

#-*- coding:utf-8 -*-

運算子

  結果是值

    算數運算

      a = 10*10

    賦值運算

      a = a+1    即a+=1

  結果是布爾值(返還真或者假)

    比較運算

      a = 1>5

    邏輯運算

      a = 1>6 or 1==1

    成員運算

      a  = "tom" in "l"

基礎資料型別 (Elementary Data Type)(不全,後面會有補充)

  數字

 name = 5 v= name.bit_length()  #用二進位表示的時候有幾位 print(v)v = int("1101",base=2) #把1101轉換成2禁止  “a” base=16 把a轉換成16進位 print(v) #在python3裡面int可以代表所有數字不管多長而在python2裡面超過多少位之後就會變成long

  字串

name = "alex"v= name.capitalize() #首字母變成大寫print(v)name = "tom"v = name.find("t",) #尋找alex中a所在的位置。print(v)name = "aLeA"v=name.casefold() #casefold 所有的字母變成小寫 capitalize也是所有字母變成小寫 但是前者更牛逼print(v)  #後者只能識別自己國家的前者可以識別所有name1="tom"v1=name1.upper() #upper 全部變成大寫print(v1)test = "tom"v= test.center(20,"中") #center 代表填充 20代表總長度 中代表周圍都是中字 單只能是一個字元print(v)name = "tom"v= name.count("t",0,2)  #去字串中尋找子序列出現的次數 0 2 代表在範圍內print(v)name = "tom"v= name.endswith("m")  #以m結尾 返回ture值v= name.startswith("t") #以t開頭 翻譯ture值print(v)name = "i am {name1},age{a}"v= name.format(name1 = "tom",a=23) #將name1 ,a替換為指定的值print(v)name = "!123tom"v = name.isalnum()  #檢查字串中是否只包含數字 字母 單獨出現也可以 返回的值是ture 或者falseprint(v)
#name = "tom"# if "t" in name:  如果t在name裡 執行1 不在執行2#     print("1")# else:#     print("2")# if "t" not in name: 如果t不在name裡 執行1 不在執行2#     print("1")# else#     print("2")# user = "tom"# pwd = "123"# v = user=="tom" and pwd =="123" or 1==1 and pwd =="456" and 1==2#等於Ture 如果and之間成立就是trub# print(v)

 

Python基礎2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.