Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
Python loops cheat sheet
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== For loops == Use a <code>for</code> loop to do something to every element in a list. >>> names = ["Jessica", "Adam", "Liz"] >>> for name in names: ... print(name) ... Jessica Adam Liz >>> names = ["Jessica", "Adam", "Liz"] >>> for name in names: ... print("Hello " + name) ... Hello Jessica Hello Adam Hello Liz === <code>if</code> statements inside <code>for</code> loop === >>> for name in ["Alice", "Bob", "Cassie", "Deb", "Ellen"]: ... if name[0] in "AEIOU": ... print(name + " starts with a vowel.") ... Alice starts with a vowel. Ellen starts with a vowel. === Building up a list === Sometimes you want to build up a new list based on information about each element in an existing list. To do this, initialize an empty list before the <code>for</code> loop, and append elements to the new list inside the <code>for</code> loop: >>> vowel_names = [] >>> for name in ["Alice", "Bob", "Cassie", "Deb", "Ellen"]: ... if name[0] in "AEIOU": ... vowel_names.append(name) ... >>> print(vowel_names) ['Alice', 'Ellen'] === Using a counter === Sometimes you want to keep track of the number of occurrences of something, or a running total, as you loop through a list. To do this, initialize a variable before the <code>for</code> loop that you update inside the <code>for</code> loop: >>> prices = [1.5, 2.35, 5.99, 16.49] >>> total = 0 >>> for price in prices: ... total = total + price ... >>> total 26.33 === <code>for</code> loops inside <code>for</code> loops === You can put <code>for</code> loops inside <code>for</code> loops. The indentation dictates which <code>for</code> loop a line is in. >>> letters = ["a", "b", "c"] >>> numbers = [1, 2, 3] >>> for letter in letters: ... for number in numbers: ... print(letter * number) ... a aa aaa b bb bbb c cc ccc The order of the <code>for</code> loops matters. Compare the above example with this one: >>> for number in numbers: ... for letter in letters: ... print(number * letter) ... a b c aa bb cc aaa bbb ccc == Useful functions related to lists and for loops == ==== sorting lists ==== Use <code>.sort()</code> to sort a list: >>> names = ["Eliza", "Joe", "Henry", "Harriet", "Wanda", "Pat"] >>> names.sort() >>> names ['Eliza', 'Harriet', 'Henry', 'Joe', 'Pat', 'Wanda'] ==== Getting the maximum and minimum values from a list ==== >>> numbers = [0, 3, 10, -1] >>> max(numbers) 10 >>> min(numbers) -1 ==== Generating a list of numbers easily with <code>range()</code> ==== 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: >>> 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 === 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! >>> [[Category:CDSW]]
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information