Python第二天

1.1常用的数据类型

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

1.1.1列表

类似于c中的数组,但是与数组不同的是,list可以储存不同类型的数据

创建一个列表

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

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

为什么要使用列表? 列表可以将我们需要的很多元素封装到一个容器中

列表的相关操作:

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

print("英雄为 :",herolist[1],herolist[0])

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

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

3、修改

herolist[3]="貂蝉"
print("修改后的列表",herolist)

4 删除

del herolist[4]
print("删除后的列表",herolist)

Python 中的循环for循环

格式

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

循环体

name = 'neusoft'
for x in name:
    print(x)
    if x=='s':
      print('吼吼')

循环次数哪去了?

这个x是什么鬼? x是临时变量不用 提前说明 Pytone自动为你创建

循环次数哪去了?

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

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

rong()可以写循环次数

给女朋友道歉一百次

for i in range(100):
    #print('对不起我错了,这是我','i+1','次向你道歉')
for i in range(1,101,2):
    print('对不起,这是我',i,'次向你道歉')

单行注释 # 注释的作用 让读代码的人看懂

爬虫

1pytone基础语法

1.1常用的数据类型

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

1.1.1列表

类似于c中的数组,但是与数组不同的是,list可以储存不同类型的数据

创建一个列表

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

生成一个【0,1,2....20】的列表

可以使用循环

创建一个空列表

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

遍历herolist

for hero in heroList:
    print(hero)
 len() 可以检测对象的元素个数
for i in range(6):
    print(herolist[i])
for i in range(len(herolist)):
 print(herolist[i])
  if herolist[i] == '李白':
    print('恭喜你选中了隐藏英雄')
    else:
      print('不是隐藏英雄')

Python软件进度条

安装 tqdm库

pip install 库的名字

导入tpdm

from tqdm import tqdm
import time
mylist = []
for i in range(10):
    mylist.append(i)

遍历mylist

#for x in tqdm(mylist):
#    time.sleep(2)

字符串

表示'' ""

要注意的是

name = "k""o""be"
name = 'k"o"be'
#print(name)
# 访问
#print(name[2])
# 访问
#print[1] = "x"
#print(name)
#name = "kebe"
#print(name)

常用操作

price =  '$9.9'
 字符串的替换
price = price.replace("$",'')
 价格涨价10倍
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()

join列表变成字符串

li = ['你','好','帅']
disk_path = ['']

元组

tuple()
list()
int()
str()

创建

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

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

访问

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

元组的用处

1,写保护,安全,Python内置函数返回的类型都是元组
2,想对列表来讲,元组更节省空间,效率更高

掌握

1.拥有一个元素的元组

# b = (100,)
 b = (type(b))

# 我们经常使用的组合方式:

 list2 = [('a',22),('b',33),('c',99)]

字典

创建字典

info = {'name':'雷焱博','age':20,'gender':'female'}
 print(type(info))

访问字典 通过建访问值

print(info['name'])

访问不存在的键

# # print(info['add'])
# print(info.get('addr','抚顺市'))

修改

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

增加 当字典不存在这个键,就会添加

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

删除

 del info['age']
 print(info)
for k,v in info.items():
    print(k, '---->',v)

获取所有键

print(list(info.keys()))

获取所有的值

print(list(info.valuse()))

Python 中的函数

def 函数名()

函数体

 def say_hello(name):
     print('hello',name)
 say_hello('neusoft')

1到任意数之间累加和

 def caculate_num(num):
   sum_num = 0 #存求和
     for i in range(1,num+1):
   sum_num = sum_num + i
   return sum_num
print(caculate_num(1000))

爬虫

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

2安装requests

import requests

然后提取我们要的信息

import response
requests = requests.get('https://www.baidu.com')

响应状态码 200 ok 404 not found

print(requests.status_code)

响应的编码方式

response.encding = 'utf-8'
print(response.status_code)
print(response.encoding)

获取string类型响应

html_data = response.text
print(html_data)

将爬取的文件写成本地html

文件路劲,读写模式,编码方式

with open('index.thm1','w',encoding='utf-8') as f:
    f.write(html_data)

图片爬取

图片地址

url = 'http://file02.16sucai.com/d/file/2014/0704/e53c868ee9e8e7b28c424b56afe2066d.jpg'
response2 = requests.get(url)

获取byte类型的响应

img_data = response2.content

文件路劲,读写模式write binary.编码方式

with open('kobe.png','wb') as f:
    if response2.status_code == 200:
        f.write(img_data)

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