学python第二天_学习Python的第二天
elif
score =73
if score >=90 and score <=100:
print('你的考試等級為A')
elif score >=80 and score <90:
print('你的考試等級為B')
elif score >=70 and score <80:
print('你的考試等級為C')
elif score >=60 and score <70:
print('你的考試等級為D')
Python中的循環(huán)
先介紹for循環(huán)
格式:
for 臨時變量 in 可迭代對象:
循環(huán)體
name ='neusoft'
for xin name:
print(x)
if x =='s':
print('滾')
循環(huán)次數(shù)哪去了?
這個x是啥呀? x是臨時變量不用 提前聲明 Python自動為你創(chuàng)建
range (起始位置,終止位置,步數(shù)) 可以寫循環(huán)次數(shù)
起始位置省略默認(rèn)為0,步長省略為1,范圍是左閉右開
給兄弟道歉一百次
for iin range(1,101,2):
print('對不起兒子,爹錯了,這是我第',i,'次道歉')
1.1常用數(shù)據(jù)類型
數(shù)字·列表·字符串·字典·元組·集合
1.1.1列表
類似于C中的數(shù)組,但是與數(shù)組不同的是,list可以存儲不同類型的數(shù)據(jù)
創(chuàng)建一個列表
heroList = ['魯班七號','安琪拉','金鵬','后羿',100,10.01]
print(heroList)
總結(jié) ;列表使用[]進行創(chuàng)建
為什么要使用列表? 列表可以將我們需要的很多元素封裝到一個容器中
列表的相關(guān)操作;
1.訪問列表中的元素 列表名[索引]
print('英雄為:',heroList[1],heroList[0])
2.添加元素append是在列表的末尾進行添加
heroList.append('魯班大師')
print('添加后的列表為',heroList)
3.修改
heroList[4] ='瑤'
print("修改后的列表為",heroList)
4.刪除
del heroList[5]
print("刪除后的列表為",heroList)
生成一個[0,1,2,.......20] 的列表
可以使用循環(huán)來創(chuàng)建
創(chuàng)建一個空的列表
list1 = []
使用循環(huán)不停的append
for iin range(21):
list1.append(i)
print(list1)
遍歷heroList
for heroin heroList:
print(hero)
len() 可以檢測對象的元素個數(shù)
for iin range(len(heroList)):
print(heroList[i])
if heroList[i] =='魯班七號':
print('恭喜你選中了隱藏英雄')
else:
print('不是隱藏英雄')
Python制作進度條
安裝tqdm庫
pip install 庫的名稱
導(dǎo)入tqdm
from tqdmimport tqdm
import time
mylist = []
for iin range(20):
mylist.append(i)
遍歷mylist
for xin tqdm(mylist):
time.sleep(1)
字符串
表示'' ""
要注意的是
name ="k'o'be"
print(name)
訪問
print(name[2])
修改
name[2] = 'x'
print(name)
name ='kobe'
print(name)
常用操作
price ='¥9.9'
字符串的替換
price = price.replace("¥",'')
價格漲價十倍
new_price =float(price)*10
print(new_price)
寫一個價值一億的AI代碼
while True:
seg =input('')
seg = seg.replace('嗎?','!')
print(seg)
strip 去空格操作
name =" neuedu "
print(len(name))
name = name.strip()
print(len(name))
join = 將列表變成字符串
li = ['好','嗨','喲']
disk_path = ['C:','Users','Administrator','Desktop','CCF']
path ='\\'.join(disk_path)
print(path)
li =''.join(li)
print(li)
元組
tuple()
list()
int()
str()
創(chuàng)建
元組和列表很相似,只不過不能修改
a = (1,'1',3)
print(a)
print(type(a))
# 訪問
print(a[2])
a[2] =6
元組的用處:
1寫保護,安全,Python內(nèi)置函數(shù)返回的類型都是元素
2相對列表來講,元組更節(jié)省空間,效率更高
掌握
1擁有一個元素的元組
b = (100,)
print(type(b))
我們經(jīng)常使用的組合方式:
list2 = [('a',22),('b',33),('c',99)]
字典
創(chuàng)建字典key -value
info = {'name':'腰子','age':30,'gender':'female'}
print(type(info))
訪問字典 通過建訪問值
print(info['name'])
訪問不存在的鍵
print(info['add'])
當(dāng)不存在這個鍵的時候,可以返回默認(rèn)設(shè)置的值
有這個鍵就正常返回
print(info.get('addr','撫順市'))
修改
info['age'] =3
print(info)
增加 當(dāng)字典中不存在這個鍵,就會添加
info['addr'] ='鞍山市'
print(info)
刪除
del info['age']
print(info)
遍歷
for k,xin info.items():
print(k,'---->',x)
獲取所有鍵
print(list(info.keys()))
獲取所有的值
print(list(info.calues()))
函數(shù) 面向過程
方法 面向?qū)ο?/p>
Python 中的函數(shù)
def 函數(shù)名():
函數(shù)體
def say_hello(name):
print('hello',name)
say_hello('neusoft')
1到 100之間加和5050
def caculate_num(num):
sum_num =0 # 存求和
for iin range(1, num+1):
sum_num = sum_num + i
return sum_num
print(caculate_num(100))
1.獲取到網(wǎng)頁的源代碼,requests
安裝requests
pip install requests
import requests
獲取指定域名的源代碼
response = requests.get('https://www.baidu.com')
響應(yīng)狀態(tài)碼200 ok 484 not found
print(response.status_code)
響應(yīng)的編碼方式
設(shè)置編碼方式
response.encoding ='utf-8'
print(response.status_code)
print(response.encoding)
獲取 string類型響應(yīng)
html_data = response.text
print(html_data)
# 將爬取的文件寫成 本地html
文件路徑,讀寫模式,編碼方式
with open('index.html','w',encoding='utf-8')as f:
f.write(html_data)
圖片爬取
圖片地址
ur1 ='https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1188387633,958216909&fm=26&gp=0.jpg'
response2 = requests.get(ur1)
獲取byte類型的響應(yīng)
img_data = response2.content
文件路徑,讀寫模式,編碼方式
with open('hanmeijuan.png','wb')as f:
if response2.status_code ==200:
f.write(img_data)
2.然后提取我們要的信息,xpath
總結(jié)
以上是生活随笔為你收集整理的学python第二天_学习Python的第二天的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 车企要“上天”?
- 下一篇: python程序员脱单攻略_作为一只程序