Peng~666

今天接触了Mermaid和PlantUML,这俩看起来都是用来写流程图的。有时间学学语法看看。。。

评论
Peng~666

DAY-3(二补):

在列表基础上开始学习for循环:

  1. for循环基本用法

# 命名约定举例:
# for cat in cats:
# for dog in dogs:
# for item in list_of_items:

magicians = ['alice','david','carolina']
for magician in magicians:
    print (magician)
# 将输出:
# alice
# david
# carolina
  1. for循环中执行更多操作

magicians = ['alice','david','carolina']
for magician in magicians:
    print (f"{magician.title()},that was a great tricks!")
    print (f"I can't wait to see your next trick, {magician.title()}.\n")
print ("Thank you, everyone. That was a great magic show!")
# 将输出:
# Alice,that was a great tricks!
# I can't wait to see your next trick, Alice.

# David,that was a great tricks!
# I can't wait to see your next trick, David.

# Carolina,that was a great tricks!
# I can't wait to see your next trick, Carolina.

# Thank you, everyone. That was a great magic show!

# 使用 for 循环处理数据,是一种对数据集执行整体操作的不错方式。例如,你可能使用 for 循环来初始化游戏——遍历角色列表,将每个角色都显示到屏幕上;然后在循环后面添加一些代码,在屏幕上绘制所有角色后显示一个 Play Now 按钮。
  1. 小练习:

# 练习:想出至少三种有共同特征的动物,将其名称存储在一个列表中,再使用 for 循环将每种动物的名称打印出来
pets = ['cat','dog','chick','sheep','cow']
for pet in pets:
    print (f"A {pet.title()} would make a great pet.")
print ("Any of these animals would make a great pet!")
# 将输出:A Cat would make a great pet.
# A Dog would make a great pet.
# A Chick would make a great pet.
# A Sheep would make a great pet.
# A Cow would make a great pet.
# Any of these animals would make a great pet!

评论
Peng~666

DAY-3(补充):

主要学习了列表排序操作~

  1. sort()方法对列表进行永久排序,传入reverse=True可以将排好的序倒过来。

cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)
# 将输出['audi', 'bmw', 'subaru', 'toyota']
# 现在,汽车是按字母顺序排列的,再也无法恢复到原来的排列顺序。
# sort()方法中传入reverse=True可以反方向排列
cars.sort(reverse=True)
print(cars)
# 将输出['toyota', 'subaru', 'bmw', 'audi']
# 现在,汽车是按字母顺序倒序排列的,再也无法恢复到原来的排列顺序。
  1. sorted()函数对列表进行临时排序,同上可以传入reverse=True进行倒序。

cars = ['bmw', 'audi', 'toyota', 'subaru']
print ('Here is the original list:')
print (cars)

print ('\nHere is the sorted list:')
print (sorted(cars))

print ('\nHere is the original list again:')
print (cars)
'''将输出:
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']

Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']

Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']
'''
# 在调用 sorted() 函数后,列表元素的排列顺序并没有变。
# 同上,向sorted()函数传入reverse=True可倒序排序
# 注意:当所有值不全是小写时,字母排序更复杂。在确定排列顺序时,有多种解读大写字母的方式,此时要指定准确的排列顺序,可能会比这里所做的更加复杂。然而,大多数排序方式是以本节介绍的知识为基础的。
  1. reverse()方法可以反转列表元素的排列顺序(不是按字母顺序倒序排列,而是单纯将原列表元素进行倒序操作)

cars = ['bmw', 'audi', 'toyota', 'subaru']
print (cars)
cars.reverse()
print (cars)
# 将输出:
# ['bmw', 'audi', 'toyota', 'subaru']
# ['subaru', 'toyota', 'audi', 'bmw']
  1. len()函数获得列表长度

cars = ['bmw', 'audi', 'toyota', 'subaru']
print (len(cars))
# 将输出4
# 在需要完成如下任务时,len() 很有用:明确还有多少个外星人未被消灭,确定需要管理多少项可视化数据,计算网站有多少注册用户,等等。

评论
Peng~666

昨天的忘记总结了,今天补上


/

DAY-2:

1、+ - * / **加减乘除、乘方运算

a = 3/3

输出a为3.0,将任意两个数相除,结果总是浮点数

a = 1+2.0

输出3.0,一个操作数是浮点数,结果总是浮点数

a = 114_000_000_000

下划线给数分组,打印不会显示下划线

a,b,c=11,45,14

多变量赋值

MAX = 114514

保持不变的常亮,通常全大写字母来命名


DAY-3

今天开始学列表

列表及其打印

bicycle = ['trek','cannondale','redline','specialized']
print(bicycle) 
# 输出['trek', 'cannondale', 'redline', 'specialized']

访问列表元素

# 访问列表元素
print(bicycle[0]) 
# 输出trek,结果干净、整洁(无方括号),注意索引从0开始(索引为0则访问第一个元素,索引为1则访问第二个元素...)

# 结合title()方法
print(bicycle[0].title())
# 输出trek,title()方法使首字母大写

# 索引
print(bicycle[1],bicycle[3],bicycle[-1])
# 输出cannondale specialized specialized。
# 索引为-1,返回倒数第一个元素
# 索引为-2,返回倒数第二个元素...

# 用f字符串创建消息
message = f"My first bicycle was a {bicycle[2].title()}."
print(message)
# 将输出"My first bicycle was a Redline."

练习

# 练习
game = ['PVZ','minecraft','genshin']
message = f"{game[0].title()} and {game[1].title()} are my favourite games, {game[2].title()} is my favourite game too!"
print(message)
# 输出Pvz and Minecraft are my favourite games, Genshin is my favourite game too!

修改列表元素

# 修改列表元素
motorcycles = ['honda','yamaha','suzuki']
print (motorcycles)
motorcycles[0] = 'ducati' # 通过索引修改列表元素。
print (motorcycles)

append()方法追加元素

motorcycles = ['honda','yamaha','suzuki']
motorcycles.append('ducati') 
print (motorcycles) # 将输出['honda', 'yamaha', 'suzuki', 'ducati']

insert()方法插入元素

motorcycles = ['honda','yamaha','suzuki']
motorcycles.insert(0,'ducati') # 将新元素插入到索引为0的位置,后边元素均后移一个位置
print (motorcycles) # 将输出['ducati', 'honda', 'yamaha', 'suzuki']

del语句删除元素

motorcycles = ['ducati', 'honda', 'yamaha', 'suzuki']
del motorcycles[0] # 删除第一个元素
print (motorcycles) # 将输出['honda', 'yamaha', 'suzuki']

pop()方法删除元素——弹出

motorcycles = ['honda','yamaha','suzuki']
pop_motor = motorcycles.pop() # 弹出最后的元素suzuki并赋给变量pop_motor
print (pop_motor) # 将输出suzuki
motorcycles = ['honda','yamaha','suzuki']
pop_motor = motorcycles.pop(1) # 弹出第二个元素并赋给变量pop_motor
print (pop_motor) # 将输出yamaha

如果要从列表中删除一个元素,且不再以任何方式使用它,就使用 del 语句;
如果要在删除元素后继续使用它,就使用 pop() 方法。

remove()方法:根据值删除元素

motorcycles = ['honda','yamaha','suzuki']
too_expensive = 'yamaha'
motorcycles.remove(too_expensive)
print (motorcycles)
print (f"\nA {too_expensive.title()} is too expensive for me.")
# 将输出['honda', 'suzuki']
# 将输出A Yamaha is too expensive for me.

评论
Peng~666

DAY-1:

1、打印Hello World!

2、方法:name.title()表示对name变量执行title()函数的操作

title():将字符串各单词首字母变成大写

upper()、lower()将字符串全部字母变成大/小写

f字符串:通过把花括号内的变量替换为其值来设置字符串的格式

例:

a=1

b=2

c=f"{a}{b}" # c='12'

\t:添加制表符

\n:添加换行符

strip()方法:删去字符串两端的空白,lstrip()和rstrip()分别删去左、右的空白

removeprefix()方法:删去前缀,括号里放要删的前缀

评论