Python基礎一,Python基礎
一、Python介紹及版本
Python崇尚優美、清晰、簡單,是一個優秀並廣泛使用的語言。
目前Python主要應用領域:
雲端運算:雲端運算最火的語言
WEB開發:眾多優秀的WEB架構,眾多大型網站均為Python開發,典型WEB架構有Django
科學運算、人工智慧:典型庫NumPy, SciPy, Matplotlib, Enthought librarys,pandas
系統營運:營運人員必備語言
金融:量化交易,金融分析
圖形GUI: PyQT, WxPython,TkInter
二、程式設計語言的分類及優缺點
解釋型:當程式運行時,將代碼一行一行的解釋成二進位,解釋一行運行一行。
優點:快速排錯,開發效率高,可跨平台
缺點:程式運行效能相當較低
典型:Python
編譯型:將程式碼一次性全部編譯成二進位,然後再運行。
優點:程式效能高
缺點:排錯慢,開發效率高,不能跨平台
典型:C語言
三、運行第一個python程式
#!/usr/bin/env python# -*- coding: utf-8 -*-print("hello,world!")四、變數五、常量
不變的量:出生日期、社會安全號碼等。python沒有規定,預設全部大寫的變數成為常量,BIRTH = 19970425
六、注釋
作用:方便自己隨時記起代碼的作用,以及其他人容易看懂你的代碼。
種類:
- 單行注釋:#。 例如:#被注釋內容
- 多行注釋:'''被注釋內容'''或"""被注釋內容"""
七、基礎資料類型
type() 判斷此資料是什麼資料類型。
1. 數字類型int
用於計算或比較。
i = 2print(i * 3)
2. 字串類型str
用於字串(加引號就是字串)
name = '老男孩'name2 = '路飛學院'print(name,'與',name2)#'與"混合使用防止錯亂msg = "My name is jim,I'm 26 years old!"msg = ''' 今天我想寫首小詩, 歌頌我的同桌, 你看他那烏黑的短髮, 好像一隻炸毛雞。'''#字串拼接n1 = '你在'n2 = '看部落格'n3 = n1 + n2print(n3)
3. 布爾值
布爾值用於判斷正確與否,其結果只能為True或False
print(1 > 2 and 3 < 4 or 4 > 5)
八、使用者互動
input資料類型全部是字串類型。
python 2x:raw_input()
python 3x:input()
name = input("請輸入你的名字:")age = input("請輸入你的年齡:")hobby = input("請輸入你的愛好:")s = "你的名字是" + name + ",今年" + age + "歲," + "愛好" + hobbyprint("我的名字是%s,我的年齡是%s,我的愛好%s" % (name,age,hobby))print(s)九、if及while迴圈的一些基本結構
if
if 條件: 結果
if 條件: 結果else: 結果
1 choice = input('請輸入你猜的數字:') 2 if choice == '2':3 print('二')4 elif choice == '3':5 print('三')6 elif choice == '4':7 print('四')
1 choice = input('請輸入你猜的數字:') 2 if choice == '2':3 print('二')4 elif choice == '3':5 print('三')6 elif choice == '4':7 print('四')8 else:9 print('選擇錯誤...')
1 if 條件:2 if 條件:3 結果4 else:5 結果6 else:7 結果
1 age = int(input('請猜我的年齡:'))2 if True:3 if age <= 18:4 print('恭喜你猜對了!')5 else:6 print('這都看出來...')7 else:8 print(666)
while
while 條件: 結果
while True: print('青花') print('王妃') print('涼涼') print('深夜地下鐵')print(222)while True: print(111) print(222)print(444)
跳出迴圈的條件:
1、改變條件
2、break
break:結束迴圈
continue:結束本次迴圈,繼續下一次迴圈
flag = Truewhile flag:print('等一分鐘')print('你的背包')print('彩色的黑')flag = False
flag = Truecount = 1while flag:print(count)count = count + 1if count == 101:flag = False
while True:print(111)print(222)breakprint(333)
count = 1while True:print(count)count = count + 1if count == 101:break