Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Two variables in for loop
#8
(Aug-25-2019, 08:56 AM)ThomasL Wrote:
(Aug-22-2019, 05:29 AM)vipinv23 Wrote: I want to iterate two variables independently and simultaneously in a single for loop.

All writen above can be put into 2 lines of code:
for angle1, angle2 in zip(range(0, 181, 5), range(-180, 181, 10)):
    print(angle1, angle2)

Wow! Thank you, I'm a newbie and I didn't know about this zip() function. I guess you could use it in many ways.

I just tried your code:
for angle1, angle2 in zip(range(0, 181, 5), range(-180, 181, 10)):
    print(angle1, angle2)
that produces this output:
Output:
0 -180 5 -170 10 -160 15 -150 20 -140 25 -130 30 -120 35 -110 40 -100 45 -90 50 -80 55 -70 60 -60 65 -50 70 -40 75 -30 80 -20 85 -10 90 0 95 10 100 20 105 30 110 40 115 50 120 60 125 70 130 80 135 90 140 100 145 110 150 120 155 130 160 140 165 150 170 160 175 170 180 180
As I am very visual, and I like self-explanatory answers, I just made a few tweakings to your code (I hope you don't mind):
for angle1, angle2 in zip(range(0, 181, 5), range(-180, 181, 10)):
    angle1 = str(angle1)
    angle2 = str(angle2)
    print("This is angle1:", angle1, "and this is angle2:",angle2)
and now it produces the following output:
Output:
This is angle1: 0 and this is angle2: -180 This is angle1: 5 and this is angle2: -170 This is angle1: 10 and this is angle2: -160 This is angle1: 15 and this is angle2: -150 This is angle1: 20 and this is angle2: -140 This is angle1: 25 and this is angle2: -130 This is angle1: 30 and this is angle2: -120 This is angle1: 35 and this is angle2: -110 This is angle1: 40 and this is angle2: -100 This is angle1: 45 and this is angle2: -90 This is angle1: 50 and this is angle2: -80 This is angle1: 55 and this is angle2: -70 This is angle1: 60 and this is angle2: -60 This is angle1: 65 and this is angle2: -50 This is angle1: 70 and this is angle2: -40 This is angle1: 75 and this is angle2: -30 This is angle1: 80 and this is angle2: -20 This is angle1: 85 and this is angle2: -10 This is angle1: 90 and this is angle2: 0 This is angle1: 95 and this is angle2: 10 This is angle1: 100 and this is angle2: 20 This is angle1: 105 and this is angle2: 30 This is angle1: 110 and this is angle2: 40 This is angle1: 115 and this is angle2: 50 This is angle1: 120 and this is angle2: 60 This is angle1: 125 and this is angle2: 70 This is angle1: 130 and this is angle2: 80 This is angle1: 135 and this is angle2: 90 This is angle1: 140 and this is angle2: 100 This is angle1: 145 and this is angle2: 110 This is angle1: 150 and this is angle2: 120 This is angle1: 155 and this is angle2: 130 This is angle1: 160 and this is angle2: 140 This is angle1: 165 and this is angle2: 150 This is angle1: 170 and this is angle2: 160 This is angle1: 175 and this is angle2: 170 This is angle1: 180 and this is angle2: 180
Thanks again to make me aware of this zip() function!

I just noticed too the change from uppercase to lowercase. I guess I need to check and use more frequently the PEP8 docs... Rolleyes
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Messages In This Thread
Two variables in for loop - by vipinv23 - Aug-22-2019, 05:29 AM
RE: Two variables in for loop - by ndc85430 - Aug-22-2019, 05:37 AM
RE: Two variables in for loop - by vipinv23 - Aug-22-2019, 07:16 AM
RE: Two variables in for loop - by ndc85430 - Aug-24-2019, 04:43 AM
RE: Two variables in for loop - by newbieAuggie2019 - Aug-25-2019, 02:34 AM
RE: Two variables in for loop - by DeaD_EyE - Aug-22-2019, 07:57 AM
RE: Two variables in for loop - by ThomasL - Aug-25-2019, 08:56 AM
RE: Two variables in for loop - by newbieAuggie2019 - Aug-25-2019, 01:14 PM
RE: Two variables in for loop - by ThomasL - Aug-25-2019, 02:12 PM
RE: Two variables in for loop - by newbieAuggie2019 - Aug-26-2019, 05:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,625 Jul-27-2022, 08:50 PM
Last Post: rob101
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,634 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  how to use 3 variables python loop evilcode1 2 1,789 Nov-12-2021, 11:43 AM
Last Post: jamesaarr
  Creating a variables inside FOR loop zazas321 5 4,315 Sep-16-2020, 04:42 PM
Last Post: Naheed
  Two variables in loop Ferdis 1 1,623 Jul-24-2020, 10:18 AM
Last Post: buran
  Create, assign and print variables in loop steven_tr 10 4,625 May-28-2020, 04:26 PM
Last Post: ndc85430
  Operations on indexed variables in loop Fibulavie 1 2,020 Aug-14-2019, 06:07 AM
Last Post: fishhook
  labelling variables of df with map() in loop theinzawoo 2 2,603 Mar-19-2019, 03:42 AM
Last Post: theinzawoo
  I need help with loop one function and creating variables don 3 2,921 Jan-24-2019, 07:31 AM
Last Post: buran
  I can't figure out how to create variables in a while loop MrCag 1 2,477 May-08-2018, 08:56 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020