本節內容
1、IDLE的替代工具
2、數字、字符串、對象
3、注釋
4、用戶輸入
5、python的內置數據結構
6、列表
7、習題
1、pycharm and jupyter notebook
思考:
1、甚么是IDE?

1 ''' 2 集成開發環境(IDE,Integrated Development Environment )是用於提供程序開發環境的應用程序,一般包括代碼編輯器、編譯器、調試器和圖形用戶界面等工具。集成了代碼編寫功能、分析功能、編譯功能、調試功能等一體化的開發軟件服務套。 3 '''
2、甚么是IDLE?

1 #IDLE 是一個純 Python 下自帶的簡潔的集成開發環境(IDE)
1、pycharm
安裝網址:https://www.jetbrains.com/pycharm/ 安裝過程(略)
永久激活:https://www.liuzhishi.com/3184.html
優勢:1、可以自動補全。
2、方便於代碼調試,可以隨時終止進程。(vim、sublime等不能直接調試)
2、jupyter notebook
問題1:甚么是jupyter notebook?
問題2:如何在CMD中運行jupyter notebook?
嘗試一下吧:
甚么是jupyter notebook?
1、jupyter是一個基於web的IDE(集成開發環境)。
2、兼具腳本操作和交互式操作的特性;
3、筆記式編輯代碼和運行,便於調試,便於保存。
notebook使用舉例:
主要為開發商和數據科學家提供舉辦機器學習競賽、托管數據庫、編寫和分享代碼的平台。
Inside Kaggle you’ll find all the code & data you need to do your data science work. Use over 19,000 public datasets and 200,000 public notebooks to conquer any analysis in no time.
eg:
2、 在cmd中運行jupyter:
①安裝
②運行
學會了么?是不是很簡單。
總結:
cmd/IDLE 中的交互執行:偶爾執行一些簡單的語句、測試
jupyter notebook:介於交互和腳本之間的(可做筆記,關心中間過程的輸出)
IDLE 小型項目,學習初期的選擇,功能完善
PyCharm 中大型項目中方便組織較多文件,功能更為豐富
pycharm 將成為學習python之路上的最重要的工具。
jupyter 將成為我們數據科學(包含大數據、數據分析)之路的重要工具。
2、變量(數字、字符串、對象)
請同學們一定要詳細閱讀:https://docs.python.org/3.7/tutorial/introduction.html#numbers 學有余力的同學將鏈接中的代碼學習一遍。
1、數字(Numbers)
The integer numbers (e.g. 2
, 4
, 20
) have type int
, the ones with a fractional part (e.g. 5.0
, 1.6
) have type float
. We will see more about numeric types later in the tutorial.
1 >>> 2 + 2 2 4 3 >>> 50 - 5*6 4 20 5 >>> (50 - 5*6) / 4 6 5.0 7 >>> 8 / 5 # division always returns a floating point number 8 1.6
Division (/
) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the //
operator; to calculate the remainder you can use %
:
1 >>> 17 / 3 # classic division returns a float 2 5.666666666666667 3 >>> 4 >>> 17 // 3 # floor division discards the fractional part 5 5 6 >>> 17 % 3 # the % operator returns the remainder of the division 7 2 8 >>> 5 * 3 + 2 # result * divisor + remainder 9 17
1 import random 2 wait_time = random.randint(1,60) 3 print(wait_time)
Python中的數據類型(整型、浮點型和 復數 )
eg:
2、字符串(Strings)
Besides numbers, Python can also manipulate strings, which can be expressed in several ways.
They can be enclosed in single quotes ('...'
) or double quotes ("..."
) with the same result 2. \
can be used to escape quotes
1 >>> 'spam eggs' # single quotes 2 'spam eggs' 3 >>> 'doesn\'t' # use \' to escape the single quote... 4 "doesn't" 5 >>> "doesn't" # ...or use double quotes instead 6 "doesn't" 7 >>> '"Yes," they said.' 8 '"Yes," they said.' 9 >>> "\"Yes,\" they said." 10 '"Yes," they said.' 11 >>> '"Isn\'t," they said.' 12 '"Isn\'t," they said.'
3、對象
python中“一切皆是對象”。P48-49 了解
3、注釋
單行注釋:# 被注釋內容
eg1:
1 # -*- coding:utf-8 -*- 2 # Author:Zhichao
多行注釋:""" 被注釋內容 """
eg2:
1 ''' 2 import random 3 wait_time = random.randint(1,60) 4 print(wait_time) 5 6 word = "bottles" 7 print(word) 8 '''
除此之外,""" 被注釋內容 """ 還可以打印變量
eg:3
1 test1 =''' 2 import random 3 wait_time = random.randint(1,60) 4 print(wait_time) 5 6 word = "bottles" 7 print(word) 8 ''' 9 print(test1)
4、用戶輸入
eg:
1 # -*- coding:utf-8 -*- 2 # Author:Zhichao 3 4 username = input("username:") 5 password = input("password:") 6 7 print(username,password)
字符串拼接+打印:
eg:
1 name = input("name:") 2 age = input("age:") 3 job = input("job:") 4 salary = input("salary:") 5 6 info = '''----- INFO OF ''' + name +'''------''' + ''' 7 age:''' + age+''' 8 job:''' + job +''' 9 salary:'''+salary 10 11 print(info)
是不是很麻煩?有沒有更簡單的方式將打印內容?接下來我們來學習 %s 占位符。
1 name = input("name:") 2 age = input("age:") 3 job = input("job:") 4 salary = input("salary:") 5 6 info = '''-------INFO OF %s ------- 7 Name:%s 8 Age:%s 9 Job:%s 10 Salary:%s 11 '''% (name,name,age,job,salary) 12 13 print(info)
%s代表 string
%d代表 number
%f代表 float
更進一步:設置數據類型,將age、salary設置為number。

1 name = input("name:") 2 age = int(input("age:")) #注意! python中默認所有的輸入均為string 3 job = input("job:") 4 salary = int(input("salary:")) 5 6 info = '''-------INFO OF %s ------- 7 Name:%s 8 Age:%d 9 Job:%s 10 Salary:%d 11 '''% (name,name,age,job,salary) 12 13 print(info)
.format():

1 info2 = '''-------INFO OF {_name} ------- 2 Name:{_name} 3 Age:{_age} 4 Job:{_job} 5 Salary:{_salary} 6 '''.format(_name=name, 7 _age=age, 8 _job=job, 9 _salary=salary) 10 print(info2)

1 info3 = '''-------INFO OF {0} ------- 2 Name:{0} 3 Age:{1} 4 Job:{2} 5 Salary:{3} 6 '''.format(name,age,job,salary) 7 print(info3)
項目1:

1 # Author:Zhichao 2 3 today = input("weekday:") 4 if today == "Saturday": 5 print("Party!") 6 elif today == "Sunday": 7 condition = input("mood:") 8 if condition == "Headache": 9 print("Recover, then rest.") 10 else: 11 print("Rest.") 12 else: 13 print("Work, work, work.")
5、Python內置數據結構
6、列表
思考:如果把我們班同學的名字都存起來,你會怎么做?用變量name1、name2、name3....來存么?
還是這樣:names=“zhangyang liuming lihua”?。。那么如果想讓你取出其中某一個值該怎么辦?
Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.
請同學們詳細閱讀官方文檔:https://docs.python.org/3/tutorial/introduction.html#lists
來看一些例子
eg1:
1 names = ["Mike","Mary","Jan","Jack"] 2 3 print(names)
eg2:
1 >>> squares = [1, 4, 9, 16, 25] 2 >>> squares 3 [1, 4, 9, 16, 25]
回顧:for循環
1 for ch in "Hi!": 2 print(ch)
for加上列表來讓我們的列表開始工作(大腦P60):

1 vowels = ['a','e','i','o','u'] 2 word = "Milliways" 3 for letter in word: 4 if letter in vowels: 5 print(letter)

1 vowels = ['a','e','i','o','u'] 2 word = "Milliways" 3 found = [] 4 for letter in word: 5 if letter in vowels: 6 if letter not in found: 7 found.append(letter) 8 for vowel in found: 9 print(vowel)

1 vowels = ['a','e','i','o','u'] 2 word = input("Provide a word to search for vowels:") 3 found = [] 4 for letter in word: 5 if letter in vowels: 6 if letter not in found: 7 found.append(letter) 8 for vowel in found: 9 print(vowel)
列表認識開始、結束和步長值:
letters[start:stop:step]
如果沒有指定開始值,則默認為0;
如果沒有指定結束指,則取列表允許的最大值;
如果沒有指定步長值,則默認步長為1.
首先,列表也是一個變量,構建列表需要用”[]“,里面的數據之間用”,“進行分隔。
①list的查詢(切片):
思考eg1中如果我們想取出某一個值應該怎么辦?取出一些值呢?
eg3:
1 #Zhichao 2 3 names = ["Mike","Mary","Jan","Jack"] 4 5 print(names) 6 print(names[0],names[2]) #取出某一個值 7 print(names[1:3]) #切片 取出中間一些連續的值(還是列表) 8 #思考:如果列表有幾千個值,幾萬個值,那么想取出最后一個值怎么辦? 9 print(names[-1]) #切片 取出最后一個值 10 print(names[-3:-1]) #切片 從后往前取出一些值(還是列表),思考為什么不是[-1:-3]? 11 print(names[-2:]) #切片 取到最后一個值 12 print(names[:2]) #切片 同上,即“0”可以省略
②list 增、刪、改:
eg:
1 #Zhichao 2 3 names = ["Mike","Mary","Jan","Jack"] 4 5 print(names) 6 names.append("Alex") #list增:append增加數據,觀察增加的位置。 7 print(names) 8 names.insert(1,"Dan") #list增:insert指定位置增加數據 9 print(names) 10 names[3] = "Zhichao" #list改:直接找到列表中某個值,然后賦新值 11 print(names) 12 names.remove("Zhichao") #list刪:remove指定某個數據進行刪除 13 print(names) 14 del names[1] #list刪: del指定[x]的某個數據位置進行刪除 15 print(names) 16 names.pop() #list刪:Remove and return item at index (default last) 不指定默認刪除最后一個值 17 print(names) 18 names.pop(1) #lsit刪:如果加上指定位置,names.pop(1)<==> del names[1] 19 print(names)
參考結果:
③列表的復制 .copy()
1 first = [1,2,3,4,5] 2 second = first 3 print(second) 4 second.append(6) 5 print(first)
正確的復制列表的方法1:
1 #接上面的代碼 2 third = second.copy() 3 third.append(7) 4 print(second,third)
方法2:
利用切片:
1 a = [1,2,3,4] 2 b = a[:] 3 print(a,b) 4 b.append(5) 5 print(a,b)
預:嘗試填大腦P67處理列表代碼
項目2(將"Don‘t panic!" 轉換為 "on tap"):大腦P68(上課詳解)

1 # Author:Zhichao 2 3 phrase = "Don't panic!" 4 plist = list(phrase) 5 print(phrase) 6 print(plist) 7 8 for i in range(4): 9 plist.pop() 10 plist.pop(0) 11 plist.remove("'") 12 plist.extend([plist.pop(),plist.pop()]) 13 plist.insert(2,plist.pop(3)) 14 new_phrase = ''.join(plist) 15 print(plist) 16 print(new_phrase)

1 # -*- coding:utf-8 -*- 2 # Author:Zhichao 3 4 phrase = "Don't panic!" 5 plist = list(phrase) 6 print(phrase) 7 print(plist) 8 9 new_pharse = ''.join(plist[1:3]) 10 new_pharse = new_pharse + ''.join([plist[5],plist[4],plist[7],plist[6]]) 11 print(plist) 12 print(new_pharse)
思考:panic和painc2 那個更好?
閱讀大腦P82-P85
for 循環了解列表:
課本案例:

1 # -*- coding:utf-8 -*- 2 # Author:Zhichao 3 4 paranoid_android = "Marvin" 5 letters = list(paranoid_android) 6 for char in letters: 7 print('\t',char)

1 # -*- coding:utf-8 -*- 2 # Author:Zhichao 3 4 paranoid_android = "Marvin, the Paranoid Android" 5 letters = list(paranoid_android) 6 for char in letters[:6]: 7 print('\t',char) 8 print() 9 for char in letters[-7:]: 10 print('\t'*2,char) 11 print() 12 for char in letters[12:20]: 13 print('\t'*3,char) 14 print()
7 習題
購物車程序項目:
要求:1、運行程序后,讓用戶輸入支付寶余額,然后打印我們的商品列表給用戶。
2、讓用戶輸入商品編號進行商品的購買。
3、用戶選擇商品后,檢查用戶的余額是否夠,若夠則直接扣款,不夠則提醒用戶。
4、用戶可以隨時退出購買,推出時打印用戶已購買的商品和支付寶余額。