'hello' + 'meining'
软件以及依赖安装
软件或依赖 | 版本 | 安装方式 | 作用 | 配置环境变量 |
anaconda | 4.10.1 | 软件安装 | 直接使用别人写好的功能 | D:\dev\Anaconda3 D:\dev\Anaconda3\Scripts D:\dev\Anaconda3\Library\bin |
Python | 3.8.10 | 软件安装 | Python解释器 | D:\dev\Python\Python38\Scripts\ D:\dev\Python\Python38\ |
pycharm | 2022.3.3 | 软件安装 | 编写代码 |
1.Python基础
1.打印语句
print("hello")
2.注释
# print("注释的内容不执行")
# "hello"
print("没有注释的内容才执行")
3.内置函数
内置函数 |
---|
len() input() |
# 查看长度len
len("hello")
4.从用户获得输入的内容
a = input("请输入内容:")
print(a)
5.变量
a = 2
print(a)
6.数据类型
6.1字符串
6.1.1新建
"hello"
'hi'
6.1.2查
# type('hello')
# len('hello')
# 'l' in 'hello'
'hello'.count('l')
'hello'[2]
# 倒数开始
'hello'[-1]
# 查看索引
'hello'.index('h')
# 取多个元素,包左不包右
'hello'[1:4]
# 省略顶到头
'hello'[1:]
6.1.3改
'hello!nihao'.split('!')
'hello'.replace('l','o')
6.1.4增
'hello' + 'meining'
'meining'*3
6.1.5删
# str.replace(原e,新e)
'meining'.replace('i','c')
6.2 数值:整数
123
6.3 数值:浮点数
1.23
6.4算术运算符
# 取整数
12 // 5
# 取余
12 % 5
6.5 数据类型转换
# 转换为整数
int(1.2)
int('2')
# 转换为浮点数
float(1)
float('2.2')
# 转换为字符串
str(1)
str(2.2)
6.6布尔值boolean
True
False
6.7 布尔值逻辑运算¶
- and: 与
- or: 或
- not: 非
先算not,再算and,最后or
False and True
False or True
not True
True or not False and False or True
6.8 比较运算符
==
!=
>
<
>=
<=
7 有序列表:List
[]
7.1 新建
['meining','waner',19,'荒']
7.2 查
type(['meining','waner',19,'荒'])
len(['meining','waner',19,'荒'])
'meining' in ['meining','waner',19,'荒']
['meining','waner',19,'荒'].count(19)
['meining','waner',19,'荒'][1]
['meining','waner',19,'荒'].index('meining')
['meining','waner',19,'荒'][0:3]
['meining','waner',19,'荒'][0:]
7.3 改
w = ['mn','we',19,'mds','凡']
w[4] = 'zin'
w
7.4 增
# 末尾添加
w = ['mn','we',19,'mds','zin']
w.append('qiny')
w
# 插入到索引之前elem
w = ['mn','we',19,'mds','zin']
w.insert(3,'qiny')
w
w = ['mn','we',19,'mds','zin']
more_w = ['hle','mya','ziy']
# 批量增加
w.extend(more_w)
w
7.5 删
# 连续删除元素
w = ['mn','we',19,'mds','zin']
w[1:4]=[]
w
# 删除某个元素 list.remove(e)
w = ['mn','we',19,'mds','zin']
w.remove('we')
w
# pop: list.pop(index)
w = ['mn','we',19,'mds','zin']
w.pop(2)
w
# 清空list:list.clear()
w = ['mn','we',19,'mds','zin']
w.clear()
w
# del: del list[index]
w = ['mn','we',19,'mds','zin']
del w[2]
w
8 无序键值对:字典dictionary
{key1:value1,key2:value2,...}
8.1 新建
# dict = {key1:value1,key2:value2,...}
w = {'name':'zs','age':28}
w
8.2 查
# dict[key]
w['name']
# dict.get(key)
w.get('name')
# 所有键 dict.keys()
w.keys()
# 所有值 dict.values()
w.values()
# 所有键值对 dict.items()
w.items()
8.3 改update
w = {'name':'zs','age':28}
w['age'] = 18
w
8.4 增
w = {'name':'zs','age':28}
w['school'] = '清华大学'
w
# 合并字典
w = {'name':'zs','age':28}
m = {'school':"清华大学"}
w.update(m)
w
8.5 删
# dict.pop(kye) 返回值
w = {'name':'zs','age':28}
w.pop('age')
w
# dict.popitem() 随机删除 一般为最后一对
w = {'name':'zs','age':28}
w.popitem() # 返回删除的对象
w
# dict.clear() 清空
w = {'name':'zs','age':28}
w.clear()
w
# del dict[key] 删除键值对
w = {'name':'zs','age':28}
del w['name']
w
9 if条件语句
9.1 单个if
judge = False
if judge:
print("及格")
9.2 if...else...
judge = False
if judge:
print("及格")
else:
print("不及格")
9.3 if...elif..else..
score = 45
if score > 90:
print("优")
elif score > 70 and score < 90:
print("良")
elif score > 60 and score < 70:
print("中")
else:
print("差")
10 while 循环语句
i = 1
while i <= 5:
print(i)
i = i + 1
print("循环结束")
11 跳转语句
1.break/continue: 只能在循环中使用
i = 1
while i <= 5:
print(i)
if i == 2:
break
i = i + 1
print("循环结束")
i = 1
while i <= 5:
print(i)
if i == 2:
continue
i = i + 1
print("循环结束")
12 for循环
# 字符串遍历
for i in "meining":
print(i)
# 列表list
ws = ['mn','we',19,'mds','zin']
for w in ws:
print(w)
13 pass 空语句
如果不写pass,很多时候会报错,因为if语句不完整或者定义函数不完整 pass是空语句,保证程序完整性 pass不做任何事情,一般用作占位语句
if 1 < 2:
pass
14 函数
def 函数名(参数...):
def say_hi():
print("hi")
say_hi()
def say_hi(name,age):
print("hi," + name + "!你只有" + str(age) +"岁")
say_hi('meining',18)
14.1 return
def cube(num):
return num**3
print(cube(2))
15 Module and Pip
# !pip install jieba
# !pip install jieba -i https://pypi.tuna.tsinghua.edu.cn/simple
!pip install --upgrade jieba -i https://pypi.tuna.tsinghua.edu.cn/simple