Python學習筆記(1)
本節內容
1.hello word
2.變數
3.使用者輸入
4.模組初識
5.資料型別初識
6.資料運算
7.表示式if.....else語句
8.while迴圈
9.for迴圈
10.break 和 continue
一、hello word
在linux下建立字尾檔案為xxx.py
#!/usr/bin/env python #指定語言
print("hello word") #列印結果;注意需要有引號
python支援中文
單行註釋:#註釋的內容
多行註釋:'''註釋內容''' '''註釋內容'''
二、變數
宣告變數:
宣告一個變數;變數為name,變數name的值為“hanyunhui”
#_*_coding:utf-8_*_
name = "hanyunhui"
print(name) #打印出結果;呼叫變數
變數定義的規則:
- 變數名只能是字母、數字或下劃線的任意組合
- 變數名的第一個字元不能是數字
- 以下不能宣告為變數:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
變數的賦值
1 name = "1" 2 name2 = name 3 4 name = "2" 5 6 print(name,name2) #呼叫多個變數中間用逗號隔開;
執行出結果為:2 1
三、使用者輸入
input
#!/usr/bin/env python #_*_coding:utf-8_*_ (python 2 需要新增字串編碼) name = input("what is your name:") print(name) #打印出結果
輸入密碼時,如果想不被看見,需要利用getpass 模組中getpass方法,即:
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import getpass 5 6 # 將使用者輸入的內容賦值給 name 變數 7 pwd = getpass.getpass("請輸入密碼:") 8 9 # 列印輸入的內容 10 print(pwd)
四、模組初識
Python的強大之處在於他有非常豐富和強大的標準庫和第三方庫,幾乎你想實現的任何功能都有相應的Python庫支援,以後的課程中會深入講解常用到的各種庫,現在,我們先來象徵性的學2個簡單的。
sys
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import sys 5 6 print(sys.argv) 7 #顯示的結果為:執行的過程
os
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import os 5 6 os.system("df -h") #呼叫系統命令
結合使用:
1 import os,sys 2 3 os.system(''.join(sys.argv[1:])) #把使用者的輸入的引數當作一條命令交給os.system來執行
五、資料型別初始
1、數字2 是一個整數的例子。
長整數 不過是大一些的整數。
3.23和52.3E-4是浮點數的例子。E標記表示10的冪。在這裡,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是複數的例子,其中-5,4為實數,j為虛數,數學中表示覆數是什麼?。
int(整型)
在32位機器上,整數的位數為32位,取值範圍為-2**31~2**31-1,即-2147483648~2147483647在64位系統上,整數的位數為64位,取值範圍為-2**63~2**63-1,即-9223372036854775808~9223372036854775807 long(長整型)
跟C語言不同,Python的長整數沒有指定位寬,即:Python沒有限制長整數數值的大小,但實際上由於機器記憶體有限,我們使用的長整數數值不可能無限大。
注意,自從Python2.2起,如果整數發生溢位,Python會自動將整數資料轉換為長整數,所以如今在長整數資料後面不加字母L也不會導致嚴重後果了。
float(浮點型) 浮點數用來處理實數,即帶有小數的數字。類似於C語言中的double型別,佔8個位元組(64位),其中52位表示底,11位表示指數,剩下的一位表示符號。
complex(複數)
複數由實數部分和虛數部分組成,一般形式為x+yj,其中的x是複數的實數部分,y是複數的虛數部分,這裡的x和y都是實數。 注:Python中存在小數字池:-5 ~ 257 2、布林值 真或假 1 或 0 3、字串
"hello world"
字串拼接:
python中的字串在C語言中體現為是一個字元陣列,每次建立字串時候需要在記憶體中開闢一塊連續的空,並且一旦需要修改字串的話,就需要再次開闢空間,萬惡的+號每出現一次就會在內從中重新開闢一塊空間。
PS:字串:%s 整數為:%d 浮點數:%f
print(type(某個型別))列印字串型別
1 name = input("name:") 2 age = input("age:") 3 4 info = ''' 5 ----- info of %s ---- 6 name:%s 7 age:%s 8 '''%(name,name,age) 9 print(info) #顯示結果為:name:輸入結果 age:輸入結果
1 name = "HYH" 2 3 print ("i am %s" % name) #執行結果為:i am HYH 4 5 print(type(name)) #打印出name顯示的字串型別
字串功能:
移除空白、分割、長度、索引、切片
4、列表
基本操作:
索引、切片、追加、刪除、長度、迴圈、包含
建立列表:
1 name_list = list(['na','me','name']) 2 或 3 name_list = ['na','me','name']
5、元組(不可變列表)
建立元組:
1 ages = (11,22,33,44) 2 ages = tuple((11,22,33,44))
6、字典(無序)
常用操作:
索引、新增、刪除、鍵 值 鍵值對、迴圈、長度
建立字典:
1 person = {"name":"mr.wu",'age':18} 2 person = dict({"name":"mr.wu",'age':18}) 3 #print(person) 執行出結果為{"name":"mr.wu",'age':18}
六、資料運算
算數運算:
比較運算:
賦值運算:
邏輯運算:
成員運算:
身份運算:
位運算:
#!/usr/bin/python a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101 print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001 #相同為0,不同為1 print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011 print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000 print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111 print "Line 6 - Value of c is ", cView Code
運算子優先順序:
七、if....else
場景一、驗證登陸;
1 import getpass 2 3 name = input("user:") 4 pawd = getpass.getpass('password:') 5 6 if name == "admin" and paed == "admin" : 7 print("welcome,HYH") 8 else: 9 print("使用者名稱或密碼錯誤")View Code
場景二:猜年齡大小
設定年齡大小;輸入正確提示correct;猜大了提示big,猜小了提示small
age = 28 num = int(input("input your guess num:")) if num == age : print("correct") elif num < age : print("small") else: print("big")View Code
外層變數可以被內層程式碼使用
內層變數不能被外層程式碼使用
八、while迴圈
死迴圈,永遠不會停止
1 num = 10 2 while True: 3 print(num) 4 # 一直迴圈10,不會停止
使用者名稱和密碼登陸;輸入三次不對以後,提示是否繼續?
1 name = 'admin' 2 pawd = 'admin' 3 count = 0 4 while count <3 : 5 inname = input("user:") 6 inpass = input("pass:") 7 8 if inname == name and inpass == pawd : 9 print("welcome log in...!") 10 break 11 count +=1 12 if count >= 3: 13 #print("Whether to continue to log in") 14 inlog = input("Continue to log in") 15 if inlog != 'n' : 16 count = 0
九、for迴圈
迴圈5次
1 for i in range(5): 2 print("loop",i)
需求一:還是上面的程式但是遇到小於5的迴圈次數就不走了,直接跳入下一次迴圈
for n in range(10): if n < 5: continue #跳出本次迴圈,進入下次迴圈 print("loop",n)
需求二:還是上面的程式但是遇到大於5的迴圈次數就不走了,直接跳入下一次迴圈
for n in range(10): if n > 5: continue print("loop",n)
十、break 和 continue
break語句用在while和for迴圈中。
如果您使用巢狀迴圈,break語句將停止執行最深層的迴圈,並開始執行下一行程式碼。
Python continue 語句跳出本次迴圈,而break跳出整個迴圈。
continue 語句用來告訴Python跳過當前迴圈的剩餘語句,然後繼續進行下一輪迴圈。
continue語句用在while和for迴圈中。
name
=
"alex"
print
"i am %s "
%
name
#輸出: i am alex