Jump to content

Study help for Python!

Go to solution Solved by BobVonBob,

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.

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 comment
https://linustechtips.com/topic/1028974-study-help-for-python/
Share on other sites

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:

Storage Server Setup:

 

Prior Build Log/PC:

Link to comment
https://linustechtips.com/topic/1028974-study-help-for-python/#findComment-12258711
Share on other sites

Link to post
Share on other sites

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-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
https://linustechtips.com/topic/1028974-study-help-for-python/#findComment-12258716
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

×