Python学习笔记

MJ大神的Python课,课堂笔记

  • int 和float运算结果是 float
  • 除法(/)的结果是float
  • 整除(//),向下取整(floor)
  • int 和 int 进行整除(//),得到的结果是int

绘制一个填充色+边框色

python 复制代码
import turtle

# 绘制一个填充色+边框色
turtle.hideturtle()
turtle.pencolor('green')
turtle.pensize(10)

turtle.fillcolor('yellow')
turtle.begin_fill()
turtle.circle(100)
turtle.end_fill()

# 运行完还保存在原地
turtle.mainloop()

画一个彩色圆

python 复制代码
import turtle

turtle.hideturtle()
turtle.pensize(10)
turtle.color('green')
turtle.circle(100, 90)

turtle.color('yellow')
turtle.circle(100, 90)

turtle.color('red')
turtle.circle(100, 90)

turtle.color('blue')
turtle.circle(100, 90)
# 运行完还保存在原地
turtle.mainloop()

变量

变量可以由:单词字符(英文字母、中文等)、数字、下划线组成

变量的交互

python 复制代码
a = 10
b = 20
print('a =', a, 'b =', b)
a, b = b, a
print('a =', a, 'b =', b)
print('a = ' + str(a) + ' b = ' + str(b))

a = 10 b = 20
a = 20 b = 10
a = 20 b = 10

输入数字,绘制正方形

python 复制代码
import turtle

# width = int(input("请输入正方形的边长:"))

# width = int(turtle.textinput("title", "content"))

width = turtle.numinput("title", "content")

turtle.forward(width)
turtle.right(90)

turtle.forward(width)
turtle.right(90)

turtle.forward(width)
turtle.right(90)

turtle.forward(width)

turtle.mainloop()

转义字符

python 复制代码
print('123\n456')
print(r'123\n456')

123
456
123\n456

前面加r,表示raw string,原始字符串,禁止转义

使用前提:去掉r,里面的字符串也是一个正确的字符串

当print打印内容过多的时候,可以使用**\ 回车**的方式,进行换行,这样打印出来的时候,还是连续的,没有换行

打印*练习

python 复制代码
print("方法一:")
print('*')
print('**')
print('***')
print('****')
print('*****')

print('\n方法二:')
print('*\n**\n***\n****\n*****')

print('\n方法三:')
print('*')
print('*' * 2)
print('*' * 3)
print('*' * 4)
print('*' * 5)

print('\n方法四:')
print('''*
**
***
****
*****
''')

有变量的打印

python 复制代码
a = 10
b = 100
print(f"a = {10}, b = {100}")
print(F"a = {10}, b = {100}")

打印结果:
a = 10, b = 100
a = 10, b = 100

name = 'Jack'
age = 19
# %s %d/%i %f
print('My name is %s, and I am %d years old' % (name, age))

打印结果:
My name is Jack, and I am 19 years old

age = 19
print('我的名字是%d岁' % age)
print('我的名字是%4d岁' % age)
print('我的名字是%-4d岁' % age)

打印结果:
我的名字是19岁
我的名字是  19岁
我的名字是19  岁


h = 1
m = 2
s = 3
print('现在的时间是:%02d:%02d:%02d' % (h, m, s))

打印结果:
现在的时间是:01:02:03

and or

and、or的运算结果并不一定就是True、False

a or b

  • 如果a为假,就返回b,否则返回a

a or b 一真则真

a是假的,则整体a or b是真是假的,需要看b的结果

a是真的,则整体的真假,只需要看a即可

a and b

  • 如果a为假,就返回a,否则返回b

a and b 一假则假

a是假的,那么整体就是假的,不需要看b,直接返回a就行

a是真的,那么,就看b是真假

总结一下and、or的运算结果

  • 要么是a、要么是b
  • 其实是最后被执行到的那个值

while练习

python 复制代码
# 提示输入一个1~100的正整数
# 如果输入错误,要求重新输入
# 如果输入正确,请打印一下计算结果1+2+3+...+n
n = int(input("请输入[1, 100]范围的正整数:"))

while n < 1 or n > 100:
    n = int(input("请输入[1, 100]范围的正整数:"))

result = 0
while n > 0:
    result = result + n
    n = n - 1
print(f"相加结果是:{result}")

绘制一个五角星

python 复制代码
import turtle
n = int(input("输入五角星宽度:"))

turtle.color('red')
turtle.hideturtle()

turtle.begin_fill()

for _ in range(5):
    turtle.forward(n)
    turtle.left(72)
    turtle.forward(n)
    turtle.right(144)

turtle.end_fill()

turtle.mainloop()
python 复制代码
import turtle
# 修改箭头颜色
turtle.shape('turtle')
#'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.
turtle.color('green')

for i in range(50):
    turtle.forward(50 + i * 5)
    turtle.right(60)
    #盖章
    turtle.stamp()

turtle.mainloop()

List

负数索引

可以使用负数索引

比如-1表示倒数第1个

非负索引 + 负索引的绝对值 == len(列表)

假设列表的长度是n

那么索引的取值范围是[ -n, n - 1]

all() any()

  • all(x):如果可迭代对象x中的所有元素都是真,就返回True,否则返回False
  • any(x):只要可迭代对象x中的有元素为真,就返回True,否则返回False

切片(slice)

列表的切片:从列表中选取一部分元素,组成一个新的列表

s[i:j]代表:从s[i] 到s[j-1]的元素

s1 = [55, 44, 33, 22, 11]

对其切片:
s2 = s1[1:4]代表:s2 = [44, 33, 22]

对s1切片,获取s2,此时,s1不会发生任何变化

s = [i ** 2 for i in range(12, 17)]

代表的意思是:
s = [12^2, 13^2, 14^2, 15^2, 16^2]


方法(method)

方法:是一种需要通过 对象 来调用的特殊函数(function)

方法的使用格式:对象.方法名(参数)

函数

print("你好")

方法

s = [1, 2, 3]
s.index(2)

append和extend的区别


str操作

s.index(x):从左往右,查找x元素,在s中的最小索引
s.rindex(x):从右往左,查找x元素,在s中的最大索引

s.find(x):从左往右,查找x元素,在s中的最小索引
s.rfind(x):从右往左,查找x元素,在s中的最大索引

两者区别:

  • index找不到的话报错(ValueError)
  • find找不到的话,给-1

给一个字符串,统计字符串中0、1出现的次数

python 复制代码
inputStr = input("请输入数字:")
count0 = 0
count1 = 0

for i, e in enumerate(inputStr):
    if inputStr[i] == "0":
        count0 += 1
    if inputStr[i] == "1":
        count1 += 1

print(f"0出现了{count0}次")
print(f"1出现了{count1}次")

sep=修改分隔符

python 复制代码
print(11, 222, 33, sep='+', end='\n')

打印结果:
11+222+33
s.title():将字符串里面,每一个单词的首字符变大写
python 复制代码
s1 = "my name is jack"
s2 = s1.title()

print(s1, s2, sep='\n')

打印结果:
my name is jack
My Name Is Jack
s.capitalize():将字符串里面,首字符变大写,其余保持不变
python 复制代码
s1 = "my name is jack"
s2 = s1.capitalize()

print(s1, s2, sep='\n')

打印结果:
my name is jack
My name is jack
相关推荐
小王子102421 分钟前
设计模式Python版 组合模式
python·设计模式·组合模式
charlie1145141911 小时前
从0开始使用面对对象C语言搭建一个基于OLED的图形显示框架(协议层封装)
c语言·驱动开发·单片机·学习·教程·oled
Mason Lin2 小时前
2025年1月22日(网络编程 udp)
网络·python·udp
马船长2 小时前
[BSidesCF 2020]Had a bad day1
学习
清弦墨客2 小时前
【蓝桥杯】43697.机器人塔
python·蓝桥杯·程序算法
黄交大彭于晏2 小时前
三端回链增加截图功能
学习
linwq82 小时前
设计模式学习(二)
java·学习·设计模式
Fhd-学习笔记3 小时前
《大语言模型》综述学习笔记
笔记·学习·语言模型
RZer4 小时前
Hypium+python鸿蒙原生自动化安装配置
python·自动化·harmonyos
简知圈4 小时前
【04-自己画P封装,并添加已有3D封装】
笔记·stm32·单片机·学习·pcb工艺