Day02 课程

课程:https://www.bilibili.com/video/BV1o4411M71o?spm_id_from=333.788.videopod.episodes&p=22

2.1 认识格式化符号

2.1.1 目标

 格式化输出

  格式化符号

  f-字符串

 print的结束符

2.1.2 输出

作用:程序输出内容给用户

print("hello Python")

age = 18
print(age)

# 需求:输出"今年我的年龄是 18 岁"

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\01-格式化输出.py 
hello Python
18

Process finished with exit code 0

2.1.3 格式化输出

所谓的格式化输出即按照一定的格式输出内容

(1)格式化符号

格式符号 转换 备注
%s 字符串 常用:str
%d 有符号的十进制整数 常用:(int 十进制整数:0、1、2、3,有符号的正整数、负整数  +1、 -1 )
%f 浮点数 常用:float
%c 字符  
%u 无符号十进制整数 (无符号的整数,前面不能加符号,也就是正整数,只能格式化输出正整数 )
%o 八进制整数  
%x 十六进制整数(小写ox)  
%X 十六进制整数(小写OX)  
%e 科学计数法(小写'e')  
%E 科学计数法(大写'E')  
%g %f 和 %e 的简写  
%G %f 和 %E 的简写  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

技巧:

  %06d,表示输出的整数显示位数,不足时以0补全,超出当前位数则原样输出

  %.2f,表示小数点后显示的小数位数

2.2 格式化符号基础使用方法

"""
1.准备数据
2.格式化符号输出数据
"""

age = 18
name = "TOM"
weight = 75.5

# 1.今年我的年龄是x岁 -- 整数 %d
print("今年我的年龄是%d岁" % age)

# 2.我的名字是x -- 字符串 %s
print("我的名字是%s" % name)

# 3.我的体重是x公斤 -- 浮点数 %f
print("我的体重是%f公斤" % weight)  # f 默认保留 6 位小数
print("我的体重是%.2f公斤" % weight)  # .2f 小数点后保留两位小数

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\01-格式化输出.py 
今年我的年龄是18岁
我的名字是TOM
我的体重是75.500000公斤
我的体重是75.50公斤

Process finished with exit code 0

2.3 格式化符号高级使用方法

age = 18
name = "TOM"
weight = 75.5
stu_id = 1
test_id = 1111

# 4.我的学号是x -- 整数 %d
print("我的学号是%d" % stu_id)

# 4.1 我的学号是001 (学生太多时,输出对不齐,001/011/111 才会对齐)
print("我的学号是%d" % test_id)
print("我的学号是%03d" % stu_id)  # 位数不足三位,前面用 0 补全,多位补全
print("我的学号是%02d" % test_id)  # 位数不足两位,前面用 0 补全, 超过两位,输出原位数

# 5.我的名字是x,今年x岁了
print("我的名字是%s,今年%d岁了" % (name, age))  # 多个变量使用括号,按变量顺序使用逗号隔开

# 5.1 我的名字是x,明年x岁了
print("我的名字是%s,明年%d岁了" % (name, age + 1))

# 6.我的名字是x,今年x岁了,体重x公斤,学号是x
print("我的名字是%s,今年%d岁了,体重%.2f公斤,学号是%06d" % (name, age, weight, stu_id))

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\02-格式化高级输出.py 
我的学号是1
我的学号是1111
我的学号是001
我的学号是1111
我的名字是TOM,今年18岁了
我的名字是TOM,明年19岁了
我的名字是TOM,今年18岁了,体重75.50公斤,学号是000001

Process finished with exit code 0

2.4 扩展格式化字符串

# %s 可以将数字和浮点数等以字符串输出
age = 18
name = "TOM"
weight = 75.5

# 我的名字是x,今年x岁了,体重x公斤,学号是x
print("我的名字是%s,今年%d岁了,体重%.02f公斤" % (name, age, weight))  # 原输出方式
print("我的名字是%s,今年%s岁了,体重%s公斤" % (name, age, weight))  # 现在输出方式

# print(f"我的名字是{name},今年{age}岁了,体重{weight}公斤")  #

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\03-格式化字符串扩展.py 
我的名字是TOM,今年18岁了,体重75.50公斤
我的名字是TOM,今年18岁了,体重75.5公斤

Process finished with exit code 0

2.5 f-格式化字符串

格式化字符串除了 %s,还可以写为 f'{表达式}'

age = 18
name = "TOM"

# 我的名字是x,今年x岁了
print("我的名字是%s,今年%d岁了" % (name, age))  # 原 %s 输出模式

# 语法 f'{表达式}'
print(f"我的名字是{name},今年{age}岁了")
print(f"我的名字是{name},明年{age + 1}岁了")

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\04-f格式化字符串.py 
我的名字是TOM,今年18岁了
我的名字是TOM,今年18岁了
我的名字是TOM,明年19岁了

Process finished with exit code 0

注意: f 格式化字符串是 Python3.6 中新增的格式化方法,该方法更简单易读

2.6 转义字符

\n:换行

\t:制表符,一个tab键(即4个空格)的距离

注意称谓: ( \ :反斜杠、/ :斜杠)

# 想要 hello world 换行输出
print("hello")  # 原始写法
print("world")

print("hello\nPython")  # 使用 \n 换行写法

# 使 hello 与 Python 间有空格
print("hello\tPython")
print("\tabcd")  # \t 制表符 四个空格距离 'hell'

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\05-转义字符.py 
hello
world
hello
Python
hello    Python
    abcd

Process finished with exit code 0

2.7 print结束符

# 语法
print
("输出的内容", end="\n")
print("hello")  # 默认 \n
print("world",end="\n")  # 与上面不写作用相同, \n 为默认值
print("hello",end="\t")  # 可修改 \n 默认值,改为其他转义字符
print("python",end="---")  # 可修改其他符号,可以自定义
print("嘿嘿嘿")

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\06-print的结束符.py 
hello
world
hello    python---嘿嘿嘿

Process finished with exit code 0

在Python中,print(),默认自带 end="\n" 这个换行结束符,所以导致每两个 print 直接会换行展示,用户可以按需求更改结束符

2.8 总结

 格式化符号

  %s:格式化输出字符串

  %d:格式化输出整数

  %f:格式化输出浮点数

 f-字符串

  f'{表达式}'

 转义字符

  \n:换行

  \t:制表符

 print结束符

2.9 了解输入

2.9.1 目标

  输入功能的语法

  输入input的特点

2.9.2 输入

在 Python 中,程序接收用户输入数据的功能即是输入

(1)输入语法

input("提示信息")

(2)输入的特点

 当程序执行到 input,等待用户输入,输入完成之后才继续向下执行

 当 Python中,input 接收用户输入后,一般存储到变量,方便使用

 在 Python中,input 会把接收到的任意用户输入的数据都当做字符串处理

2.10 输入功能实现

"""
1.书写 input
    input("提示信息")
2.观察特点
    2.1 遇到input,等待用户输入
    2.2 接收input存变量
    2.3 input接收到的数据类型都是字符串
"""

password = input("请输入密码:")  # 等待输入后,才继续向下执行
print(f"输入的密码是{password}")  # 存储在变量中,方便使用
print(type(password))  # 输入是字符串类型

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\07-input输入.py 
请输入密码:123456
输入的密码是123456
<class 'str'>

Process finished with exit code 0

2.11 体验数据类型转换

2.11.1 目标

  数据类型转换的必要性

  数据类型转换常用方法

2.11.2 转换数据类型的作用

问:input() 接收用户输入的数据都是字符串类型,如果用户输入 1,想得到整型该如何操作?

答:转换数据类型既可,即将字符串类型转换成整型

函数 说明 备注
int(x[,base]) 将x转换为一个整数  
float(x) 将x转换为一个浮点数  
complex(real [,imag]) 创建一个复数,real 为实部,imag为虚部  
str(x) 将对象x转换为字符串  
repr(x) 将对象 x 转换为表达式字符串  
eval(str) 用来计算在字符串中的有效Python表达式,并返回一个对象 转换成它原本的数据类型
tuple(s) 将序列 s 转换为一个元祖  
list(s) 将序列 s 转换为一个列表  
chr(x) 将一个整数转换为一个Unicode字符  
ord(x) 将一个字符转换为它的ASCII整数值  
hex(x) 将一个整数转换为一个十六进制字符串  
oct(x) 将一个整数转换为一个八进制字符串  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

"""
1. input输入
2.检测input数据类型str
3.int() 转换数据类型
4.检测是否转换成功
"""

num = input("请输入数字:")
print(num)

print(type(num))  # 返回str 字符串类型

print(type(int(num)))  # 返回int 数字类型

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\08-快速体验数据类型转换.py 
请输入数字:123456
123456
<class 'str'>
<class 'int'>

Process finished with exit code 0

2.12 数据类型转换函数

# 1. float() -- 将数据转换成浮点型
num1 = 1
print(type(num1))  # int 类型
print(type(float(num1)))  # 将数字int 类型转换为浮点数float
print(float(num1))  # 转换后 变为了浮点数的 1.0

str1 = '10'
print(type(str1))  # str 类型
print(type(float(str1)))  # 将字符串str 类型转换为浮点数float
print(float(str1))  # 转换后 变为了浮点数的 10.0

# 2. str() -- 将数据转换成字符串类型
print(type(str(num1)))

# 3. tuple() -- 将一个序列转换成元祖
list1 = [10, 20, 30]
print(type(list1))  # list 类型
print(type(tuple(list1)))  # 将列表list 类型转换为元祖tuple类型
print(tuple(list1))

# 4. list() -- 将一个序列转换成列表
t1 = (100, 200, 300)
print(type(t1))  # tuple 类型
print(type(list(t1)))  # 将元祖tuple 类型转换为列表list类型
print(list(t1))

# 5. eval() -- 计算字符串中的有效Python 表达式,并返回一个对象  转换成它原本的类型
str2 = "1"
str3 = "1.1"
str4 = "(1000, 2000, 3000)"
str5 = "[1000, 2000, 3000]"

print(type(eval(str2)))
print(type(eval(str3)))
print(type(eval(str4)))
print(type(eval(str5)))

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\09-数据类型转换函数.py 
<class 'int'>
<class 'float'>
1.0
<class 'str'>
<class 'float'>
10.0
<class 'str'>
<class 'list'>
<class 'tuple'>
(10, 20, 30)
<class 'tuple'>
<class 'list'>
[100, 200, 300]
<class 'int'>
<class 'float'>
<class 'tuple'>
<class 'list'>

Process finished with exit code 0

2.13 总结数据类型的转换

 转换数据类型常用的函数

  int()

  float()

  str()

  list()

  tuple()

  eval()

2.14 PyCharm 交互式开发

注意:简单的测试在交互式环境开发中测试,生产业务代码需要留存,在文件中书写

image

2.15 运算符的分类

2.15.1 目标

  掌握常用运算符的作用

2.15.2 运算符的分类

  算数运算符(数学中加减乘除)

  赋值运算符(定义变量时)

  复合赋值运算符

  比较运算符(判断中 > 、 < 等)

  逻辑运算符(或、且)

2.16 算数运算符

运算符 描述 实例 备注
+ 1 + 1 输出结果为 2  
- 1 - 1 输出结果为 0  
* 2 * 2 输出结果为 4  
/ 10 / 2 输出结果为 5  
// 整除 9 // 4 输出结果为 2 取除后整数的商
% 取余 9 % 4 输出结果为 1  
** 指数 2 ** 4 输出结果为 16,即 2 * 2 * 2 * 2  
() 小括号 小括号用来提高运算优先级,即 (1 + 2)*3 输出结果为 9  

 

 

 

 

 

 

 

 

 

 

注意:混合运算优先级顺序:() 高于 ** 高于 * / // % 高于 + -

C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe "D:/sofaward/pycharm/PyCharm 2024.3.6/plugins/python-ce/helpers/pydev/pydevconsole.py" --mode=client --host=127.0.0.1 --port=4989 
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['D:\\Pycharm\\code'])
PyDev console: starting.
Python 3.13.7 (tags/v3.13.7:bcee1c3, Aug 14 2025, 14:15:11) [MSC v.1944 64 bit (AMD64)] on win32
1 + 1
2
1 + 1.1 
2.1         # 加减法中,有浮点数,结果类型为浮点数
1 - 1
0
1 - 0.5
0.5
2 * 3
6
2 * 0.5
1.0
4 / 2
2.0          # 除法中,结果类型为浮点数
9 // 4
2
9 % 4
1
2 ** 3
8
1 + 2 * 3
7
(1 + 2) * 3
9
2 * 3 ** 2
18            # ** 优先级高于 *
(2 * 3) ** 2
36            # () 优先级高于 **

2.17 赋值运算符

运算符 描述 实例
= 赋值 将 = 右侧的结果赋值给等号左侧的变量

 

 

 

  单个变量赋值

num = 1
print(num)

  多个变量赋值

# 按照前后顺序赋值,用,分隔
num1, float1, str1 = 10, 0.5, 'hello world'
print(num1)
print(float1)
print(str1)

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\10-赋值运算符.py 
10
0.5
hello world

Process finished with exit code 0

  多变量赋相同的值

# 100赋值给 a 、100赋值给b
a = b = 10
print(a)
print(b)

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\10-赋值运算符.py 
10
10

Process finished with exit code 0

2.18 复合赋值运算符

运算符 描述 实例
+= 加法赋值运算符 c += a 等价于 c = c + a
-= 减法赋值运算符 c -= a 等价于 c = c - a
*= 乘法赋值运算符 c *= a 等价于 c = c * a
/= 除法赋值运算符 c /= a 等价于 c = c / a
//= 整除赋值运算符 c //= a 等价于 c = c // a
%= 取余赋值运算符 c %= a 等价于 c = c % a
**= 幂赋值运算符 c **= a 等价于 c = c ** a

 

 

 

 

 

 

 

 

 

a = 10
a += 1  # 等价于 a = a + 1
print(a)

b = 10
b -= 1  # 等价于 b = b - 1
print(b)

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\10-赋值运算符.py 
11
9

Process finished with exit code 0

2.19 复合赋值注意点

# 注意: 先算复合赋值运算符右面的表达式,再算复合赋值运算
c = 10
# 两种方式 c = 10 + 1 + 2  错
# 两种方式 c += 3 -> c = c + 3  对
c += 1 + 2
print(c)

d = 10
# 两种方式 d = 10 * 1 + 2  错
# 两种方式 d *= 3 -> d = d * 3  对 <-- 这种方式
d *= 1 + 2
print(d)

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\10-赋值运算符.py 
13
30

Process finished with exit code 0

2.20 比较运算符

注意:比较运算符也叫关系运算符,通常用来判断

运算符 描述 实例
== 判断相等,如果两个操作数的结果相等,则条件结果为真(True),否则条件结果为假(False) 如 a = 3,b = 3,则(a == b)为 True
!= 不等于,如果两个操作数的结果不相等,则条件为真(True),否则条件结果为假(False)

如 a = 1,b = 3,则(a != b)为 True

如 a = 3,b = 3,则(a != b)为 False

> 运算符左侧操作数结果是否大于右侧操作数结果,如果大于,则条件为真,否则为假 如 a = 7,b = 3,则(a > b)为 True 
< 运算符左侧操作数结果是否小于右侧操作数结果,如果小于,则条件为真,否则为假 如 a = 7,b = 3,则(a < b)为 False
>= 运算符左侧操作数结果是否大于等于右侧操作数结果,如果大于等于,则条件为真,否则为假

如 a = 7,b = 3,则(a <= b)为 False

如 a = 7,b = 3,则(a >= b)为 True

<= 运算符左侧操作数结果是否小于等于右侧操作数结果,如果小于等于,则条件为真,否则为假 如 a = 3,b = 3,则(a <= b)为 True

 

 

 

 

 

 

 

 

 

 

1 == 1
True
1 != 1
False
1 == 2
False
1 != 2
True

2.21 逻辑运算符

运算符 逻辑表达式 描述 实例 备注
and x and y 布尔"与":如果 x 为 False,x and y 返回 False,否则它返回 y 的值

True and True,返回 True 

True and False,返回 False

且,并且

两边都为真才为真

两边有一个假就为假

or x or y 布尔"或":如果 x 为 Ture,它返回 False,否则它返回 y 的值

False or False,返回 False

False or True,返回 True

或,

两边都为假才为假

两边只要有一个真就为真

not not x

布尔"非":

  如果 x 为 Ture,返回 False

  如果 x 为 False,返回 Ture

not True 返回 False

not False 返回 True

取反

 

 

 

 

 

 

 

 

 

 

a = 0
b = 1
c = 2

# 1. and: 与  都真才真,有一个假则为假
print(a < b and c > b)  # 0 < 1 and 2 > 1 即 True and True
print(a > b and c > b)  # 0 > 1 and 2 > 1 即 False and True

# 2. or: 或  有一个真则为真,都假才假
print(a < b or c > b)  # 0 < 1 or 2 > 1 即 True and True
print(a > b or c > b)  # 0 > 1 or 2 > 1 即 False and True

# 3. not: 非  取反
print(not False)  # 取反后为 True
print(not c > b)  # not 2 > 1 即 not True

------------------------------------------------ 执行后
C:\Users\马俊南\AppData\Local\Microsoft\WindowsApps\python3.13.exe D:\Pycharm\code\day02\11-逻辑运算符.py 
True
False
True
True
True
False

Process finished with exit code 0

2.22 逻辑运算符书写习惯

# 工作中 and、or、not 前后的表达式过于复杂,加上括号,避免有歧意
a = 1
b = 2
c = 3
print((a < b) and ( b < c))  # 1 < 2 and 2 < 3 即 True and True 得 True
print((a > b) and ( b < c))  # 1 > 2 and 2 < 3 即 False and True 得 False
print((a > b) or ( b < c))  # 1 > 2 or 2 < 3 即 False or True 得 True
print(not ( a > b))  # not 1 > 2 即 not False 得 True

2.23 数字的逻辑运算

# and 运算符,只要有一个值为 0,则结果为 0,否则结果为最后一个非0数字
1 and 0
0
0 and 1
0
1 and 2
2
2 and 1
1
# or 运算符,只有所有值为0结果才为0,一个0一个非0,返回非0数字,两个非0,结果为第一个非0数字
0 or 1
1
0 or 0
0
1 or 2
1
2 or 1
2

2.24 运算符总结

 1.算数运算的优先级

  混合运算优先级顺序:() 高于 ** 高于 * / // % 高于 + -

 2.赋值运算符

  =

 3.复合赋值运算符

  +=

  -=

  优先级

   1.先算复合赋值运算符右侧的表达式

   2.再算复合赋值运算的算数运算

   3.最后算赋值运算

 4.比较运算符

  判断相等:==

  大于等于:>=

  小于等于:<=

  不等于:!=

 5.逻辑运算符

  与:and

  或:or

  非:not

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2025-09-05 14:43  马俊南  阅读(2)  评论(0)    收藏  举报