Python
  Home arrow Python arrow Page 3 - Python 101 (part 2): If Wishes Were Pythons
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Python 101 (part 2): If Wishes Were Pythons
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Python 101 (part 2): If Wishes Were Pythons - Q
    ( Page 3 of 9 )

    Thus far, you've been assigning values to your variables at the time of writing the program itself (referred to in geek lingo as "design time"). In the real world, however, many variables are assigned only after the program is executed (referred to as "run time"). And so, before moving on to strings, I'd like to take some time out to demonstrate a Python program which allows you to ask the user for input, assign this input to a variable, and then perform specific actions on that variable.

    #!/usr/bin/python # ask a question... number = input("Gimme a number! ") # process the answer... square = number ** 2 # display the result print "The square of", number, "is", square
    If you try it out, you'll see something like this:

    $ Gimme a number! 4 The square of 4 is 16 $
    The first line of the program prints a prompt, asking the user to enter a number; this is the purpose of the input() function call. Once the user enters a number, it is raised to the power 2 and the result is assigned to the a new variable. Finally, the print() statement is used to output the result to the terminal.{mospagebreak title=Playing It Again...And Again...And Again...} Next up, strings. String values can be assigned to variables in the same manner as numeric values, except that strings are always surrounded by single or double quotes.

    >>> name = "popeye" >>> snack = 'spinach'
    Note that if your string contains quotes, it's necessary to escape them with a backslash.

    >>> string = "...and so he said, \"backslash me, knave!\"" >>> print string ...and so he said, "backslash me, knave!" >>>
    Python also allows you to create strings which span multiple lines, by enclosing them in triple quotes ("""). The original formatting of the string, including newlines and whitespace, is retained when such a string is printed.

    >>> welcome = """Hello, and how ... are you today? My name is Vikram ... and I will be your guide to the wonderful and ... mysterious world of Python.""" >>> print welcome Hello, and how are you today? My name is Vikram and I will be your guide to the wonderful and mysterious world of Python. >>>
    Strings can be concatenated with the + operator, in much the same way as numbers are added. Note that although the operator is the same, Python possesses enough intelligence to decide whether the operation is addition or concatenation on the basis of the object type.

    >>> a = "ding" >>> b = "dong" >>> c = "bell" >>> abc = a + b + c >>> abc 'dingdongbell' >>>
    Strings placed next to each other are automatically concatenated.

    >>> print "ding" "dong" "bell" dingdongbell >>> # this is equivalent >>> print "ding" + "dong" + "bell" dingdongbell >>>
    A quick aside on the print() function call here: typically, if arguments passed to print() are separated with commas, Python automatically prints them one after the other (separated by a single space) and adds a line break at the end.

    >>> print "ding", "dong", "bell" ding dong bell >>>
    Python also includes a repetition operator, the asterisk (*), used to repeat a string a number of times.

    >>> famousoldmovieline = "Play it again, Sam!\n" >>> print famousoldmovieline * 4 Play it again, Sam! Play it again, Sam! Play it again, Sam! Play it again, Sam! >>>


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

       

    PYTHON ARTICLES

    - 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
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek