Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Foundational Concepts
Lecture: Choosing an item at random

Login or purchase this course to watch this video and the rest of the course contents.
0:01 This tip is nice and short but really helpful.
0:03 So let's talk about how we choose a random item.
0:07 Here in PyCharm, first I want to show you the bad
0:09 what I am calling C-style but this appears in many languages.
0:13 So we have these letters, and letters and numbers,
0:15 we'd like to randomly pick one and show it to the user.
0:18 So the most natural thing to do coming from a language like C
0:21 is to create a random number that will be a proper index into that list,
0:25 so we'd say "index = rand", we create "rand" and "n" be from 0 to the "len" of letters.
0:32 And we'll say "item = letters of index".
0:37 Now, this actually includes the upper bound
0:40 so we need to take one away from it
0:42 to make sure we don't have an off-by-one error, but of course,
0:45 this is something you have to go look up in the documentation
0:48 to see - is it including the upper bound and lower bound
0:51 or just lower bound? Something like this.
0:53 And we can just print out the item,
0:55 if I run it, apparently "y" is the randomly selected one
0:58 run it a few more times, "zero", "w", "k", awesome.
1:02 So what's wrong with this? Well, there is a couple of things.
1:05 One, I have to go and calculate the length
1:08 I have to know this is including the upper bound
1:11 when I ask for these random numbers, so I have to do minus 1,
1:14 oh and I forgot to check that there is actually an item in here
1:17 that the index is not negative, something like that.
1:20 So let's write the Pythonic version.
1:23 You'll see it's easier to read, it's shorter, it's safer, everything you want.
1:27 So, I want a random item, and given a sequence,
1:31 I can just say "random, choose a random item", from that sequence.
1:36 Done, I don't have to think about what the documentation says
1:39 about upper and lower bounds,
1:41 I don't have to verify anything, just print out the item,
1:45 run it again, "c" was chosen by the C-style,
1:48 and "d" by the Python style.
1:51 "b", "z", "4", "t", and so on.
1:54 Simple, sweet, but very effective
1:55 and once you start using it, you will never want to go back to the C-style.
1:59 So here is that code in a graphic.
2:01 We have the bad style using the "random int",
2:04 oh and you can see in my slide we actually have a bug about the upper bound,
2:08 how interesting, huh?
2:10 There, I fixed it, but it was interesting that we had this place
2:13 where we could introduce the bug and of course we get the index,
2:16 we use the index to index into the letters, get the item and then we can print it out.
2:19 The Python one instead let's just go random that choice,
2:22 from a sequence, boom, there you go.
2:25 In Python it's generally preferable to use declarative code
2:29 rather than procedural code, and this is a little step in that direction.