Python
  Home arrow Python arrow Page 6 - Python 101 (part 2): If Wishes Were Py...
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
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 101 (part 2): If Wishes Were Pythons
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2001-05-31

    Table of Contents:
  • Python 101 (part 2): If Wishes Were Pythons
  • Tax Evasion
  • Q
  • Sliced And Diced
  • Comparing Apples And Oranges
  • If Only...
  • Tying Up The Loose Ends
  • Cookie-Cutter Code
  • Time For Lunch

  • 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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    Python 101 (part 2): If Wishes Were Pythons - If Only...


    (Page 6 of 9 )

    Why do you need to know all this? Well, comparison operators come in very useful when building conditional expressions - and conditional expressions come in very useful when adding control routines to your code. Control routines check for the existence of certain conditions, and execute appropriate program code depending on what they find.

    The first - and simplest - decision-making routine is the "if" statement, which looks like this:

    if condition: do this!
    The "condition" here refers to a conditional expression, which evaluates to either true or false. For example,

    if hearing spooky noises: call Ghostbusters!
    or, in Python lingo:

    if spooky_noises == 1: callGhostbusters()
    If the conditional expression evaluates as true, all statements within the indented code block are executed. If the conditional expression evaluates as false, all statements within the indented block will be ignored, and the lines of code following the "if" block will be executed.

    One of the unique things about Python is that it does not require you to enclose conditional statement blocks within curly braces, like most other languages. Instead, Python identifies code blocks according to indentation; all Python statements with the same amount of indentation are treated as though they belong to the same code block.

    If, however, the conditional block consists of a single statement, Python also allows you to place it on the same line as the "if" statement. Consequently, the example above could also be written as

    if spooky_noises == 1: callGhostbusters()
    Here's a simple program that illustrates the basics of the "if" statement.

    #!/usr/bin/python # ask for a number alpha = input("Gimme a number! ") # ask for another number beta = input("Gimme another number! ") # check if alpha == beta: print ("Can't you read, moron? I need two *different* numbers!") print ("Go away now!")
    And they say that the popular conception of programmers as rude, uncouth hooligans is untrue!

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


     

       

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





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