RUVIDEO
Поделитесь видео 🙏

For loop tutorial - Python for beginers

📁 Обучение 👁️ 16 📅 02.12.2023

Hello
This is a tutorial for beginers and I'm going to show you how for loops works in python. I will generate some errors because you have to make mistakes in order to understand, so don't be afraid to make mistakes
Let's add the following variables:
none value,
empty list,
empty "dictionary",
a non-empty list
a simple string,
a non-empty dictionary
a multidimensional list and multidimensional dictionary

Let's write a basic for loop:
first, let's add the keyword for
declare a variable name.
add the keyword in and
then put the list/dictionary or string you wanna iterate.
at the end of this line, you should always add a colon
next line you have to ident it and add code that will be part of the loop.
If you don't wanna add any code you can add the pass keyword.

The invented variable is going to be a single item from list or anything we put there.
Keep in mind that None is not a list or dictionary, but you might think that there is a list when in some situations it make become none, so make sure you have an iterable list/dictionary all the time. Let's say a function will return a list or None, so that's a case when iterating a None value will fail the loop

If you look at "simple list" for example our invented variable is going to be at iteration 1 a, at iteration 2 c, at iteration 3 b and the last one d.

So each loop will be overwritten by the current item.

You can't iterate lists that are not defined
If we try to use a variable with a None value, we will get a Type Error: "NoneType is not iterable"

Empty list and dictionary are not generating any errors, so you just have to make sure these are not "None"

In the console, there is nothing displayed because the list is empty
The same thing happens with an empty dictionary

Inside a for loop, you can use if-else and anything else you can use outside loop

If you wanna skip an item, in for loop, you can use a continue statement.

To exit a for loop you can use the break statement
You can iterate a string just like any list
When you iterate a dictionary the way you iterate a list, "item" is the key

A multidimensional list will display each item list
This is a list of dictionaries. You can easily loop this
Before we go to multidimensional lists and dictionaries, lets first see how we can mimic traditional for loops with range function

range(20) - this will iterate from 0 to 20 -1
range(20, 2) - if you wanna iterate from 20 to 2 is not going to work

range(5, 15) - if you wanna iterate from 5 to 15 you can do it by specifying the second param like this

range(5, 15, 3 ) - You can add the third argument to specify the loop step

range(11, 0, -1) - To make a reverse count you can put the step with minus like this. This will loop from 11 to 0 +1

range(10, -1, -1) -so you can take the 1 into account and start from -1 to get to nr 0 (I switched to 10 just to make it from 10 to 0 )

To iterate this as key - value we can use enumerate function
You can use this for strings, list, and dictionaries.

For dictionaries, you can use items() method to achieve this as well

If you don't understand something from this video post a comment, and I will try to help you the best I can.

Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «For loop tutorial - Python for beginers», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.

Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.

Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!

Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.