python第一天課程:歷史,數據類型, if, while
python 2 與 python 3的區別:
python 2源代碼不標準,混亂,重複代碼太多
python 3源代碼統一,去除重複代碼
python 的環境:
編譯型:一次性將所有程序編譯成二進制的文件。
缺點:開發效率低,不能夠跨平臺
優點:運行速度快
:c,c++等等
解釋型:儅程序執行時,一行一行的解釋。
優點:開發效率高,可以跨平臺
缺點:運行速度相對於編譯型的慢
:python,php等等
python 種類:
運行第一個文件:
python 3x: python 文件路徑 回車
python 2x: python2 文件路徑 回車
python 2和python 3的區別:python 2默認編碼方式是ascii碼
解決方式:在文件的首行: #-*-encoding:utf-8 -*-
python3默認編碼方式utf -8
變量:
變量就是將一些運算的中間結果暫存到内存卡中,以便後續代碼調用。
1.必須由數字,字母,下劃綫任意組合,且不能以數字開頭。
2.不能是python中的關鍵字。
['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']
3.變量具有可描述性
4.不能是中文
常量:
一直不變的量 π BIR_OF_CHINA = 1949 注釋
方便自己方便他人理解代碼
單行注釋:#
多行注釋:'被注釋内容',"被注釋内容"
用戶交互(input):
1.等待輸入 2.將你輸入的内容賦值給了前面變量
3.input出來的數據類型全部都是str
基礎數據類型初始
數字:int 1,2,3,4,5
+ - * / **
%取餘數
ps:type
字符串轉化成數字:int(str) 條件:str必須由數字組成的
數字轉化成字符串:str(int)
字符串:python當中凡是由引號引起來的都是字符串
可相加:字符串的拼接
可相乘:str*int
bool:布爾值 True False。
#print ''' x = 1+2+3+4+5 print(x) print(x * 5) y = x * 5 print(y+100-45+2) print('泰哥泰哥,我是小弟') print('泰哥泰哥,我是三弟小妹') age1 = 12 age2 = age1 age3 = age2 age2 = 100 age = 5 print(age1,age2,age3) a = '泰哥' b = '小二' c = a + b print(c) print('泰哥' + '小二' + '貨') print('堅强' * 8) msg=""" 今天我想寫首小詩, 歌頌我的同桌, 你看他那烏黑的短髮, 好像一只炸毛鷄。 """ print(msg) print(True,type(True)) print('True',type('True')) name = input('請輸入你的名字') age = input('請輸入你的年齡') print('我的名字是' + name,'我的年齡' + age + '歲') ''' #If ''' #第一種 if 4 > 5: print('我請你喝酒') print('喝什麽酒') #第二種 if 4 > 5: print('我請你喝酒') else: print('喝什麽酒') ''' #多選 ''' #多選: num = input('請輸入你猜的數字') if num == '1': print('一起抽烟') elif num == '2': print('一起喝酒') elif num == '3': print('新開了一家,走看看') else: print('你猜錯了') score = int(input('輸入分數')) if score > 100: print('我擦,最高分才100...') elif score >= 90: print('A') elif score >= 80: print('B') elif score >= 60: print('C') elif score >= 40: print('D') else : print('太笨了...E') name = input('請輸入名字:') age = input('請輸入年齡:') if name == '小二': if age == '18': print(666) else: print(333) else: print(錯了) ''' #While ''' #while print('111') while True: print('我們不一樣') print('在人間') print('養') print('222') count = 1 flag = True while flag: print(count) count = count + 1 if count > 100: flag = False count = 1 while count <= 100: print(count) count = count +1 count = 1 sum = 0 while count <= 100: sum = sum + count count = count + 1 print(sum) ''' #break ''' print('11') while True: print('222') print(333) break print(444) print('abc') count = 1 while True: print(count) count = count + 1 if count > 100: break ''' #continue ''' print(111) count = 1 while count < 20: print(count) continue count = count + 1 count = 0 while count <= 100: count += 1 if count > 5 and count < 95: continue print("loop", count) print("-----out of while loop -----") '''
作業:
#1.使用while loop輸入123456 8910 ''' count = 0 while count < 10: count = count +1 if count == 7: print('') continue print(count) ''' #2.求1-100所有數的和 ''' count = 1 sum = 0 while count <= 100: sum = sum + count count = count + 1 print(sum) ''' #3.輸出1-100内的所有奇數 ''' odd = 1 while odd < 99: odd = odd + 2 print(odd) ''' #4.輸出1-100内的所有偶數 ''' even = 2 while even < 99: even = even + 2 print(even) ''' #5.求1-2+3-4+5 ... 99的所有數的和 ''' sum = 0 count = 1 while count < 100: if count % 2 == 0: sum = sum - count else: sum = sum + count count = count + 1 print(sum) ''' #6.用戶登錄(三次機會重試) ''' i = 0 while i<3: username = input('請輸入用戶名') password = int(input('請輸入密碼')) if username == '鹹魚哥' and password == 123: print('登錄成功') else: print('登錄失敗,請重試') i += 1 '''