PYTHON_REAL Telegram 965
Цикл for в Python: разбор возможностей

Цикл for в Python — это мощный инструмент для итерации по последовательностям, таким как списки, кортежи, строки и даже словари. В отличие от традиционного for в других языках, здесь он работает как итератор, проходя по элементам коллекции без явного указания индексов.

🔹 Основные возможности:
Простая итерация

for num in [1, 2, 3, 4, 5]:
print(num)

Выведет числа от 1 до 5.

Использование range()

for i in range(5): # Эквивалентно range(0, 5)
print(i)

Выведет 0, 1, 2, 3, 4.

Перебор строки

for char in "Python":
print(char)

Выведет символы P, y, t, h, o, n.

Перебор словаря

data = {"name": "Alice", "age": 25}
for key, value in data.items():
print(f"{key}: {value}")

Выведет:

name: Alice
age: 25


Использование enumerate() для индексов

words = ["apple", "banana", "cherry"]
for index, word in enumerate(words):
print(index, word)

Выведет:

0 apple
1 banana
2 cherry


Цикл for с else

for i in range(3):
print(i)
else:
print("Цикл завершен!")

Если break не был вызван, else выполнится после окончания цикла.

https://realpython.com/python-for-loop/

#python

👉 @python_real
👍4



tgoop.com/python_real/965
Create:
Last Update:

Цикл for в Python: разбор возможностей

Цикл for в Python — это мощный инструмент для итерации по последовательностям, таким как списки, кортежи, строки и даже словари. В отличие от традиционного for в других языках, здесь он работает как итератор, проходя по элементам коллекции без явного указания индексов.

🔹 Основные возможности:
Простая итерация


for num in [1, 2, 3, 4, 5]:
print(num)

Выведет числа от 1 до 5.

Использование range()

for i in range(5): # Эквивалентно range(0, 5)
print(i)

Выведет 0, 1, 2, 3, 4.

Перебор строки

for char in "Python":
print(char)

Выведет символы P, y, t, h, o, n.

Перебор словаря

data = {"name": "Alice", "age": 25}
for key, value in data.items():
print(f"{key}: {value}")

Выведет:

name: Alice
age: 25


Использование enumerate() для индексов

words = ["apple", "banana", "cherry"]
for index, word in enumerate(words):
print(index, word)

Выведет:

0 apple
1 banana
2 cherry


Цикл for с else

for i in range(3):
print(i)
else:
print("Цикл завершен!")

Если break не был вызван, else выполнится после окончания цикла.

https://realpython.com/python-for-loop/

#python

👉 @python_real

BY Реальный Python




Share with your friend now:
tgoop.com/python_real/965

View MORE
Open in Telegram


Telegram News

Date: |

3How to create a Telegram channel? Telegram message that reads: "Bear Market Screaming Therapy Group. You are only allowed to send screaming voice notes. Everything else = BAN. Text pics, videos, stickers, gif = BAN. Anything other than screaming = BAN. You think you are smart = BAN. While some crypto traders move toward screaming as a coping mechanism, many mental health experts have argued that “scream therapy” is pseudoscience. Scientific research or no, it obviously feels good. Judge Hui described Ng as inciting others to “commit a massacre” with three posts teaching people to make “toxic chlorine gas bombs,” target police stations, police quarters and the city’s metro stations. This offence was “rather serious,” the court said. How to Create a Private or Public Channel on Telegram?
from us


Telegram Реальный Python
FROM American