Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Yoo Song Won

Study help for Python!

Those are list locations, accessing list[x] will give the x-th item in the array, starting at 0. The [1:] is a special case of this for python and languages like it called slice notation, it's a slightly more complex list location that essentially says "every item from the 1st location to the last" in this case (1st location is the 2nd item, because arrays start at 0)

 

Check out https://www.learnpython.org/en/Lists for a beginner rundown on all this stuff.

Recommended Posts

Posted · Original PosterOP

I'm not really sure what the values in the square brackets means in both of these short programs. Can someone please explain the purpose of them? 

G8SBie1A-QyIerbZMfP5ZVtP0n93vsvFoPp-G6y_rHvDwDLBa3rTcIsgCJDUNLDCETU_TR_dvVqqaCuhj0QPZO8cdICC2dckSbqY8YOQEm1zTGPSzWQtXDYShzzjqxyLGigRQSaI

In this one, what does the "[ i ]" do in "totalSum = totalSum + numbers[ i ]"

8BgoQM0rM3Zt38wzaRSqPTM5bJL2kdcKTkxa_CLloP4BfPjuSeR4J0MAMYZ87FwGtcQO0Bgv-_JNeuWh8vIlxj_SE3idKaePLJhUErMdqma4M7T9cYWthCFby1ta3yfocYPGtG21

In this second one, what does the [0] and [1:] do in "return numbers[0] + addNums(numbers[1:])" 

 

Please can you help me in the simplest terms possible? Thank you very much in advance!

Link to post
Share on other sites

i is an iteration. Look at the 'marks' list, you have 5 values defined. It's kind of like saying iterate through this loop a number of times equal to the number of references which python figures out with the "len(numbers)" part. When you get to the totalSum + numbers part it tells python which value in the list to use from the 'marks' list variable. So the first time it adds 0 + 3, then 3 + 6, then 9+2... and so on.


Current Network Layout:

Current Build Log/PC:

Prior Build Log/PC:

Link to post
Share on other sites
Posted · Best Answer

Those are list locations, accessing list[x] will give the x-th item in the array, starting at 0. The [1:] is a special case of this for python and languages like it called slice notation, it's a slightly more complex list location that essentially says "every item from the 1st location to the last" in this case (1st location is the 2nd item, because arrays start at 0)

 

Check out https://www.learnpython.org/en/Lists for a beginner rundown on all this stuff.


¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-3820 | Corsair H100i | ASUS P9X79-LE | 16GB Patriot Viper 3 1866MHz DDR3 | MSI GTX 970 Gaming 4G | 2TB WD Blue M.2 SATA SSD | 2TB Hitachi Deskstar HDD | 1TB WD Black HDD | Corsair CX750M Fractal Design Define R5 Windows 10 Pro / Linux Mint 19 Cinnamon

 

Laptop:

Dell XPS 15 9560 4K Touch | Intel Core i5-7300HQ | 12GB Generic (Crucial?) 2133MHz DDR4 | Nvidia GTX 1050 | 256GB Toshiba M.2 NVMe SSD | Windows 10

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


×