Python
  Home arrow Python arrow Python Conditionals, Lists, Dictionari...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python Conditionals, Lists, Dictionaries, and Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-11-26

    Table of Contents:
  • Python Conditionals, Lists, Dictionaries, and Operators
  • Elif
  • Lists
  • Tuples
  • Dictionaries
  • Operators

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Save your reputation with your customers. Learn how you can have embedding success with Advantage Database Server (ADS).

    Python Conditionals, Lists, Dictionaries, and Operators
    (Page 1 of 6 )

    In our last article we learned how to get input from the user, store data in a variable, and work with some basic operators to manipulate that data. In this article we will learn to use Conditional Statements and possibly create functions. So wipe that mustard off your chin, clean the dishes, and let's get to work.

    Conditional Statements

    Sometimes in programming you need to ask certain questions and have the program respond accordingly. You can do so with conditional statements. Basically the way they work is like this: "If this, then that." They get more complicated of course. You also can say "If this happens, do this, else do that." Or even If this and That, or This or That, then do this else do that or that.

    If Statements

    The If Statement simply says: "If this, do that." Here is how that looks in code:

    Note: don't forget that with Python you MUST indent.


    #!/usr/local/bin/python


    name = “The Black Knight”


        if name == “The Black Knight”:

            print “I'm not dead yet!”

    The above code creates a variable named (oddly enough) name. It then stores the value “The Black Knight” in it. It then enters an If Statement, that says If the value of name is “The Black Knight” then print “I'm not dead yet.” If not, do nothing. In this instance, the criteria is met and so the following is printed to the screen:

      I'm not dead yet!

    Pretty simple right? But obviously lacking in what the poet JJ (aka Jimmy Walker) would call DYN-O-MITE...ness. What if the name wasn't the Black Knight? For that, you would need to use our good old buddy the Else Clause.


    #!/usr/local/bin/python


    name = “The Purple Knight”


        if name == “The Black Knight”:

            print “I'm not dead yet!”

        else:

            print “Now I am dead.”

    Since the value of name is not equal to “The Black Knight”, the program continues on to the else clause and prints the following text to the monitor:

      Now I am dead.

    Before we get into the next Conditional Statement, let's take a look at a few operators. Check out the table below:


    Operator

    What it Does

    <

    Less Than

    >

    Greater Than

    <=

    Less than or equal to

    >=

    Greater than or equal to

    “==”

    Equal to

    !=

    Not equal

    <>

    A different way to say not equal

    More Python Articles
    More By James Payne


       · In [7]: suess = ['I', 'like', 'green', 'eggs', 'and', 'spam']In [8]: print...
       · I used a combo box in form along with text boxes.. now when the user submit the...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway