学习目标:python基础语法(1)
学习内容:
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 版的开发
3.python 语法
1.基本数据结构
1.数值型
int 整型
float 小数
2.字符串
str
1.注释
1.单行注释 #
2.多行注释 :
'''
xxxx
'''
"""
xxxx
"""
2.数值类型
1.int
n1 = 10
print(n1)
print(type(n1))
2.float
n2 = 10.1
print(n2)
print(type(n2))
3.str:
1.单引号 或者双引号 【普通字符串的定义】
'a' "abc"
2.多行字符串【跟多行注释有点类似】
"""
asdas
"""
s1 = ‘longshao’
s2 = “qiaoshao”
print(s1)
print(s2)
print(type(s1))
print(type(s2))
s3 = ‘’’
xinwei 说咱班美女多,我说 真的多
‘’’
print(s3)
print(type(s3))
4.运算符
算数运算符:+、-、*、/、%、**、//:
n1 = 5
n2 = 10
n3 = 2
print(n1 + n2)
print(n1 - n2 )
print(n1 * n2 )
print(n1 / n2 )
print(n1 % n2 )
print(n1 ** n3 )
print(n1 // n3 )
比较运算符: == < > != <= >=
1.python 中:
bool
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))
赋值运算符:= += -= /= %= //= **=
n1 = 10
n2 = 20
print(n1)
print(n2)
print(“n1=”,n1,‘n2=’,n2)
n2 +=n1
print(“n1=”,n1,‘n2=’,n2)
n2 -=n1
print(“n1=”,n1,‘n2=’,n2)
n2 /=n1
print(“n1=”,n1,‘n2=’,n2)
n2 *=n1
print(“n1=”,n1,‘n2=’,n2)
n2 **=n1
print(“n1=”,n1,‘n2=’,n2)
n2 //=n1
print(“n1=”,n1,‘n2=’,n2)
n2 %= n1
print(“n1=”,n1,‘n2=’,n2)
逻辑运算符:and or not
n1 = True
n2 = False
print(n1 and n2)
print(n1 or n2 )
print(not n1 )
成员运算符:in 、not in
s1 = “ningshao”
print(‘n’ in s1)
print(‘x’ not in s1)
5.流程控制:
1.分支结构:if
2.循环:
for、while
1.if
if_stmt ::= “if” assignment_expression “:” suite
(“elif” assignment_expression “:” suite)*
[“else” “:” suite]
类型转换:
# 1.类型转换
n1 = 1
print(n1)
print(n1+“1”)
print(n1,“1”)
print(str(n1) + “1”)
print(int(“123”) + 20)
if:
score = float(input(“请输入一个数字:”))
if(score<60):
print(“不及格”)
elif (score<80):
print(“良”)
elif (score <=100):
print(“优秀”)
else:
print(“输入有误”)
2.for
for_stmt ::= “for” target_list “in” expression_list “:” suite
[“else” “:” suite]
java 集合有哪些:
Collection:
List: 存储元素,有序、可重复
Set: 存储元素,不可重复、无序
Map:
kv:存储一对一对的数据
l1 = [1,2,3,4,“abc”,“456”]
for el in l1 :
print(el)
else:
print(“xxx”)
3.while
while_stmt ::= “while” assignment_expression “:” suite
[“else” “:” suite]
flag = float(input(“请输入一个数字:”))
while(flag == 1):
score = float(input(“请输入一个数字:”))
if (score < 60):
print(“不及格”)
elif (score < 80):
print(“良”)
elif (score <= 100):
print(“优秀”)
else:
print(“输入有误”)
else:
print(“退出while”)
4.break continue
L1= [1,2,3,4,5]
for el in L1:
if(el == 3):
# break
continue
print(el)
else:
print(el)
6.常见的数据结构
1.str :
1.常用的函数
2.字符串插值
2.集合:
List :有序的,数据可重复【列表】
Set :无序的 数据不可重复 【集合】
tuple:【元组】
dict: kv 【字典】 =》 kv
1.str
s1 = “NingShao”
s2 = “XinWei”
print(s1)
print(s2)
print(s1 + s2)
转义
s3 = “flink\nspark”
print(s3)
s4 =r"flink\nspark"
print(s4)
函数
print(len(s1))
print(len(s2))
print(s1.upper())
print(s2.lower())
s5= “abcDDD”
s5 = s5.replace(‘D’,“d”)
print(s5) #abcddd =>1 2
s6 = “a,a,a,b,b,b”
L2 = s6.split(",")
print(L2)
字符串插值
name = “ningshao”
age = 40
print(name,age)
print("name is : "+name,"age is : “,age)
print(f"name is {name} , age is {age}”)
print(“name is {0} , age is {1}”.format(name, age))
2.集合:
List : 有序的集合,元素可以重复
list 类型
L1 = [1,2,3,4,“abc”]
1.取值
print(L1[0])
print(L1[0:3]) #左闭右开
print(L1[-1])
2.设置值
L1[0]=“ningshao”
print(L1)
3.添加值
L1.append(1)
L1.append(“xuanxuan”)
L1.append(1)
print(L1)
4.移除值
L1.pop(0)
print(L1)
L1.remove(“xuanxuan”)
print(L1)
del L1[0]
print(L1)
5.函数
print(len(L1))
print(L1.len())
L2 = [1,2,3,4,1,6,5]
print(L2)
L2.sort()
print(L2)
L2.sort(reverse=True)
print(L2)
l3 = sorted(L2,reverse=False)
print(l3)
2.tuple 元组
元组 使用小括号 ,列表使用 方括号
注意:
元组里面的元素是不能被修改的
tuple
t1 = (1,1,1,12,3,“ns”,“zhege”,“xw”)
print(t1)
print(type(t1))
# 1.取值
print(t1[0])
print(t1[0:3])
# 2.添加值
t2 = t1.__add__((1,2,3))
print(t2)
print(t1 + (1, 2, 3))
# 2.函数
print(len(t1))
n = t1.index("zhege")
print(n)
m = t1.count(1)
print(m)
-
3.set
-
不可重复 无序
s1 = {1,1,1,2,3,4,5,“abc”}
print(s1 )
1.添加元素
s1.add(10)
s1.add(“zs”)
print(s1)
s1.remove(10)
print(s1)
s1.pop()
s1.pop()
print(s1)
交 并
print(s1 & {2, 3, 4})
print(s1 | {2, 3, 4})
4.dict
kv
d = {“name”:“yusang”,“age”:21,“city”:“鞍山”}
print(d)
print(type(d))
1.取值
print(d.get(“name”))
print(d.get(“age”))
print(d.get(“city”))
2.添加kv
d[“gender”]=‘男’
print(d)
d[“age”]=18
print(d)
d.pop(“name”)
print(d)
函数
print(d.items())
print(d.keys())
print(d.values())
for k,v in d.items():
print(f"key is {k} , value is {v}")
集合常用函数:
1.enumerate:返回list 下标+value
l1 = [1,2,3,4]
for index,value in enumerate(l1):
print(index,value)
2.zip():将多个list 匹配 形成 一个
l1 = [“name”,“age”,“gender”]
l2 = [“zs”,21,“女”]
print(zip(l1, l2))
print(list(zip(l1, l2)))
for k,v in list(zip(l1, l2)):
print(“key is {0}, value is {1}”.format(k,v))
3.reversed() :对元素进行 降序
print(range(1, 10))
for el in range(1,10,2):
print(el)
for i in reversed(range(1, 10, 2)):
print(i)
7.推导式
1.python 特性 快速构建 集合
1.list 推导式
[x for x in iterable]
l1 = []
for el in range(5):
l1.append(el)
print(l1)
l2 = [el for el in range(5)]
print(l2)
2.set 推导式
s1 = {el for el in range(10) if el > 4}
print(s1)
3.字典推导式
d1 = {key:key*2 for key in range(10) if key < 3}
print(d1)