DS4UX (Spring 2016)/Day 3 follow up

From CommunityData

Here are some important concepts that we didn't have a chance to go into in great detail last week. You can use the sections below to review the concepts individually. You can also review how they work together in math_game.py.

Return random values with the random module

Use random.choice() to select items at random from a list.

>>> import random
>>> my_list = ["terry j.","john","parrot","michael","terry g.", "graham", "llama"]
>>> random.choice(my_list)
'graham'
>>> random.choice(my_list)
'terry j.'
>>> 

Use random.sample() to gather a given number of random items from a list. The first argument you pass to the random.sample() function is the set of items you are sampling from. The second argument is the number of items you want to gather from that set.

>>> random.sample(my_list,3)
['terry j.', 'llama', 'michael']

Use random.randint() to gather a random number from a list of numbers. You specify the list of sequential numbers by passing the starting number as the first argument, and the final number as the last argument. Unlike with range() function discussed below, when you use randint() both the first and last numbers you specify are included in the set you are sampling from.

>>> random.randint(1,10)
8
>>> random.randint(1,10)
3
>>> random.randint(1,10)
10
>>> 

Generating a list of numbers easily with range()

>>> range(5)
[0, 1, 2, 3, 4]
>>> for i in range(5):
...     print("Hi" * i)
...

Hi
HiHi
HiHiHi
HiHiHiHi

The range() function returns a list of numbers. This is handy for when you want to generate a list of numbers on the fly instead of creating the list yourself.

>>> range(5)
[0, 1, 2, 3, 4]

Use range when you want to loop over a bunch of numbers in a list, or perform an operation a certain number of times:

>>> numbers = range(5)
>>> for number in numbers:
...     print(number * number)
...
0
1
4
9
16

We could rewrite the above example like this:

>>> for number in range(5):
...     print(number * number)
...
0
1
4
9
16

You can also set the start, end, and increment value (called "step") for a range.

>>> for i in range(2,20,2):
...         print(i)
2 
4
6
8
10
12
14
16
18


Get user input with input()

>>> for i in range(100):
...     my_input = input("Please type something> ")
...     if my_input == "Quit":
...         print("Goodbye!")
...         break
...     else:
...         print("You said: " + my_input)
... 
Please type something> Hello
You said: Hello
Please type something> How are you?
You said: How are you?
Please type something> Quit
Goodbye!
>>>

Iterating an indeterminate number of times with while loops

grocery_list = []
testAnswer = input('Press y if you want to enter more groceries: ')
while testAnswer == 'y':
    food = input('Next item:')
    grocery_list.append(food)
    testAnswer = input('Press y if you want to enter more groceries: ')
print('Your grocery list:')
for food in grocery_list:
    print(food)


Splicing list items together with .join</code