Home arrow Python arrow Page 4 - Python 101 (part 3): A Twist In The Tail

We Don't Need Another Hero - Python

Find out more about adding flow control to your Python programswith the "for" and "while" loops, see how the range() function can be usedto generate number ranges, and learn all about list objects. And bring anold flame along for the ride.

TABLE OF CONTENTS:
  1. Python 101 (part 3): A Twist In The Tail
  2. Here Comes A Hero
  3. Making Friends And Influencing People
  4. We Don't Need Another Hero
  5. Looping The Loop
  6. Twist And Turn
  7. Within Range()
  8. Just Passin' Through
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 4
June 13, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Unlike strings, lists are "mutable", which means that the elementscontained within a list can be changed at will. For example, any listelement can be altered simply by assigning a new value to it via its index.

>>> superheroes ['Spiderman', 'Superman', 'Human Torch', 'Batman'] >>> superheroes[3] = "Captain America" >>> superheroes ['Spiderman', 'Superman', 'Human Torch', 'Captain America'] >>>
You can alter more than one value at a time by using list slices.

>>> superheroes ['Spiderman', 'Superman', 'Human Torch', 'Captain America'] >>> superheroes[0:2] = ["Incredible Hulk", "Green Lantern"] >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>>
The built-in append() method makes it easy to add items to a list,

>>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>> superheroes.append("Spawn") >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America', 'Spawn'] >>>
while the del() method makes it just as easy to remove them.

>>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America', 'Spawn'] >>> del superheroes[4] >>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>> del superheroes[0:2] >>> superheroes ['Human Torch', 'Captain America'] >>>
Note that there's also a remove() method, which allows you to remove anelement by value rather than index.

>>> superheroes ['Incredible Hulk', 'Green Lantern', 'Human Torch', 'Captain America'] >>> superheroes.remove("Green Lantern") >>> superheroes.remove("Captain America") >>> superheroes ['Incredible Hulk', 'Human Torch'] >>>
Finally, the sort() and reverse() methods allow you to rearrange thecontents of a list.

>>> oldFlames ['Jennifer', 'Susan', 'Tina', 'Bozo The Clown'] >>> oldFlames.sort() >>> oldFlames ['Bozo The Clown', 'Jennifer', 'Susan', 'Tina'] >>> oldFlames.reverse() >>> oldFlames ['Tina', 'Susan', 'Jennifer', 'Bozo The Clown'] >>>
A number of other list methods are also available - take a look at thePython Library Reference at http://www.python.org/doc/current/lib/lib.htmlfor more information and examples.

Let's move on to loops.

 
 
>>> More Python Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: