score=input('请输入您的年龄')
print(type(score))
score=int(score)
print(type(score))
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')
elif score<60:
print('你的考试等级为E')
python中的循环 ,光介绍for循环,for 临时变量 in可迭代对象:
循环体
name='neusoft'
for x in name:
print(x)
if x =='s':
print('哈哈')
这个x是什么鬼? x是临时变量不用 提前声明 python自动为你创建
循环次数哪里去了?
range(起始位置,终止位置,步长)可以写循环次数
起始位置省略为0,步长省略为1 范围是左闭右开
给女朋友道歉100次
for iin range(1,101,2):
print('对不起老婆我错了,这是我',i,'次向您道歉')
1.1常用数据类型
生成一个[0,1,2.....20]的列表,可以使用循环创建,创建一个空的列表
list1=[]
for i in range(21):
list1.append(i)
print(list1)
使用循环不停的append
heroList = ["鲁班七号",'安其拉','李白','后裔','100','10.1']
print(heroList)
遍历herolist
for hero in heroList:
print(hero)
len()可以检测对象元素的个数
for i in range(len(heroList)):
print(heroList[i])
if heroList[i]=='后裔':
print('恭喜你选中了隐藏英雄')
else:
print('不是隐藏英雄')
python做进度条,安装tqdm库,pip install 库的名称
导入tqdm
from tqdmimport tqdm
import time
mylist=[]
for iin range(10):
mylist.append(i)
遍历mylist
for xin tqdm(mylist):
time.sleep(2)
字符串,表示‘’ “”,要注意
name='k"e"be'
print(name)
name="k'e'be"
print(name)
访问
print(name[2])
修改
name[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(len(name))
join 将列表变成字符串
li =['你','好','帅']
disk_path=['C:','Users','Administer','Desktop','ccf']
path='\'.join(disk_path)
print(path)
li=''.join(li)
print(li)
元组
tuple()
list()
int()
str()
创建,元组和列表很相似,只不过不能修改
a=(1,'1',['ss'])
print(a)
print(type(a))
访问
print(a[1])
a[1]=6
元组的用处:
1.写保护,安全,Python内置函数返回的类型都是元素
2.相对列表来讲,元组更节省空间,效率更高
掌握两点 1.拥有一个元素的元组
b=(100,)
print(type(b))
2.我们经常使用的组合方式:
list2=[('a',22),('b',33),('c',99)]
字典,创建字典key-vlaue
info={'name':'韩东阳','age':18,'gender':'female'}
print(type(info))
访问字典 通过建访问值
print(info['name'])
访问不存在的建
print(info['add'])
当不存在这建的时候,可以返回默认设置的值,有这个建就正常返回
print(info.get('add','抚顺市'))
修改
info['age']=3
print(info)
增加 当字典中不存在这个,就会添加
info['add']='沈阳市'
print(info)
删除
del info['age']
print(info)
遍历
for k,vin info.items():
print(k,'---->',v)
获取所有建
print(list(info.keys()))
获取所有值
print(list(info.values()))
函数 面向过程,方法 面向对象,python 中的函数,def 函数名():,函数体
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.获取到网页的源代码requests,安装request
pip install requests
import requests
获取指定域名的源代码
response=requests.get('https://www.baidu.com')
响应状态码200 ok 404 not found
print(response.status_code)
响应的编码方式,设置编码方式
response.encoding='utf-8'
print(response.encoding)
获取string类型的响应
html_data=response.text
print(html_data)
将爬取的文件写成本地html,文件路径 读写模式 编码方式
with open('index.html','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('hua.jpg','wb')as f:
if response2.status_code==200:
f.write(img_data)
2.提取需要的信息xpath