#21 Python Tutorial for Beginners | For Loop in Python
Summary
TLDRThe video explains the difference between while and for loops in Python. While loops iterate based on a condition, for loops iterate through sequences like lists and strings. The script shows how to print list values with a for loop and use the range() function to print sequences of numbers, including in reverse order. It demonstrates how to use an if statement inside a for loop to selectively print values, like printing numbers not divisible by 5. The next video will cover printing patterns using nested for loops.
Takeaways
- 😀 While loops are used for uncertain iterations, for loops work with sequences like lists, tuples, and strings
- 👉🏻 For loops automatically iterate over the elements of a sequence, no need to manage iteration variable and conditions
- 📝 Use 'for i in sequence:' syntax and indent the code block to use for loop
- 🔢 Can iterate over numeric ranges using the range() function - specify start, stop and step size
- ❗ Range excludes the stop value, so make the stop value 1 more if you want to include it
- 🔄 Set step size as -1 in range() to iterate in reverse order
- 🤝 Use if condition inside for loop to filter values while iterating
- 🎯 For loops can be nested inside other for loops similar to while loops
- 📜 String, lists, tuples can directly be used in place of a variable in for loop syntax
- 👍 For loops clean way to iterate as alliteration managed automatically
Q & A
What are the two types of loops discussed in the video?
-The two types of loops discussed are the while loop and the for loop.
How does a for loop differ from a while loop?
-A for loop works with sequences like lists, tuples, and strings and automatically iterates through the values, while a while loop depends on a condition and we have to manually increment/decrement the iterating variable.
How can we print values from a list using a for loop?
-We can print values from a list using a for loop by iterating through the list element by element using the iterating variable of the loop.
What is the syntax for using a for loop?
-The syntax is: for var in sequence: #loop body
How can we print numbers from 1 to 10 using a for loop?
-We can print numbers from 1 to 10 using the range() function along with a for loop. For example: for i in range(1, 11): print(i)
Can we use if statements inside a for loop?
-Yes, we can use if statements inside a for loop to add conditional logic and filtering.
How can we print numbers in reverse order using for loop?
-We can print numbers in reverse order by using range() with negative step size. For example: for i in range(20, 10, -1)
What collection types can be used with a for loop?
-For loops can be used with lists, tuples, strings, and any other iterable objects in Python.
Can we nest for loops in Python?
-Yes, we can nest one for loop inside another for loop in Python to create nested iterations.
What patterns will be discussed related to loops in the next video?
-In the next video, some patterns related to printing shapes and structures using loops will be discussed.
Outlines
📝 For Loops in Python
This paragraph explains how for loops work in Python. It iterates over sequences like lists, tuples, and strings to print values one by one. It is easier than using while loops and index values. The syntax includes the for keyword, a variable to represent each element, the in keyword, and the sequence to iterate over. The range() function can also be used with for loops to print a specific range of numbers, including options to set start, end, and step size values.
🔁 Using Range and Conditions in For Loops
This paragraph continues discussing how to use for loops in Python. It shows how to print numbers in reverse order using range() through setting negative step values. It also covers using an if condition inside the for loop to selectively print values, like only printing numbers not divisible by 5 in a given range. This demonstrates the flexibility of using logical conditions within for loop iterations.
Mindmap
Keywords
💡for loop
💡range
💡iteration
💡sequence
💡loop variable
💡loop body
💡modulo
💡nested loops
💡loop control
💡indentation
Highlights
For loops work with sequences like lists, strings, tuples
The variable 'i' refers to each element of the sequence in the for loop
For loops automatically iterate through the sequence, unlike while loops
Can print sequence elements one by one using a for loop
For loops can directly iterate over the sequence without needing a separate iteration variable
The range() function generates a sequence of numbers that can be iterated with a for loop
Range allows controlling start, end, and step size of numeric sequence
For loops can go in reverse order using range() with negative step value
If statements can be included inside for loops for conditional iteration
For loops can be nested inside other for loops
Will cover some pattern printing examples with for loops in next video
For loops are useful for iterating over sequences and programmatic control
For loops avoid needing manual increment logic like while loops
The iteration variable moves automatically through the sequence
Can filter iteration with conditions inside the for loop
Transcripts
[Music]
welcome back aliens my name is Ivan
vetti and let's continue this series on
Python so in the last video we have
talked about loops right and
specifically it was a while loop in this
video we'll talk about for loop
so while loop and for loop they both are
loops right but then the working is
different in fact normally when we talk
about while loop it is for certain 2
iterations and it is positon conditions
so if ever the while loop we specify a
condition but that's not the case with
father because in for loop it normally
works with sequence whatever sequence
I'm talking about think about least
think about tuple think about string so
what I'm basically talking about is
let's say if you have a laced here and
in this list AHA certain values it can
be any random value let's say my name
Naveen 65 and if I say they'd say 2.5 so
these are I have this values one is
string one is integer and when his float
doesn't matter what type of value you
have there I want to print this values
it's really easy actually you can simply
say print and you can print the value of
x this will work okay let me just run
this code and you can see record the
output but what if you want to get the
output individually I want to print the
values one by one so in this case you
will be using a for loop now for loop
works so we have to say for then we have
to use a variable with this we will say
this time I now I represents one element
of this list at a time so example
initially I will refer to Naveen then I
will refer to 65 then I will refer to
2.5 one by one so you have to say for I
in X so one by one it will fetch the
value and again this is a suit so you
have to use a code in there and you can
write lines of statements again the
point is you need to make sure that you
use a proper indentation so I will be
printing the value that's it
I will printing the value of I that's it
said those there's no iteration unlike a
while here so we don't the enjoys the
variable we don't take the condition and
we don't increment or decrement by
ourselves which will be done by for loop
itself this which is smart right so now
if I run this code you can see record
the values one by one so yes if you want
to print all the values from at least
use for loop so for loop normally can be
used with
sequence example list which you have
worked with now all a string let's say
you have a string here let's say the X
is not at least now it is a simple
string which is Naveen and then I want
to print the value of this one by one so
one way is you can use an array right so
we can use a while loop here as well and
then you can use that square back and
you can specify index index values
otherwise if you run this code you can
see this is how you're for the works so
what it will do the value of I first
will have N and that will have a let me
let me just do that again the thing
which we are doing from a long time
which is debug so I'd like and say debug
let's trace it so if i trace you can see
the value of x is naveen and the value
of i is and now i'll give you fire
repeat if I said next iteration you can
see the value of I it will become a and
the next iteration value of I becomes V
the value of I becomes I and then value
of I becomes n so that's moving for you
and you're printing all the values right
so if I see the console you got all the
run is one by one right of course I
forgot to one do one more so you got
live in that so that's the advantage of
using for loop oh can I use for loop
with something else yes we can example
we can also use this with tuple or we
can use with say but then we have one
more example in you can do you can
specify the list itself okay instead of
having a different variable you can have
your list here even that works here so
you have two six Paul and if I done this
code you can see we got one by one so
you can have your list yet itself so
syntax is you mention for then you say I
in and then you mention the collection
or the sequence it can be a list it can
be a string everything works in fact
what if I want to print the values from
1 to 10 because of course why we can do
that
we can do that in while loop can we do
it here and this is yes for that we have
to use range if you remember in sequence
we have talked about range in range you
can specify 10 what it means it means
start from 0 and end at 9 so in total we
have 10 values and if I have this code
you can see record and 0 to 9 so we got
0 and then it ends at 9 so in total we
have 10 values so we can use range as
well and using this range you can
perform certain operations in fact in
rain
you can actually start from different
values it's I don't want to print from 0
to 10 I want to print from let's say 11
to 20 in that case you would say hey I
want to start from 11 I want to end at
2070 so we want 20 included right so
we'll say 21 and every iteration will
have 1 so it will be 1 by 1 iteration so
if I say run so you can see we got
values from 11 to 20 so you have to
mention three things starting point
ending point and the iteration it will
be difference would be 1 or 2 example if
I say this is 2 in this case so look at
the values now it will be different
values 11 13 15 17 and 19 so if there's
a difference of two if I say 5 you can
see we got 11 and 16 that's it so this
is how you specify the gap there what if
you want to print in Reverse let's try
can I start with 20 and if I end at 11
if I run this code it will give you it
is giving you nothing because it always
goes in ascending order we have to
mention go in reverse order so you have
to say minus 1 so it will start with 20
and then 1918 and it wasn't that way so
you can see we got from 20 to 12 but
there's a problem you have to make sure
that it goes till 11 right so you have
to because the value 11 is excluded so
we have to use 10 so that it will
include 11 that you want this again and
it works so this is how you can reuse
range and this how you can use for loop
now in fact we can do one more thing so
you can see if we have a range of 20 and
then I'm printing the value of I in fact
I will just go I will start with 1 so
much from 1 to 20 so I will inclusive I
will make sure it is 21 so that it will
include 20 and the iteration would be 1
or even if you don't mention that that's
fine if I say print value we can see it
just 1 to 20
what I want is so every time the value
which is a divisible by 5 it should not
get printed okay so I want that so I
want to print only when the value is not
user by 5 so we can use a inside phone
right so we can do that so we can have
if condition inside for if you want to
if you have if you want to have that
condition so you can say if I mod 5 is
equal to equal to 0 don't print
otherwise you can print okay but how can
I do that so what we can do is we can
say not equal to so print only when it's
not equal right so we can do that so
print only when it is not equal to say
fine on this code if I say
oh we got an error because I missed
something and I'm also using brackets
because I have this habit of working on
Java so it's the anyway it's optional in
Python so let's run this and you can see
we got we got one and regard for you can
see we don't have five there because we
are taking about the condition so yes we
can write it inside of four so since we
had if inside fault we can also have
fall inside of four which is a
straightforward loop but then we will
not be doing that here in the next video
we'll play with some patterns you know
we'll try the dorsum patterns and we'll
try to understand how do we do that
so in the next video it will be fun so I
hope you are enjoying the series it
means the comment section and dukey like
button if you're enjoying it I'm
watching everyone bye
you
Ver más vídeos relacionados
![](https://i.ytimg.com/vi/GS-lAZBNm6k/hq720.jpg)
For Loops | Godot GDScript Tutorial | Ep 08
![](https://i.ytimg.com/vi/EBHOo-6xn4A/hq720.jpg)
#18 While vs For Loop | Which to use and When?
![](https://i.ytimg.com/vi/8nrBG6kJoDs/hq720.jpg)
While Loops | Godot GDScript Tutorial | Ep 07
![](https://i.ytimg.com/vi/eLVbNXI5HgI/hq720.jpg)
Looping Arrays, Breaking and Continuing | JavaScript 🔥 | Lecture 044
![](https://i.ytimg.com/vi/nW9M0MQinfg/hq720.jpg)
a BASH script PUSH-UP counter (for #gains )
![](https://i.ytimg.com/vi/WZizrQiXhYo/hq720.jpg)
Pattern Printing | Problems on Loops - Part 1 | Lecture 7 | C++ and DSA Foundation Course
5.0 / 5 (0 votes)