DS4UX (Spring 2016)/Day 3 follow up: Difference between revisions

From CommunityData
(Created page with "<div style="font-family:Rockwell,'Courier Bold',Courier,Georgia,'Times New Roman',Times,serif; min-width:10em;"> <div style="float:left; width:100%; margin-right:2%;"> {{Link/...")
 
No edit summary
Line 1: Line 1:
<div style="font-family:Rockwell,'Courier Bold',Courier,Georgia,'Times New Roman',Times,serif; min-width:10em;">
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 <code>math_game.py</code>.
<div style="float:left; width:100%; margin-right:2%;">
{{Link/Graphic/Main/2
|highlight color= 27666b
|color=460c40
|link=
|image=
|text-align=left
|top font-size= 1.1em
|top color=FFF
|line color=FFF
|top text=This page is a work in progress.
|bottom font-size= 1em
|bottom color= FFF
|bottom text=Last updated: 20:36, 10 April 2016 (EDT)
|line= none
}}</div></div>
<div style="clear:both;"></div>


*while loops
===Select random items from a set with <code>random.choice()</code>===
*dict.get()
 
*.join()
 
*input()
===Generating a list of numbers easily with <code>range()</code>===
*range()
 
<pre>
>>> range(5)
[0, 1, 2, 3, 4]
>>> for i in range(5):
...    print("Hi" * i)
...
 
Hi
HiHi
HiHiHi
HiHiHiHi</pre>
 
The <code>range()</code> 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 <code>range</code> 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 <code>input()</code> ===
 
>>> 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 <code>while</code> 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 <code>.join</code===

Revision as of 22:46, 17 April 2016

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.

Select random items from a set with random.choice()

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