Python Forum
Question about Python Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about Python Code
#1
Hi, I was wondering if someone can help me understand this Python code. If I have a list with a collection of 3, 2, 5, 7, 6, 8 as show below.  How is "i" assigned in this unordered list?  Say if I am starting from index 0, which is 3, so is i =3 and i+1 = 2?  Assuming if this is the case, then wouldn't 3 and 2 would not be switch, since i = 3 > i + 1= 2? 

I tried to workout this algorithm by hand, and I'm not getting the same result as the output. The resulting output is an ordered list of 2, 3, 5, 6, 7, 8.

Also, is len(aList)-2 is 3, 2, 5, 7 instead of 3, 2, 5, 7, 6, 8?
aList = [3, 2, 5, 7, 6, 8]
stop = len(aList) - 2
for i in aList:  

 for i in range (0, stop):   
  if aList[i] > aList[i+1]:   
  temp = aList[i]   
  aList[i] = aList[i+1]   
  aList[i+1] = temp

print (aList)
Reply


Messages In This Thread
Question about Python Code - by Than999 - Nov-04-2019, 06:39 PM
RE: Question about Python Code - by Axel_Erfurt - Nov-04-2019, 07:08 PM
RE: Question about Python Code - by jefsummers - Nov-04-2019, 08:34 PM
RE: Question about Python Code - by Than999 - Nov-04-2019, 10:14 PM
RE: Question about Python Code - by Than999 - Nov-04-2019, 11:36 PM
RE: Question about Python Code - by jefsummers - Nov-05-2019, 02:59 AM
RE: Question about Python Code - by Than999 - Nov-05-2019, 07:02 PM
RE: Question about Python Code - by ichabod801 - Nov-05-2019, 07:10 PM
RE: Question about Python Code - by Than999 - Nov-05-2019, 07:32 PM
RE: Question about Python Code - by jefsummers - Nov-05-2019, 08:30 PM
RE: Question about Python Code - by Than999 - Nov-05-2019, 11:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Use Python for solving the following physics question. Python Code required ishahid 8 3,862 Dec-18-2019, 06:59 AM
Last Post: akashraj128

Forum Jump:

User Panel Messages

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