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
相关推荐
databook13 分钟前
Manim实现闪光轨迹特效
后端·python·动效
Juchecar1 小时前
解惑:NumPy 中 ndarray.ndim 到底是什么?
python
用户8356290780512 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_2 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
数据智能老司机8 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机9 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机9 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机9 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i10 小时前
drf初步梳理
python·django
每日AI新事件10 小时前
python的异步函数
python