Jump to content

For loop in Python

Yoo Song Won
Go to solution Solved by Slottr,

It means for every item in cmds it will perform that print statement 

 

So for example, heres another for loop:

x = [a,b,c,d]

for i in x:
	print(i)
    
    
#output#

a
b
c
d

 

I'm a bit confused on what the fourth line and the "i" part of the "for i in cmds:" section of this code does. 

 

Does it mean "every element in the list cmds" of which then is printed?

 

Thanks in advance.

image.png.0101be87d3e2b0e468c9842f73ae5f0c.png

Link to comment
Share on other sites

Link to post
Share on other sites

It means for every item in cmds it will perform that print statement 

 

So for example, heres another for loop:

x = [a,b,c,d]

for i in x:
	print(i)
    
    
#output#

a
b
c
d

 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Slottr said:

It means for every item in cmds it will perform that print statement 

 

So for example, heres another for loop:


x = [a,b,c,d]

for i in x:
	print(x[i])
    
    
#output#

a
b
c
d

 

Thanks a lot!! Appreciate it!

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/27/2019 at 2:41 PM, Yoo Song Won said:

Thanks a lot!! Appreciate it!

Realized I made a small goof in the print statement, should be:

print(i)

#not

print(x[i])

 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×