標籤:
1、Python優點
簡單,優雅,明確
強大的模組第三方庫
易移植
面向對角
可擴充
2、缺點
代碼不能加密
執行速度慢
3、變數定義
第一個字母必須是字母表中的大小寫,或底線。不能以數字為開頭。
1)變數賦值舉例
eg:
>>> x=123
>>> y=x
>>> id(x)
22582176
>>> id(y)
22582176
>>> x=100
>>> id(x)
22580736
>>> id(y)
22582176
>>> tast-date = 1
File "<stdin>", line 1
SyntaxError: can‘t assign to operator ; 不能以非大小寫底線定義變數
>>> tast_date = 1
2)變數實值型別
布爾型:true,false
eg:
If True: print ‘ddd’
ddd
整型,長整型,浮點型:
eg:
>>> type(a)
<type ‘long‘>
>>> a = 2**34
>>> type(a)
<type ‘int‘>
>>> a=1.34
>>> type(a)
<type ‘float‘>
>>> a=3
>>> type(a)
<type ‘int‘>
>>> b=2.3
>>> type(b)
<type ‘float‘>
字串:
>>> name=‘wang‘
>>> type(name)
<type ‘str‘>
序列類型:列表,數組……
>>> name_list=[‘wang‘,‘bai‘,‘gui‘]
>>> type(name_list)
<type ‘list‘>
4、運算
a) "/" 除法,預設取只為整,可跟小數點。
>>> 3/2
1
>>> 3.0/2
1.5
b) 取被除數
>>> 10//2
5
>>> 10//4
2
>>> 10//3
3
c) 加減乘除法
+= “c+=a等於c=c+a”
*= “c*=a等於c=c*a”
**= “c**=a等於c=c**a”
d) 與運算&:
10和15的與運算
1010 1111 è10
10 20
1010 10100 è
>>> 10 & 20
0
>>> 10 & 15
10
e) 或運算:
10 20
1010 10100 è 11110 30
>>> 10 | 20
30
5、注釋
單行注釋:#
多行注釋:三個章引號’’’ ‘’’或者三個雙引號””” “”” ,另一種也可做為格式化輸出
提示:單引號和雙引號無區別
6、理解字元編碼
三種字元級:ASSIC(預設一個位元組) Unicode(兩個位元組) UTF-8(可變位元組,漢字三個位元組)
概述:一個位元組8位 111111 256個字,兩個位元組16位 65536個字
1024位元組=1KB
1)(ACCIS)
一個位元組舉例:
>>> ord(‘a‘)
97 ; a對應8位的某幾位
>>> ord(‘A‘)
65 ;A對應8位的某幾位
相當於每個對應256裡個的各一位,一一對應。
2)UTF-8編碼:可變的
存英文用一個位元組存,漢字用三個位元組存。
>>> a=‘wang‘
>>> type(a)
<type ‘str‘>
>>> a=u‘wang‘
>>> type(a)
<type ‘unicode‘>
>>> a
u‘wang‘
>>> name=u‘王小哥‘
>>> type (name)
<type ‘unicode‘>
>>> name
u‘\u738b\u67cf\u8d35‘
>>> name = "王小哥"
>>> name
‘\xe7\x8e\x8b\xe6\x9f\x8f\xe8\xb4\xb5‘
>>> name = u"王小哥"
>>> name
u‘\u738b\u67cf\u8d35‘
3)unicode轉換成UTF-8
>>> name = u‘王小哥‘
>>> name
u‘\u738b\u67cf\u8d35‘
>>> name.encode(‘utf-8‘)
‘\xe7\x8e\x8b\xe6\x9f\x8f\xe8\xb4\xb5‘
4)UTF-8轉換成unicode
>>> wang="王小哥"
>>> wang
‘\xe7\x8e\x8b\xe6\x9f\x8f\xe8\xb4\xb5‘
>>> wang.decode(‘utf-8‘)
u‘\u738b\u67cf\u8d35‘
提示:
python系統裡預設是ASSIC碼編碼格式,對應一個位元組,所以在python裡面存中文,會有問題,應該轉換成UTF8編碼格式
#_*_ coding:utf-8 _*_
Name = u”中文”
Print name
提示:
系統中讀到記憶體裡預設是unicode格式,存到硬碟可以以UTF-8格式存,因為unicode預設都是以兩個位元組存的,占空間。
8、匯入模組
三種匯入方式:
1) import modulename
2) from module import sayHi
3) import moduleName as newname
eg:
Import sys
Print sys.argv
或
From sys import argv
Print argv
或
From sys import * ;不建議使用
或
Import multiprocessing as nulte
=========================
eg:
a) 調用系統命令
>>> import os
>>> os.system(‘df‘)
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18423556 1691736 15795936 10% /
tmpfs 405824 0 405824 0% /dev/shm
/dev/sda1 198337 29668 158429 16% /boot
0 ;預設會輸出返回上一條指令的傳回值
==> 將傳回值存入並輸出
>>> cru_dir = os.system(‘pwd‘)
/root/python/day01
>>> print cru_dir
0
b) 如何將輸出結果輸出?
倒入import commands模組
>>> import commands
>>> res = commands.getstatusoutput(‘pwd‘)
>>> res
(0, ‘/root/python/day01‘)
3) 倒入import sys模組
[[email protected] day01]# cat test1.py
import sys
print sys.argv
print sys.argv[2]
[[email protected] day01]# python test1.py a b c
[‘test1.py‘, ‘a‘, ‘b‘, ‘c‘]
b
9、使用者互動
1) raw_input
[[email protected] day01]# cat test2.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input(‘please input your name:‘)
age = raw_input(‘age:‘)
print name , age
[[email protected] day01]# cat test2.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input(‘please input your name:‘)
age = raw_input(‘age:‘) ;raw_input無論輸入任何都當字串來解釋
job = raw_input(‘job:‘) ;可通過int(raw_input(‘age:’)) 轉換成數字,或直接用input(‘age:’),注意:imput後面跟的是原生態,之前是什麼,就是什麼,定要指明是什麼類型等,不然會有錯誤。
salary = raw_input(‘salary:‘)
print ‘‘‘
name: %s
age : %s
job : %s ;%s代表字串,%d代表數字,%f代表浮點數
salary : %s
-----------------
‘‘‘ %(name,age,job,salary)
Imput舉例:
[[email protected] day01]# vim test2.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input(‘please input your name:‘)
age = input(‘age:‘)
job = raw_input(‘job:‘)
salary = raw_input(‘salary:‘)
print type(age)
print ‘‘‘
name: %s
age : %s
job : %s
salary : %s
-----------------
‘‘‘ %(name,age,job,salary)
[[email protected] day01]# python test2.py
please input your name:wangbaigui
age:28
job:it
salary:2w
<type ‘int‘>
name: wangbaigui
age : 28
job : it
salary : 2w
-----------------
[[email protected] day01]# vim test2.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
AGE = 28
name = raw_input(‘please input your name:‘)
age = input(‘age:‘)
job = raw_input(‘job:‘)
salary = raw_input(‘salary:‘)
print type(age)
print ‘‘‘
name: %s
age : %s
job : %s
salary : %s
-----------------
‘‘‘ %(name,age,job,salary)
[[email protected] day01]# python test2.py
please input your name:wangbaigui
age:AGE
job:it
salary:3w
<type ‘int‘>
name: wangbaigui
age : 28
job : it
salary : 3w
10、流程式控制制
1) if… else…舉例:
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input(‘please input your name:‘)
age = input(‘age:‘)
job = raw_input(‘job:‘)
salary = raw_input(‘salary:‘)
if age > 30:
meg = ‘you are so old...‘
elif age >20:
meg = ‘…’
else:
meg = ‘you are so yongest...‘
print ‘‘‘
name: %s
age : %d
job : %s
salary : %s
-----------------
%s
‘‘‘ % (name,age,job,salary,meg)
2) for迴圈
[[email protected] day01]# cat test4.py
#!/usr/bin/env ptyhon
#_*_ coding:utf-8 _*_
name = raw_input(‘please input your name:‘)
job = raw_input(‘job:‘)
salary = raw_input(‘salary:‘)
relea_age = 28
for i in range(10):
age = input(‘age:‘)
if age >30:
meg = "think small..."
elif age == 28:
meg = "good!,you are right."
break
else:
meg = "go to think"
print meg
print "you have only %s times to trye" %(9 - i)
print ‘‘‘
name: %s
age : %d
job : %s
salary : %s
-----------------
%s
‘‘‘ % (name,age,job,salary,meg)
3)while迴圈
[[email protected] day01]# cat test5.py
slect_num = input(‘which num you want:‘)
count = 0
while count < 100:
if count == slect_num:
print ‘you slect right:%s‘%(slect_num)
choice = raw_input(‘you want go on or contine (Y/N)‘)
if choice == ‘Y‘:
while True:
slect_num = input(‘which num you want agan:‘)
if slect_num <= count:
print "lookup alred past..,ples input newest num!"
else:
break
continue
else:
break
else:
print ‘lookup‘,count
count +=1
else:
print ‘Alread more then 100.. so Bye!‘
python 自學(一)