Python基础 01 - 真 · 基础
目录
python概述
Python:
1.概述:
1.语言
2.开发起来简洁 =》 java
2.使用场景:
1.数据分析 =》 numpy 、pandas
2.web 开发 =》 用的不多 、 百度
3.游戏开发 =》 用的不多
4.AI (机器学习、深度学习)
5.爬虫
1.部署安装:
1.安装:
1.python 原生的安装包 www.python.org
2.anaconda 安装 =》
1.python 有的
2.有一些丰富的第三方库
anaconda:
1.下载安装包
2.安装 =》 next
2.开发python :
1.ide :
1.pycharm =》 idea
2.jupyter =》web 版的开发
python里的注释
# print("python") 单行注释
'''
多行注释
'''
"""
多行注释(模版字符串,但是可以当注释使用)
"""
数据类型
int float str
n1 = 10
print(n1)
print(type(n1))
10
<class 'int'>
n2 = 10.1
print(n2)
print(type(n2))
10.1
<class 'float'>
s1 = 'xianyu'
s2 = "xian"
print(s1)
print(s2)
print(type(s1))
print(type(s2))
xianyu
xian
<class 'str'>
<class 'str'>
s3 = '''xianyu
abaabaaaa
hahhaa'''
print(s3)
print(type(s3))
abaabaaaa
hahhaa
<class 'str'>
算数运算符
n1 = 5
n2 = 10
n3 = 2
print(n1+n2) 15
print(n1-n2) -5
print(n1*n2) 50
print(n1/n2) 0.5
print(n1%n2) 5
# 返回x的n次幂
print(n1 ** n3) 25
# 整除(向小取整)
print(n1 // n3) 2
赋值运算符
n1 = 10
n2 = 20
print("n1=", n1 , "n2=" ,n2) n1= 10 n2= 20
n2 += n1
print("n1=", n1 , "n2=" ,n2) n1= 10 n2= 30
n2 /= n1
print("n1=", n1 , "n2=" ,n2) n1= 10 n2= 3.0
n2 *= n1
print("n1=", n1 , "n2=" ,n2) n1= 10 n2= 30.0
n2 **= n1
print("n1=", n1 , "n2=" ,n2) n1= 10 n2= 590490000000000.0
n2 //= n1
print("n1=", n1 , "n2=" ,n2) n1= 10 n2= 59049000000000.0
n2 %= n1
print("n1=", n1 , "n2=" ,n2) n1= 10 n2= 0.0
比较运算符
n1 = 10
n2 = 20
print(n1 == n2 )
print(n1 <= n2 )
print(n1 >= n2 )
print(n1 != n2 )
print(n1 > n2 )
print(n1 < n2)
print(type(n1 == n2))
逻辑运算符、成员运算符
#逻辑运算符:and or not
n1 = True
n2 = False
print(n1 and n2)
print(n1 or n2 )
print(not n1 )
#成员运算符:in 、not in
s1 = "xianyu"
print('x' in s1)
print('y' not in s1)
类型转换,python里面不能直接拼接,要先强转
n1 = 1
print(n1) 1
print(n1 +int( "1")) 2
print(str(n1) + "1") 11
print((int("123") + 20))143
流程控制
1.分支结构:if
2.循环:
for、while
分支结构
'''
if_stmt ::= "if" assignment_expression ":" suite
("elif" assignment_expression ":" suite)*
["else" ":" suite]
'''
score = float(input("please input a number"))
if(score < 60):
print("不及格")
elif(score<80):
print("良")
elif (score <= 100):
print("优秀")
else:
print("输入有误")
循环
'''
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
'''
l1 = [1,2,3,4,5,"abc","456"]
for el in l1:
print(el)
else:
print("xxx")
'''
while_stmt ::= "while" assignment_expression ":" suite
["else" ":" suite]
'''
flag = float(input("please input a number:"))
while(flag == 1 ):
score = float(input("please input a number:"))
if(score < 60):
print("不及格")
elif(score<80):
print("良")
elif (score <= 100):
print("优秀")
else:
print("输入有误")
# continue
else:
print("退出while")
L1 = [1,2,3,4,5]
for el in L1:
if(el == 3):
print(el)
break
else:
print(el)
L2 = [1,2,3,4,5]
for el in L2:
if(el == 3):
continue
print(el)
else:
print(el)
常见数据结构
1.str :
1.常用的函数
2.字符串插值
2.集合:
List :有序的,数据可重复【列表】
Set :无序的 数据不可重复 【集合】
tuple:【元组】
dict: kv 【字典】 =》 kv
Java中的集合有哪些
java 集合有哪些:
Collection:
List: 存储元素,有序、可重复
Set: 存储元素,不可重复、无序
Map:
kv:存储一对一对的数据
字符串
s1 = "xianyu"
s2 = "lao"
print(s1 + s2) xianyulao
# 函数
print(len(s1)) 6
# 转义字符
s3 = "spa\nrk"
print(s3) spa
rk
# 不转义加个R就可以
s4 = r"spa\nrk"
print(s4) spa\nrk
# self可以理解为this
print(s1.upper()) XIANYU
print(s2.lower()) lao
# 直接替换为什么不变?因为String是不可变的
# 相当于在内存中申请一个内存空间
# 或者将其覆盖,或者开辟新空间,不能直接修改这块内存空间
s5 = "abcDDD"
s5.replace("D","d")
print(s5) abcDDD
s5 = s5.replace("D","d")
s6 = s5.replace("D","d")
print(s5+"---"+s6) abcddd---abcddd
s7 = "a,a,a,b,b,b"
L2 = s7.split(",")
print(L2) ['a', 'a', 'a', 'b', 'b', 'b']
# 字符串差值
name = "xianyu"
age = 18
# print(name,age)
print("name is :" + name ,"age is :",age) name is :xianyu age is : 18
# 字符串差值,加f,或者是使用format方法
print(f"name is {name} age is {age}") name is xianyu age is 18
"name is:{0},age is:{1}".format(name,age)
python中的集合
List
l1 = [1,2,3,4,"abc","456"]
for el in l1 :
print(el)
else:
print("xxx")
L1 = [1,2,3,4,"abc"]
#1.取值
print(L1[0])
print(L1[0:3]) #左闭右开
print(L1[-1])
# 2.赋值
L1[0] = "xianyu"
print(L1)
# 3.添加值
L1.append(1)
L1.append("abc")
print(L1)
# 4.移除
L1.pop(0)
print(L1)
L1.remove("abc")
print(L1)
# 删除对比
# del 内存空间变小
# pop 内存空间存在
del L1[0]
print(len(L1))
print(L1.__len__())
L2 = [1,2,3,4,1,6,5]
print(L2)
L2.sort()
print(L2)
L2.sort(reverse=1)
print(L2)
L3 =sorted(L2,reverse=True)
print(L3)
元组tuple
# 元组 使用小括号 ,列表使用 方括号
# 注意: 元组里面的元素是不能被修改的
t1 = (1,1,1,1,2,3,"xy","sf","aba")
print(t1)
print(type(t1))
# 1.取值
print(t1[0])
print(t1[0:3])
# 添加值
print(t1 + (1, 2, 3))
t2 = t1.__add__((1,2,3))
print(t2)
# ctrl + p 报提示
n = t1.index("xy")
print(n)
m = t1.count(1)
print(m)
Set
# set: 不可重复 无序
s1 = {1,1,1,2,3,4,5,"abc"}
print(s1)
# 1.添加元素
s1.add(10)
s1.add("xianyu")
print(s1)
s1.remove(10)
print(s1)
s1.pop() #删除第一个
print(s1)
# 交集,并集
print(s1 & {2, 3, 4}) #交集
print(s1 | {2, 3, 4}) #并集
字典dict
# 字典,kv
# ctrl + r 批量替换
d = {"name":"xianyu","age":21,"city":"沈阳"}
print(d)
type
# 1.取值
print(d.get("name"))
# 2.添加kv
d["gender"] = '男'
print(d)
# 修改值
d["age"] = 18
print(d)
# 删除
d.pop("name")
print(d)
# 函数,item可以理解为元素,用tuple维护
print(d.items())
print(d.keys())
print(d.values())
for k,v in d.items():
print(f"k is {k} , values is {v}")
常用函数
# 常用函数
# 1.返回List下标+value
L1 = [1,2,3,4,5,6]
for i,v in enumerate(L1):
print(i,v)
# 2.将多个list进行匹配,形成一个
questions = ['name', 'quest', 'favorite color']
answers = ['lancelot', 'the holy grail', 'blue']
print(zip(questions,answers)) #输出地址
print(list(zip(questions,answers))) #输出list
for q,a in zip(questions,answers):
print(f"whats your {q} + my is {a}")
# 3.reversed() 对元素降序排列
for i in reversed(range(1, 10, 2)):
print(i)
# 大部分代码都是左闭右开
print()
for el in range(1, 10,2):
print(el)
# reversed()
推导式
# 推导式原版
l1 = []
for el in range(5):
l1.append(el)
print(l1)
# 7.推导式
l2 = [el for el in range(5)]
print(l2)
# set集合推导式
set3 = {el for el in range(5) if el >3}
print(set3)
# 字典推导式
d1 = {key:key*2 for key in range(10) if key<3}
print(d1)
# 函数,异常,导包,面向对象
# python + 爬虫
# java scala可以
# 额外学习 sparkStreaming,已停更
# Flink,支持flinksql,尚硅谷可以
# structed streaming
# 数仓,维度和指标
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话