安装python运行环境
安装python解释器 安装pyCharm开发工具
注:这俩步如果有问题的话,可以在CSDN里搜索一下,与很多教程)
打印Hello world
交互式命令行
- 终端输入python可进入python交互式命令行;
- 接下来可以在里面输入python代码:print("Hello world")
终端运行脚本
- 使用vs code编写python脚本,输入python代码:print("Hello world"),保存为test.py文件,所有的python文件后缀都是py;
- 终端执行python脚本,输入python 文件名;
变量和注释
变量
变量的意义
- 提取变量,多个地方使用相同变量,如果要修改变量值只需要修改一个地方,方便维护代码;
- 变量可以进行动态变化,比如有个变量存储学生信息,那学生可能换名字或者新增同学,这时候就需要变量的一些方法满足程序的要求;
name="张三"
age=18
print(name) # 张三
print(age) # 18
#多变量赋值更简洁的写法
name,age="张三",18
print(name) # 张三
print(age) # 18
变量命名规则
- 变量名可以包含 字母、数字和下划线,但不能以数字开头;
- 变量名中不能包含空格;
- 变量名不能和关键字重名;
关键字
变量值的变化
a = 1
a = "张三"
print(a) # 张三
注释
单行注释
#注释的内容代码不会执行
多行注释
#单引号多行注释
'''
第一行注释
第二行注释
等等
'''
#双引号多行注释
"""
第一行注释
第二行注释
等等
"""
常用的数据类型
查看数据类型:type函数
数字类型
整型 int
不带小数点的数字,如-1,2,3...
浮点型 float
带小数点的数字,如1.0,-2.2...
数字计算
加 +
减 -
**乘 ***
除 /、//、%
/: 除法,获得小数
//:取商
%: 取余数
**次方 ****
字符串
#字符串带单引号
'张三问李四:"你叫什么?"'
#字符串带双引号
"张三问李四:'你叫什么?'"
#字符串拼接
name = "张三"
hobit = "爱足球"
print(name + hobit) # 张三爱足球
#字符串长度
len(name) # 2
数字与字符串转换
a = 2
b = "10"
d = "10.1"
# 将字符串d转换为float
e = float(d)
print(type(e)) # <class 'float'>
# int(b) 把字符串转换为int
print(a+int(b)) # 12
# str(a) 把数字转换为字符串
print(str(a)+b) # 210
特殊字符
# \n换行
str = "姓名\n张三"
print(str) # 姓名
张三
# \t缩进
str = "姓名\t张三"
print(str) # 姓名 张三
索引
#正数索引 01234
str = "Hello"
#负数索引 -5-4-3-2-1
#取最后一个字符:str[-1]
print(str[-1]) # o
切片
str = "Hello world"
# [起始位置:终点位置:步长], 步长为1可不写
str[0:4:2]
str3="张三在打球"
# str3索引从0到3,4不包含,步长为2
print(str3[0:4:2]) # 张在
# 取第一个到第5个
# 左包含,右不包含
print(str[0:5]) # Hello
# str[起始索引:终止索引+1:步长] 步长好比爬楼梯,如果为1可以省略,每次一步一步走,如果为2,每次爬2个楼梯
print(str[::2]) # Hlo
print(str[0:11:2]) # Hlo
# 取o w
print(str[4:7:1]) # o w
print(str[4:7]) # o w
字符串方法
字符串查找
str = "hello world"
# find方法找到了,返回第一个元素的索引
print(str.find("hello")) # 0
# find方法查不到,返回:-1
print(str.find("111")) # -1
# find是返回查到的第一个元素索引
print(str.find("l")) # 2
# rfind是返回查到的最后一个元素的索引
print(str.rfind("l")) # 9
# index是返回查到的第一个元素索引
print(str.index("l")) # 2
# rindex是返回查到的最后一个元素的索引
print(str.rindex("l")) # 9
# index方法如果没有查到会抛错
print(str.index("112")) # SyntaxError: invalid syntax
# 统计出现的次数
print(str.count("l")) # 3
字符串替换
str = "hello world"
# replace(需要替换的字符串,替换后新的字符串),返回一个新的字符串,不是在原来的字符串里面改
str1 = str.replace("l","L")
print(str1) # heLLo worLd
字符串分隔
str = "hello wor ld"
# 去查找字符串里面有没有" ",找到了, 把空格去掉,把切割的元素放到列表
a = str.split(" ")
print(a) # ['hello', 'wor', 'ld']
names = "张三;李四;王五"
# 去查找字符串里面有没有";",找到了, 把";"去掉,把切割的元素放到列表
nameList = names.split(";")
print(nameList) # ['张三', '李四', '王五']
# 去查找字符串里面有没有1,没有找到,把整个字符串塞到列表
nameList1 = names.split("1")
print(nameList1) # ['张三;李四;王五']
str = "hello world"
# 去字符串查找该分隔符"o", 查找第一个出现的元素,就把分隔符左边的所有元素作为第一个元素,分隔符本身作为第二个元素,分隔符右边的作为第三个元素
str1 = str.partition("o")
print(str1) # ('hell', 'o', ' wor ld')
# 去字符串查找该分隔符"lll",没有找到,把整个字符串作为第一个元素,后面两个元素就是空字符串
str2 = str.partition("lll")
print(str2) # ('hello world', '', '')
字符串第一个字母大写
str = "hello world"
str1 = str.capitalize()
print(str1) # Hello world
字符串每一个单词字母大写
str = "hello world"
str2 = str.title()
print(str2) # Hello World
判断字符串是否以某个字符串开头或结尾,返回的结果为bool类型:True/False
str = "hello world"
print(str.startswith("hello")) # True
print(str.startswith("hello1")) # False
print(str.endswith("d")) # True
print(str.endswith("l")) # False
字符串全部转换大小写
str = "hello world"
str1 = str.upper()
print(str1) # HELLO WORLD
str = "HELLO WORLD"
str1 = str.lower()
print(str1) # hello world
字符串去除特定字符,去除两边的特定字符,中间的不会去除
str = "00000003210Runoob01230000000"
# 去除首尾两边的字符串
print(str.strip("0")) # 3210Runoob0123
# 去除左边的字符串
print(str.lstrip("0")) # 3210Runoob01230000000
# 去除右边的字符串
print(str.rstrip("0")) # 00000003210Runoob0123
### 中间的字符串是不能去除的
print(str.strip("R")) # 00000003210Runoob01230000000
字符串类型判断
str = "helloworld1232"
# 判断是否是数字,若字符串中只包含数字,则返回 True。反之,则返回 False
print(str.isdigit()) # False
# 判断是否是字母,若字符串中只包含字母,则返回 True。 反之,则返回 False
print(str.isalpha()) # False
# 判断是否数字或字母,若字符串中至少有一个字符并且所有字符都是字母或数字,则返回 True。反之,则返回 False
print(str.isalnum()) # True
spa = " "
# 判断字符串是否是空格,若字符串中只包含空格,则返回True。反之,则返回 False
print(spa.isspace()) # True
列表里面的元素进行字符串拼接
# 列表里面的元素进行字符串拼接
a = ["张三","李四","王五"]
str = "he".join(a)
print(str) # 张三he李四he王五
布尔表达式
a = 1
b = 2
print(1 == 1) # True
print(1>2) # False
print(a == 1 and b == 2) # True
# and表示且,两边都是true才会是true
print(a == 2 and b == 2) # False
# or表示或,两边只要有一个是true,就是true
print(a == 2 or b == 2) # True
# !=表示不等于
print(a != 2) # True
从客户端输入的内容都是字符串类型
num = input("请输入数字:")
print(type(num)) # <class 'str'>
#将字符串转换为int: int(num)
print(type(int(num))) # <class 'int'>