Python Forum
Trouble Understanding Why This Code Works
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble Understanding Why This Code Works
#2
After running
z, y, x = 2, 1, 0 
x, z = z, y
y = y - z 

print(x, y, z)
you can realise that x = 2, y = 0 & z = 1

with the variables that are being assigned to in the same order as the final print, the variables on the right are arranged to give correct order
x, y, z = y, z, x # same as x, y, z = 0, 1, 2
with with the variables that are being assigned to in the reverse order of the final print, the variables on the right are also arrange to give the reverse order.
z, y, x = x, z, y # same as z, y, x = 2, 1, 0
then the print is in the order of x, y, z
print(x, y, z)
Reply


Messages In This Thread
RE: Trouble Understanding Why This Code Works - by Yoriz - Apr-05-2019, 03:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 1,344 Nov-15-2023, 06:56 PM
Last Post: jst
  Code understanding: Need help in understanding dictionary code jt123 0 526 Jul-09-2023, 01:13 PM
Last Post: jt123
  Code works but doesn't give the right results colin_dent 2 821 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 1,986 May-09-2023, 08:47 AM
Last Post: buran
  Code used to work 100%, now sometimes works! muzicman0 5 1,606 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Understanding a piece of code Michael1 4 1,540 Jan-20-2022, 07:14 PM
Last Post: Michael1
  Pyspark - my code works but I want to make it better Kevin 1 1,883 Dec-01-2021, 05:04 AM
Last Post: Kevin
Question email code works in 2.7 but not in 3 micksulley 3 2,690 Nov-04-2021, 09:44 PM
Last Post: micksulley
  My simple code don't works !! Nabi666 1 1,662 Sep-06-2021, 12:10 PM
Last Post: jefsummers
  HackerRank Problem: Code works on VS Code but not on the HackerRank site Pnerd 3 2,808 Feb-28-2021, 07:12 PM
Last Post: Pnerd

Forum Jump:

User Panel Messages

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