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. So let's talk about how we choose a random item. Here in PyCharm, first I want to show you the bad
0:10
what I am calling C-style but this appears in many languages. So we have these letters, and letters and numbers,
0:16
we'd like to randomly pick one and show it to the user. So the most natural thing to do coming from a language like C
0:22
is to create a random number that will be a proper index into that list,
0:26
so we'd say "index = rand", we create "rand" and "n" be from 0 to the "len" of letters. And we'll say "item = letters of index".
0:38
Now, this actually includes the upper bound so we need to take one away from it to make sure we don't have an off-by-one error, but of course,
0:46
this is something you have to go look up in the documentation to see - is it including the upper bound and lower bound
0:52
or just lower bound? Something like this. And we can just print out the item, if I run it, apparently "y" is the randomly selected one
0:59
run it a few more times, "zero", "w", "k", awesome. So what's wrong with this? Well, there is a couple of things.
1:06
One, I have to go and calculate the length I have to know this is including the upper bound
1:12
when I ask for these random numbers, so I have to do minus 1, oh and I forgot to check that there is actually an item in here
1:18
that the index is not negative, something like that. So let's write the Pythonic version.
1:24
You'll see it's easier to read, it's shorter, it's safer, everything you want. So, I want a random item, and given a sequence,
1:32
I can just say "random, choose a random item", from that sequence. Done, I don't have to think about what the documentation says
1:40
about upper and lower bounds, I don't have to verify anything, just print out the item, run it again, "c" was chosen by the C-style,
1:49
and "d" by the Python style. "b", "z", "4", "t", and so on. Simple, sweet, but very effective
1:56
and once you start using it, you will never want to go back to the C-style. So here is that code in a graphic.
2:02
We have the bad style using the "random int", oh and you can see in my slide we actually have a bug about the upper bound, how interesting, huh?
2:11
There, I fixed it, but it was interesting that we had this place where we could introduce the bug and of course we get the index,
2:17
we use the index to index into the letters, get the item and then we can print it out. The Python one instead let's just go random that choice,
2:23
from a sequence, boom, there you go. In Python it's generally preferable to use declarative code
2:30
rather than procedural code, and this is a little step in that direction.