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

Making Friends And Influencing People - 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
Lists can be concatenated with the + operator,

>>> oldFlames = ["Jennifer", "Susan", "Tina", "Bozo The Clown"] >>> superheroes = ["Spiderman", "Superman", "Human Torch", "Batman"] >>> strangeFriends = oldFlames + superheroes >>> strangeFriends ['Jennifer', 'Susan', 'Tina', 'Bozo The Clown', 'Spiderman', 'Superman', 'Human Torch', 'Batman'] >>>
and repeated with the * operator, in much the same manner as strings andnumbers.

>>> oldFlames * 3 ['Jennifer', 'Susan', 'Tina', 'Bozo The Clown', 'Jennifer', 'Susan', 'Tina', 'Bozo The Clown', 'Jennifer', 'Susan', 'Tina', 'Bozo The Clown'] >>>
"Slices" of a list can be extracted using notation similar to that used forextracting substrings - take a look:

>>> oldFlames ['Jennifer', 'Susan', 'Tina', 'Bozo The Clown'] >>> oldFlames[0] 'Jennifer' >>> oldFlames[0:2] ['Jennifer', 'Susan'] >>> oldFlames[0:5] ['Jennifer', 'Susan', 'Tina', 'Bozo The Clown'] >>> oldFlames[0:] ['Jennifer', 'Susan', 'Tina', 'Bozo The Clown'] >>> oldFlames[:3] ['Jennifer', 'Susan', 'Tina'] >>> oldFlames[-4] 'Jennifer' >>> oldFlames[-1] 'Bozo The Clown' >>>
The built-in len() function can be used to calculate the number of elementsin a list,

>>> superheroes ['Spiderman', 'Superman', 'Human Torch', 'Batman'] >>> len(superheroes) 4 >>>
while the "in" and "not in" operators can be used to test for the presenceof a particular element in a list. A match returns 1 (true), while afailure returns 0 (false).

>>> superheroes ['Spiderman', 'Superman', 'Human Torch', 'Batman'] >>> "batman" in superheroes 0 >>> "Batman" in superheroes 1 >>> "Batma" in superheroes 0 >>> "Incredible Hulk" in superheroes 0 >>> "Incredible Hulk" not in superheroes 1 >>>


 
 
>>> 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: