7-16
學(xué)習(xí)pycharm的使用及復(fù)習(xí)基本語法
參考書籍:《Python編程:從入門到實踐》
《編程小白的第一本python編程入門書》
pycharm的使用及常規(guī)設(shè)置
new project→new file→python file
默認設(shè)置更改:file→setting
高效使用python的系列文檔:https://pedrokroger.net/getting-started-pycharm-python-ide/(暫時好像沒啥用)
基本語法
字符串
輸入
what_he_does = 'plays'
his_instruments = 'guitar'
his_name = 'Robert'
artist_intro = his_name + what_he_does + his_instruments
print(artist_intro)
Run
F:\pythonwork\venv\Scripts\python.exe F:/pythonwork/a01.py
Robertplaysguitar
Process finished with exit code 0
不同類型字符串不能合并
查看類型print(type(word))
轉(zhuǎn)化類型num2=int(string)
字符串的分片與索引
字符串可以通過string[x]的方式進行索引、分片,字符串的分片實際上可以看做從字符串中找出你要截取的東西,復(fù)制出來一小段長度,儲存在另一個地方。
name = 'my name is mike'
print(name[0])
print(name[-4])
print(name[11:14])
print(name[5:])
print(name[:5])
Run
m
m
mik
me is mike
my na
第一個字符是0,最后一個是-1
接下來面對一個場景,就是在登陸賬戶的時候,密碼需要遮擋,賬戶信息只顯示后四位。
phone_number = '1386-666-0006'
hiding_number = phone_number.replace(phone_number[:9],'*' * 9)
print(hiding_number)
Run
*********0006
接下來解決另一個問題,手機中號碼查找功能
search = '168'
num_a = '1386-168-0006'
num_b = '1681-222-0006'
print(search + ' is at ' + str(num_a.find(search) + 1 ) + ' to'+ str(num_a.find(search) + len(search)) + ' of num_a')
print(search + ' is at ' + str(num_b.find(search) + 1 ) + ' to'+ str(num_b.find(search) + len(search)) + ' of num_b')
Run
168 is at 6 to8 of num_a
168 is at 1 to3 of num_b
函數(shù)
創(chuàng)建函數(shù)
def fahrenherit_converter(C):
fahrenherit = C * 9/5 +32
return str(fahrenherit) + 'F'
C2F = fahrenherit_converter(35)
print(C2F)
位置參數(shù)傳入的方式
trapezoid_area(base_up=1, base_down=2, height=3)
trapezoid_area(1,2,3)
邏輯控制與循環(huán)
成員運算符與身份運算符
album = ['Black Star','David Bowie',25,True]
album.append('new song')
'Black Star' in album
append用于添加
第三行代碼返回true or false
嵌套循環(huán)
for i in range(1,10):
for j in range(1,10):
print('{} X {} = {}'.format(i,j,i*j))
數(shù)據(jù)結(jié)構(gòu)
Python有四種數(shù)據(jù)結(jié)構(gòu):列表,字典,元組,集合。
list = [val1,val2,val3,val4]
dict = {key1:val1,key2:val2}
tuple = (val1,val2,val3,val4)
set = {val1,val2,val3,val4}
列表
列表可以裝進python的所有對象
fruit.insert(1,'grape')插入必須指定位子
remove()刪除列表中元素
字典
字典這些數(shù)據(jù)結(jié)構(gòu)特征如現(xiàn)實中字典一樣,使用名稱-內(nèi)容進行數(shù)據(jù)的構(gòu)建,python中分別對應(yīng)key-value
key不可重復(fù)不可改變,value可以
NASDAQ_code.update({'FB':'Facebook','TSLA':'Tesla'})添加元素
del NASDAQ_code['FB']刪除元素,用key來索引
元組
元組可以別看作是穩(wěn)定的列表,不能被修改
集合
不能別切片也不能被索引
列表推導(dǎo)式的用法
list = [item for item in iterable]
item是想要放在列表的元素,后面就是循環(huán)
d = {i:i+1 for i in range(4)}
g = {i:j for i,j in zip(range(1,6),'abcde')}
g = {i:j.upper() for i,j in zip(range(1,6),'abcde')}
類
class CocaCola:
formula = ['caffeine','sugar','water','soda']
print(CocaCola.formula)
for element in coke_for_me.formula:
print(element)
創(chuàng)建屬性object.new_attr
class cocacoal:
formula = ['caffeine','suger','water','soda']
def drink(self,how_much):
if how_much == 'a sip':
print('cool')
if how_much == 'whole bottle':
print('headache')
ice_coke = cocacoal()
ice_coke.drink('a sip')
python中存在一些方法被稱為魔術(shù)方法,init()意味著在創(chuàng)建實例的時候不去引用命令也會被自動執(zhí)行
類的繼承
class CaffeineFree(CocaCola):
caffeine = 0
ingredients = [
'High Fructose Corn Syrup',
'Carbonated Water',
'Phosphoric Acid',
'Natural Flavors',
'Caramel Color',
]
coke_a = CaffeineFree('Cocacola-FREE')
coke_a.drink()
在新的類caffeinefree后面括號中放入cocacola,表示這個類繼承于cocacola這個父類,caffeinefree成了子類
安裝自己的庫
找到python文件夾,找到下面的site-packages文件夾,記住你的文件名,將你寫的py文件放進去
使用第三方庫
https://awesome-python.com/
比如要找爬蟲的庫,查找web Crawling這個分類
pycharm中安裝庫
file→default settings
搜索project interpreter選擇當(dāng)前版本,按加號添加庫,搜索,install
使用pip安裝庫
python3 -m pip install PackageName
pip list 查看庫
pip install --upgard pip 升級pip