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: |

Select: Settings – Manage Channel – Administrators – Add administrator. From your list of subscribers, select the correct user. A new window will appear on the screen. Check the rights you’re willing to give to your administrator. End-to-end encryption is an important feature in messaging, as it's the first step in protecting users from surveillance. In handing down the sentence yesterday, deputy judge Peter Hui Shiu-keung of the district court said that even if Ng did not post the messages, he cannot shirk responsibility as the owner and administrator of such a big group for allowing these messages that incite illegal behaviors to exist. According to media reports, the privacy watchdog was considering “blacklisting” some online platforms that have repeatedly posted doxxing information, with sources saying most messages were shared on Telegram. Hashtags are a fast way to find the correct information on social media. To put your content out there, be sure to add hashtags to each post. We have two intelligent tips to give you:
from us


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