Python: for Loops

Python

This is post 20 of 30 in the series “Introduction to Python” Go to Chapter 21 of A Smarter Way. Remember an earlier for loop we used: friends = [‘bill’, ‘jeff’, ‘jim’] for friend in friends: print(“Hello, ” + friend.title() + “!”) Often we use these inside if-tests: friends = [‘bill’, ‘joel’, ‘jeff’, ‘jim’, ‘scott’, …

Python: Tuples

Python

This is post 19 of 30 in the series “Introduction to Python” Go to Chapter 20 of A Smarter Way. A tuple is like a list, but its elements cannot be changed once they’re defined. In a list we used square brackets to define the list: friends = [‘bill’, ‘jeff’, ‘jim’, ‘scott’, ‘joel’] By contrast, …

Python: Lists: Take a Slice, Delete Elements, Popping Elements

Python

This is post 18 of 30 in the series “Introduction to Python” Go to Chapter 17 of A Smarter Way. Return to our lists page: http://introtopython.org/lists_tuples.html#Want-to-see-what-functions-are? …and scroll down slightly because the page TOC is (again) broken. Taking a slice from a list: friends = [‘bill’, ‘jeff’, ‘jim’, ‘scott’, ‘joel’] # Grab the first three …

Python: Adding To and Changing Lists

Python

This is post 17 of 30 in the series “Introduction to Python” Go to Chapter 16 of A Smarter Way. Return to our lists page: http://introtopython.org/lists_tuples.html#Common-List-Operations Change an item in a list: friends = [‘bill’, ‘jeff’, ‘jim’] friends[0] = ‘fred’ print(friends) Add an item to a list: friends.append(“joel”) # Note that we use parens, not …

Python: Lists

Python

This is post 16 of 30 in the series “Introduction to Python” Go to Chapter 15 of A Smarter Way. Let’s start out on this highly detailed page on handling lists: http://introtopython.org/lists_tuples.html#Introducing-Lists Create an empty list: friends = [] Populate a list: friends = [‘bill’, ‘jeff’, ‘jim’] for friend in friends: print(“Hello, ” + friend.title() …