HomePython Page 3 - Python 101 (part 2): If Wishes Were Pythons
Q & A - Python
Begin your tour of Python with a look at its number and stringtypes, together with examples of how they can be used in simple Pythonprograms. Along the way, you'll also learn how to build conditionalexpressions, slice and dice strings, and accept user input from the commandline
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!
>>>