1. 程式人生 > >笨方法學習Python(1-10)

笨方法學習Python(1-10)

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 + e
This 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 
May
Jun 
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)