SunQuest
 
       Python
  Home arrow Python arrow Page 3 - 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 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    - 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