python的第二天


1.Python中的循环

1.1 for循环

格式:for 临时变量 in 可迭代对象

#循环体

name ='neusoft'

for x in name:print(x)

if x =='s':print('666')

range(起始位置,终止位置,步长)可以写循环次数

起始位置省略,默认为0,步长省略,默认为1,范围是左闭右开

给女朋友道歉100次

foriinrange (1,101,2):print('对不起!!!我错了!!!!,这是我',i,'次向您道歉')

1.2 常用数据类型

数字、列表、字符串、字典、元组、集合

创建一个列表

heroList = ["鲁班七号","安琪拉","李白",'后羿',100,10.1]print(heroList)

总结:列表使用[]进行创建

列表可以将我们需要的很多元素封装到一个容器中。

1.访问列表中元素  列表名[索引]

print("英雄为:",heroList[0],heroList[3])

2.添加元素  append是在列表的末尾进行添加元素

heroList.append('鲁班大师')print("添加后的列表",heroList)

3.修改

heroList[4]='貂蝉'print("修改后的列表",heroList)

4.删除

delheroList[5] print("删除后的列表",heroList)

生成一个[0,1,2,......20]的列表,可以使用循环来创建

5.创建一个空列表

list1 = []#使用循环不停的appendforiinrange(21):    list1.append(i)print(list1)

遍历herolist

forheroinheroList:print(hero)

len()可以检测对象的元素个数

foriinrange (len(heroList)):print(heroList[i])ifheroList[i]=='后羿':print('恭喜你选中了隐藏英雄')else:print('不是隐藏英雄')

1.3 Python制作进度条

1.安装 tqdm库

pip install 库的名称

2.导入 tqdm

fromtqdmimporttqdmimporttimemylist = []foriinrange(20):    mylist.append(i)    print(mylist)

3.遍历mylist

forxintqdm(mylist):    time.sleep(0.5)

4.字符串

5.表示 ‘’  “ ” 都可以

name =' K"o"be 'print(name)

6.访问

print(name[3])

7.修改

name[1] ="x"print(name) name ="kobe"print(name)

8.常用操作

price ='¥9.9'

9.字符串的替换

price = price.replace("¥",'')print(price)

价格涨价10倍

new_price =float(price)*10print(new_price)

10.写一个价值一亿的AI代码

whileTrue:    seg = input('')   

 seg = seg.replace('吗?','!')

print(seg)

strip 去空格

name ='  neuedu  'print(len(name)) name = name.strip()print(len(name))

join 将列表变成字符串

li = ['you ','are ','handsome']

disk_path=['C:','Users','DELL','Desktop','大三上学期']

path ='\\'.join(disk_path)print(path)li=''.join(li)print(li)

1.4 元组

tuple()

list()

int()

str()

1.创建

元组和列表很相似,只不过不能修改

a = (1,'1',3)print(a)print(type(a))

2.访问

print(a[2]) a[2] = 6  错误

3.元组的用处:(1)写保护,安全,Python内置函数返回的类型都是元组(2)相对于列表来讲,元组更节省空间,效率更高

4.掌握:拥有一个元素的元组 b = (100,)print(type(b))

1.5 字典

1. 创建字典  key -value

info = {'name':'费星浩','age':20,'gender':'male'}print(type(info))

2. 访问字典 通过建访问值

print(info['name'])

3. 访问不存在的建

print(info['add'])

4. 当不存在这个建的时候,可以返回默认设置的值; 有这个建就正常返回

print(info.get('addr','抚顺市'))

5. 修改

info['age'] = 3print(info)

6. 增加

当字典中不存在这个建,就会添加

info['addr'] ='鞍山市'print(info)

7. 删除

delinfo['age'] print(info)###遍历

for k,v in info.items():

print(k,'------->',v)

8. 获取所有建

print(list(info.keys()))#获取所有值print(list(info.values()))

1.6 爬虫

1.获取到网页的源代码  requests

安装requests

pip install requestsimportrequests

# 获取指定域名的源代码response = requests.get('https://www.baidu.com')

# 响应状态码 200 ok  404 not foundprint(response.status_code)

# 响应的编码方式

# 设置编码方式response.encoding ='utf-8'print(response.status_code)print(response.encoding)

# 获取 string类型的响应html_data = response.textprint(html_data)

# # 将爬取的文件写成本地 htmlwithopen('index.html','w',encoding='utf-8')asf:    f.write(html_data)

# 文件路径,读写模式,编码方式# 图片爬取# 图片地址url ='http://b-ssl.duitang.com/uploads/item/201805/09/20180509003136_epwlq.jpg'response2 = requests.get(url)

#获取byte类型的响应img_data = response2.contentwithopen('bts.jpg','wb')asf:ifresponse2.status_code ==200:      

  f.write(img_data)

你可能感兴趣的:(python的第二天)