## python基础训练-for循环

news/2024/4/24 18:16:02/文章来源:https://blog.csdn.net/weixin_42595747/article/details/136519330

for 循环打印列表元素：

``````fruits = ["apple", "banana", "cherry"]
for fruit in fruits:print(fruit)
``````

for 循环打印数字范围：

``````for i in range(5):print(i)
``````

while 循环计算累加和：

``````sum = 0
i = 1
while i <= 10:sum += ii += 1
print("Sum of first 10 natural numbers:", sum)
``````

``````for i in range(1, 10):for j in range(1, 10):print(i, "*", j, "=", i*j)
``````

``````person = {"name": "Alice", "age": 30, "city": "New York"}
for key, value in person.items():print(key, ":", value)
``````

``````for i in range(1, 11):if i % 2 == 1:continueprint(i)

for i in range(1, 11):if i == 5:breakprint(i)
``````

``````for i in range(5):print(i)
else:print("Loop finished without a break")
``````

``````squared_numbers = [x**2 for x in range(1, 6)]
print(squared_numbers)
``````

``````numbers = [1, 2, 3, 4, 5]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers)
``````

``````coordinates = [(x, y) for x in [1, 2, 3] for y in [3, 5, 7]]
print(coordinates)
``````

``````names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
for name, age in zip(names, ages):print(name, "is", age, "years old")
``````

``````fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits):print("Fruit", index+1, "is", fruit)
``````

``````numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
``````

``````numbers = [1, 2, 3, 4, 5]
for num in reversed(numbers):print(num)
``````

``````num = 17
for i in range(2, num):if num % i == 0:print(num, "is not a prime number")break
else:print(num, "is a prime number")
``````

``````while True:try:number = float(input("Enter a number: "))print("You entered a number")breakexcept ValueError:print("That's not a number, try again")
``````

``````import random
number_to_guess = random.randint(1, 100)
while True:guess = int(input("Guess the number: "))if guess == number_to_guess:print("Congratulations! You guessed the number")breakelif guess < number_to_guess:print("Try higher")else:print("Try lower")
``````

``````with open("example.txt", "r") as file:for line in file:print(line.strip())
``````

``````try:a = 5 / 0
except ZeroDivisionError as e:print("Error:", e)
``````

### 数据结构：顺序表的奥秘

&#x1f389;个人名片&#xff1a; &#x1f43c;作者简介&#xff1a;一名乐于分享在学习道路上收获的大二在校生&#x1f43b;‍❄个人主页&#x1f389;&#xff1a;GOTXX &#x1f43c;个人WeChat&#xff1a;ILXOXVJE&#x1f43c;本文由GOTXX原创&#xff0c;首发CSDN&a…

### kerberos学习系列一：原理

1、简介 Kerberos 一词来源于古希腊神话中的 Cerberus —— 守护地狱之门的三头犬。 Kerberos 是一种基于加密 Ticket 的身份认证协议。Kerberos 主要由三个部分组成&#xff1a;Key Distribution Center (即KDC)、Client 和 Service。 优势&#xff1a; 密码无需进行网络传…

### java-springboot 源码 01

01.springboot 是一个启动器 先安装maven&#xff0c;按照网上的流程来。主要是安装完成后&#xff0c;要修改conf目录下的setting.xml文件。 添加&#xff1a;阿里云镜像 <mirror><id>aliyunmaven</id><mirrorOf>*</mirrorOf><name>ali…

### Unity UGUI之Scrollbar基本了解

Unity的Scrollbar组件是用于在UI中创建滚动条的组件之一。滚动条通常与其他可滚动的UI元素&#xff08;如滚动视图或列表&#xff09;一起使用&#xff0c;以便用户可以在内容超出可见区域时滚动内容。 以下是Scrollbar的基本信息和用法: 1、创建 在Unity的Hierarchy视图中右…

### Vue-04

Vue 指令 指令补充 指令修饰符&#xff1a;通过"."指明一些指令后缀&#xff0c;不同后缀封装了不同的处理操作 → 简化代码 按键修饰符 keyup.enter → 键盘回车监听 在input中使用keyup.enter&#xff0c;这个时候按enter键也能实现添加&#xff0c;和点击按钮实…

### gpt批量工具,gpt批量生成文章工具

GPT批量工具在今天的数字化时代扮演着越来越重要的角色&#xff0c;它们通过人工智能技术&#xff0c;可以自动批量生成各种类型的文章&#xff0c;为用户提供了便利和效率。本文将介绍5款不同的GPT批量工具&#xff0c;并介绍一款知名的147GPT生成工具&#xff0c;以及另外一款…