#21 Python Tutorial for Beginners | For Loop in Python

Telusko
12 Jul 201807:27

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

00:00

📝 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.

05:01

🔁 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

A for loop is a programming construct that repeats a block of code a specified number of times. In Python, it iterates over a sequence like a list or string, assigning the values to a loop variable one by one. The video shows how to use a for loop to print the elements of a list individually, instead of printing the whole list at once.

💡range

The range() function in Python generates a sequence of numbers that can be used to perform repeated actions a set number of times in a for loop. As shown in the video, range() lets you specify the start, stop and step size for the sequence of numbers, including counting backwards.

💡iteration

Iteration means repeating a process over and over. In a Python for loop, iteration happens automatically based on the sequence it's looping over. We don't have to manually control the iteration like in a while loop.

💡sequence

A sequence in Python refers to an ordered collection of elements that can be looped over, like a list, tuple, string, etc. For loops work by iterating over the elements of a sequence in order.

💡loop variable

The loop variable in a Python for loop is a temporary variable that gets assigned the value of each item from the sequence in turn. It gives access to the individual elements inside the loop body. Here, the loop variable 'i' holds values from the list 'x'.

💡loop body

The loop body contains the code that needs to be executed repeatedly in the loop. It must be indented properly to associate it with the for loop. Here, the print statement printing the loop variable is inside the loop body.

💡modulo

The modulo or mod operator (%) gives the remainder when one number is divided by another. It's used here inside the loop to check if 'i' is divisible by 5, to print only those numbers not divisible by 5.

💡nested loops

We can have loops inside other loops in Python, which is called nested loops. The video mentions how a for loop can be nested inside another for loop, though it does not demonstrate it.

💡loop control

Loop control refers to controlling how a loop executes by skipping iterations or stopping the loop altogether. Here, the if condition inside the loop serves as loop control by skipping numbers divisible by 5.

💡indentation

Python uses indentation instead of braces `{}` to denote blocks of code like loops. The lines inside the loop body have to be indented consistently for the loop to work correctly. Improper indentation causes errors.

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

play00:00

[Music]

play00:03

welcome back aliens my name is Ivan

play00:05

vetti and let's continue this series on

play00:07

Python so in the last video we have

play00:09

talked about loops right and

play00:10

specifically it was a while loop in this

play00:13

video we'll talk about for loop

play00:14

so while loop and for loop they both are

play00:17

loops right but then the working is

play00:19

different in fact normally when we talk

play00:20

about while loop it is for certain 2

play00:23

iterations and it is positon conditions

play00:25

so if ever the while loop we specify a

play00:27

condition but that's not the case with

play00:29

father because in for loop it normally

play00:31

works with sequence whatever sequence

play00:33

I'm talking about think about least

play00:35

think about tuple think about string so

play00:38

what I'm basically talking about is

play00:39

let's say if you have a laced here and

play00:41

in this list AHA certain values it can

play00:44

be any random value let's say my name

play00:46

Naveen 65 and if I say they'd say 2.5 so

play00:50

these are I have this values one is

play00:51

string one is integer and when his float

play00:53

doesn't matter what type of value you

play00:55

have there I want to print this values

play00:57

it's really easy actually you can simply

play00:58

say print and you can print the value of

play01:00

x this will work okay let me just run

play01:03

this code and you can see record the

play01:04

output but what if you want to get the

play01:07

output individually I want to print the

play01:08

values one by one so in this case you

play01:11

will be using a for loop now for loop

play01:14

works so we have to say for then we have

play01:17

to use a variable with this we will say

play01:19

this time I now I represents one element

play01:23

of this list at a time so example

play01:25

initially I will refer to Naveen then I

play01:27

will refer to 65 then I will refer to

play01:29

2.5 one by one so you have to say for I

play01:32

in X so one by one it will fetch the

play01:36

value and again this is a suit so you

play01:38

have to use a code in there and you can

play01:40

write lines of statements again the

play01:42

point is you need to make sure that you

play01:44

use a proper indentation so I will be

play01:46

printing the value that's it

play01:47

I will printing the value of I that's it

play01:49

said those there's no iteration unlike a

play01:51

while here so we don't the enjoys the

play01:53

variable we don't take the condition and

play01:55

we don't increment or decrement by

play01:57

ourselves which will be done by for loop

play01:59

itself this which is smart right so now

play02:01

if I run this code you can see record

play02:04

the values one by one so yes if you want

play02:07

to print all the values from at least

play02:08

use for loop so for loop normally can be

play02:11

used with

play02:12

sequence example list which you have

play02:14

worked with now all a string let's say

play02:16

you have a string here let's say the X

play02:18

is not at least now it is a simple

play02:21

string which is Naveen and then I want

play02:23

to print the value of this one by one so

play02:26

one way is you can use an array right so

play02:28

we can use a while loop here as well and

play02:30

then you can use that square back and

play02:31

you can specify index index values

play02:33

otherwise if you run this code you can

play02:35

see this is how you're for the works so

play02:37

what it will do the value of I first

play02:39

will have N and that will have a let me

play02:41

let me just do that again the thing

play02:43

which we are doing from a long time

play02:44

which is debug so I'd like and say debug

play02:48

let's trace it so if i trace you can see

play02:50

the value of x is naveen and the value

play02:52

of i is and now i'll give you fire

play02:55

repeat if I said next iteration you can

play02:57

see the value of I it will become a and

play03:00

the next iteration value of I becomes V

play03:01

the value of I becomes I and then value

play03:04

of I becomes n so that's moving for you

play03:06

and you're printing all the values right

play03:07

so if I see the console you got all the

play03:10

run is one by one right of course I

play03:11

forgot to one do one more so you got

play03:13

live in that so that's the advantage of

play03:15

using for loop oh can I use for loop

play03:18

with something else yes we can example

play03:20

we can also use this with tuple or we

play03:21

can use with say but then we have one

play03:23

more example in you can do you can

play03:26

specify the list itself okay instead of

play03:28

having a different variable you can have

play03:30

your list here even that works here so

play03:33

you have two six Paul and if I done this

play03:35

code you can see we got one by one so

play03:37

you can have your list yet itself so

play03:39

syntax is you mention for then you say I

play03:42

in and then you mention the collection

play03:45

or the sequence it can be a list it can

play03:46

be a string everything works in fact

play03:49

what if I want to print the values from

play03:51

1 to 10 because of course why we can do

play03:53

that

play03:53

we can do that in while loop can we do

play03:55

it here and this is yes for that we have

play03:58

to use range if you remember in sequence

play04:00

we have talked about range in range you

play04:02

can specify 10 what it means it means

play04:04

start from 0 and end at 9 so in total we

play04:08

have 10 values and if I have this code

play04:09

you can see record and 0 to 9 so we got

play04:11

0 and then it ends at 9 so in total we

play04:14

have 10 values so we can use range as

play04:17

well and using this range you can

play04:18

perform certain operations in fact in

play04:20

rain

play04:21

you can actually start from different

play04:22

values it's I don't want to print from 0

play04:25

to 10 I want to print from let's say 11

play04:27

to 20 in that case you would say hey I

play04:29

want to start from 11 I want to end at

play04:31

2070 so we want 20 included right so

play04:34

we'll say 21 and every iteration will

play04:36

have 1 so it will be 1 by 1 iteration so

play04:39

if I say run so you can see we got

play04:41

values from 11 to 20 so you have to

play04:44

mention three things starting point

play04:45

ending point and the iteration it will

play04:48

be difference would be 1 or 2 example if

play04:50

I say this is 2 in this case so look at

play04:53

the values now it will be different

play04:55

values 11 13 15 17 and 19 so if there's

play04:58

a difference of two if I say 5 you can

play05:00

see we got 11 and 16 that's it so this

play05:03

is how you specify the gap there what if

play05:05

you want to print in Reverse let's try

play05:07

can I start with 20 and if I end at 11

play05:11

if I run this code it will give you it

play05:13

is giving you nothing because it always

play05:14

goes in ascending order we have to

play05:16

mention go in reverse order so you have

play05:18

to say minus 1 so it will start with 20

play05:20

and then 1918 and it wasn't that way so

play05:23

you can see we got from 20 to 12 but

play05:26

there's a problem you have to make sure

play05:27

that it goes till 11 right so you have

play05:28

to because the value 11 is excluded so

play05:31

we have to use 10 so that it will

play05:32

include 11 that you want this again and

play05:35

it works so this is how you can reuse

play05:37

range and this how you can use for loop

play05:39

now in fact we can do one more thing so

play05:40

you can see if we have a range of 20 and

play05:42

then I'm printing the value of I in fact

play05:44

I will just go I will start with 1 so

play05:47

much from 1 to 20 so I will inclusive I

play05:49

will make sure it is 21 so that it will

play05:51

include 20 and the iteration would be 1

play05:53

or even if you don't mention that that's

play05:54

fine if I say print value we can see it

play05:56

just 1 to 20

play05:56

what I want is so every time the value

play05:59

which is a divisible by 5 it should not

play06:00

get printed okay so I want that so I

play06:03

want to print only when the value is not

play06:05

user by 5 so we can use a inside phone

play06:09

right so we can do that so we can have

play06:10

if condition inside for if you want to

play06:12

if you have if you want to have that

play06:14

condition so you can say if I mod 5 is

play06:18

equal to equal to 0 don't print

play06:20

otherwise you can print okay but how can

play06:23

I do that so what we can do is we can

play06:25

say not equal to so print only when it's

play06:28

not equal right so we can do that so

play06:29

print only when it is not equal to say

play06:31

fine on this code if I say

play06:33

oh we got an error because I missed

play06:35

something and I'm also using brackets

play06:37

because I have this habit of working on

play06:38

Java so it's the anyway it's optional in

play06:40

Python so let's run this and you can see

play06:43

we got we got one and regard for you can

play06:46

see we don't have five there because we

play06:48

are taking about the condition so yes we

play06:50

can write it inside of four so since we

play06:52

had if inside fault we can also have

play06:55

fall inside of four which is a

play06:56

straightforward loop but then we will

play06:58

not be doing that here in the next video

play07:00

we'll play with some patterns you know

play07:01

we'll try the dorsum patterns and we'll

play07:03

try to understand how do we do that

play07:05

so in the next video it will be fun so I

play07:07

hope you are enjoying the series it

play07:08

means the comment section and dukey like

play07:10

button if you're enjoying it I'm

play07:11

watching everyone bye

play07:21

you