python基本概念入門篇

來源:互聯網
上載者:User

Python中有4種類型的數

1. 整數

例如: 2, 10 等

2. 長整數

比整數儲存的數值更大

3. 浮點數

即小數, 例如 2.5, 12.3E-4,  12.3E-4表示12.3 * 10-4。

4. 複數

例如 (-1+2j)和(3.4-5.6j)


一、字串的三種標記法

  1、使用單引號(')

    例如:

print 'hello world'
  2、使用雙引號(")

    例如:

print "hello world"
  3、使用三引號('''或"""), 三引號可以包含多行字串, 並簽中間可以任意使用單引號或雙引號。

    例如:

print '''hello world, "this's ishello word."可以是多行'''print """hello world, "this's ishello word."可以是多行"""
二、逸出字元的使用

  如果想在單引號裡使用單引號, 或是雙引號裡使用雙引號,那麼可以通過逸出字元來實現, '代表單引號, "代表雙引號,\代表反斜線, 在行末使用代表續行, 即下一行與所在的行為同一個字串.

  例如:

print 'this's hello world'輸出結果是this's hello world
以上代碼也可以表示為

print "this's hello world"
使用換行的例子

print "hello world, this's same line with hello world"輸出結果為:hello world, this's same line with hello world
三、自然字串

  使用r或R標識後邊的字串中不使用逸出字元, 常用於Regex.

例如:

print r"n想輸出反斜線後邊跟一個n"輸出結果為:n
四、Unicode字串

  使用u或U標識後邊的字串使用Unicode編碼

五、字串串連

  將兩個字串放到一起, 將自動連接, 字串之間可以有空格.

  例如:

print 'hello''world'輸出結果是: helloworld
六、注意事項和建議

1 單引號和雙引號沒有任何區別2 當使用Regex時, 建議使用自然字串, 即用r或R後邊跟一個字串3 當有單引號和雙引號混用的情況時, 建議使用三引號


python 學習筆記 標識符和變數(3)
一、標識符

  合法的標識符: 必須由字母、數字、底線組成並且以字母或底線開頭。

  標識符是大小寫敏感的。

  標識符不可以是python的關鍵字和保留字

  例如:以下的標識符都是有效標識符

mynamemyName_myName__myNamemyName8
  其中第一個變數和第二個變數是不同的變數, 因為第三個字母一個是小寫'n', 一個是大寫'N'.

二、變數

  在python中變數不需要定義, 直接使用即可

  例如: 以下內容可以使用python正確運行

i = 0i = i + 1print i
  以上代碼運行結果為: 1

 

python 學習筆記 對象(4)
python中任何東西都是對象.

python對象包括數字, 字串, 函數

python 學習筆記 物理行與邏輯行(5)
一、物理行

  在編寫代碼的過程中看到的每一行都是一個物理行

二、邏輯行

  在python語言中的一個邏輯語句。每個邏輯行可以使用分號(;)表示結束。

  例如:

i = 5print ii = i + 5;print 5;
  以上代碼有3個物理行, 4個邏輯行.

強烈建議每一個邏輯行佔用一個物理行


python 學習筆記 縮排(6)
python語言中在行首的空格和定位字元用來決定邏輯行的縮排和層次.

如果同一層次的縮排不一致的話, 會產生異常或錯誤的結果.

例如:

print 'hello world' print 'hello world'
  以上代碼中第一行代碼會正常運行, 第二行會拋出異常.

1 i = 32 if i == 3:3     print 'hello world i == 3'4     print 'hello world'5 if i < 3:6     print 'hello world i < 3'7 print 'Done'
  以上代碼中, 如果i為3則列印輸出第3,4,7行
  如果 i<3時, 則列印輸出6,7行
  

從以上代碼可以看到 相同縮排下的代碼會在同一個語句塊中.相同縮排下的代碼類似於java中的一對大括弧中的內容

聯繫我們

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