笨方法學習Python(1-10)

來源:互聯網
上載者:User

標籤:python   基礎   笨方法學習python   

以下學習內容以python2為基準

UTF-8

#conding:utf-8    or    #__coding:utf-8__

此句要置頂,表示代碼支援UTF8的格式,最好每個代碼檔案都加上


注釋

# A comment, this is so you can read your program later.

代碼前加“#”表示的是注釋,以後寫每行代碼的上一行記得都加上解釋資訊


python2與python3

print“abc”    #python2的寫法print ("abc")    #python3的寫法


數字和數學計算

+    plus 加號

-    minus 減號

/    slash 斜杠

*    asterisk 星號

%    percent 百分比符號

<    less-than 小於符號

>    greater-than 大於符號

<=    less-than-equal 小於等號

>=    greater-than-equal 大於等於符號


變數和命名

>>> cars = 100>>> print "There are", cars, "cars available."

結果: There are 100 cars available.


更多的變數和列印

>>> my_name = 'Kavin'>>> print "Let's talk about %s." % my_name

結果

>>>  Let's talk about Kavin.

python中的%r和%s

%r用rper()方法處理對象

%s用str()方法處理對象

有些情況下,兩者處理的結果是一樣的,比如說處理int型對象

%r會在字串兩側多出‘ ’


字串(srring)和文本

w = "This is the left side of..." e = "a string with a right side."print w + eThis is the left side of...a string with a right side.print "." * 10..........


列印

days = "Mon Tue Wed Thu Fri Sat Sun" months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" print "Here are the days: ", days print "Here are the months: ", months print """ There's something going on here. With the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6. """$ python ex9.py Here are the days: Mon Tue Wed Thu Fri Sat Sun Here are the months: Jan Feb Mar Apr MayJun Jul Aug There's something going on here. With the three double-quotes. We'll be able to type as much as we like. Even 4 lines if we want, or 5, or 6.

\n 強制換行

print  """        """    按照既定的格式顯示

'''  ''' 也可使用單引號

print "",abc      加逗號,逗號後面的不換行


那是什麼

\    轉義符,可將難列印出來的字元放到字串

\t \r \n都是逸出字元,空格就是單純的空格,輸入時可以輸入空格

\t 的意思是 橫向跳到下一定位字元位置

\r 的意思是 斷行符號

\n 的意思是斷行符號換行







笨方法學習Python(1-10)

聯繫我們

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