Python
  Home arrow Python arrow Page 2 - Python: Input and Variables
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? 
PYTHON

Python: Input and Variables
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-11-19


    Table of Contents:
  • Python: Input and Variables
  • Beating...I Mean Asking the User for Information
  • Loops
  • For Loop

  • 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: Input and Variables - Beating...I Mean Asking the User for Information
    ( Page 2 of 4 )

    So now that we know everything there is to know about printing and we know about variables, let's learn how to get data from the user and work with it.


    #!/usr/local/bin/python


    print “Go no further!”

    name = raw_input(“Who are you?”)

    print “Did you say:”, name

    The above code prints a statement: Go no further! Then it asks the user: Who are you? and prompts them for some text. When the user types in the text and presses enter, it then prints: Did you say: and appends the value of the variable name to the end.

    Let's say the user entered in the name: The Knights Who Go Neep! This is what the code would print:

      Go no further!

      Who are you?

      Did you say: The Knights Who Go Neep!

    We use raw_input when we want to get textual input from a user. If we wanted to get a numeric value, we would use input, like so:


    #!/usr/local/bin/python


    age = input(“Enter your age: “)

    print “You are”, age

    print “If I were twice your age I would be: “, age * 2

    Same principle as before. If the user had entered an age of 20, this would be the printout result:

      Enter your age:

      You are 20

      If I were twice your age I would be 40

    And of course we could always combine the two together:


    #!/usr/local/bin/python


    first_name = raw_input(“What is your first name?”)

    last_name = raw_input(“What is your last name?”)

    age = input(“How old are you?”)


    print “Your full name is “, first_name + last_name

    Print “And this is your age: “, age

    If the user had answered Lando Calrissian 50, then the printout would be:

      Your full name is LandoCalrissian

      And this is your age: 50

    When you use the + symbol with text it is called concatenating strings, or adding them to one another. You will note that the values in the concatenated sentence have no space. You must add one to the sentence like so:


    #!/usr/local/bin/python


    first_name = raw_input(“What is your first name?”)

    last_name = raw_input(“What is your last name?”)

    age = input(“How old are you?”)


    print “Your full name is “, first_name + “ “ + last_name

    Print “And this is your age: “, age

    This will print:

      Your full name is Lando Calrissian

      And this is your age: 50



     
     
    >>> More Python Articles          >>> More By James Payne
     

       

    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 5 Hosted by Hostway
    Stay green...Green IT