(12 ratings)   
By: techiwarehouse.com
When known to the interpreter, the script name and additional arguments thereafter are passed to the script in the variable sys.argv, which is a list of strings. Its length is at least one; when no script and no arguments are given, sys.argv[0] is an...
Added: 29 April 2008    Views: 171  
PathComputers    Programming    Python
Keywords: computer   program   python   programming   language   code   coder   developer   object   string   name   class   classes   function   web  
Do you like this tutorial? Now you can support our team to add more :     
 
 
 
Functions

Function calls

You have already seen one example of a function call
: type("32")
type 'string'
The name of the function is type, and it displays the type of a value or variable. The value or variable, which is called the argument of the function, has to be enclosed in arentheses. It is common to say that a function "takes" an argument and "returns" a result.

The result is called the return value.
Instead of printing the return value, we could assign it to a variable:
betty = type("32")
print betty
As another example, the id function takes a value or a variable and returns an integer that acts as a unique identifier for the value:

id(3)
134882108
betty = 3
id(betty)
134882108

Every value has an id, which is a unique number related to where it is stored in the memory of the computer. The id of a variable is the id of the value to which it refers.
.

String

A compound data type

So far we have seen three types: int, float, and string. Strings are qualitatively different from the other two because they are made up of smaller pieces characters.

Types that comprise smaller pieces are called compound data types. Depending on what we are doing, we may want to treat a compound data type as a single thing, or we may want to access its parts. This ambiguity is useful.
The bracket operator selects a single character from a string.

fruit = "banana"

letter = fruit[1]

print letter

The expression fruit[1] selects character number 1 from fruit. The variable letter refers to the result. When we

display letter, we get a surprise:

a

The first letter of "banana" is not a. Unless you are a computer scientist. For perverse reasons, computer scientists always

start counting from zero. The 0th letter ("zero-eth") of "banana" is b. The 1th letter ("one-eth") is a, and the 2th ("two-eth")

letter is n.

If you want the zero-eth letter of a string, you just put 0, or any expression with the value 0, in the brackets:

letter = fruit[0]

print letter

b
The expression in brackets is called an index. An index specifies a member of an ordered set, in this case the set of characters in the string. The index indicates which one you want, hence the name. It can be any integer expression.

Length

The len function returns the number of characters in a string:

fruit = "banana"

len(fruit)

6

To get the last letter of a string, you might be tempted to try something like this:

length = len(fruit)

last = fruit[length] # ERROR!

That won't work. It causes the runtime error IndexError: string

index out of range. The reason is that there is no 6th letter in "banana". Since we started counting at zero, the six letters are numbered 0 to 5. To get the last character, we have to subtract 1 from length:

length = len(fruit)

last = fruit[length-1]

Alternatively, we can use negative indices, which count backward from the end of the string. The expression fruit[-1] yields the last letter, fruit[-2] yields the second to last, and so on.

Traversal and the for loop

A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal. One way to encode a traversal is with a while statement:

index = 0

while index < len(fruit):

letter = fruit[index]

print letter

index = index + 1

This loop traverses the string and displays each letter on a line by itself. The loop condition is index < len(fruit), so when index is equal to the length of the string, the condition is false, and the body of the loop is not executed. The last character accessed is the one with the index len(fruit)-1, which is the last character in the string.

As an exercise, write a function that takes a string as an argument and outputs the letters backward, one per line. Using an index to traverse a set of values is so common that Python provides an alternative, simpler syntax the for loop:

for char in fruit:

print char

Each time through the loop, the next character in the string is assigned to the variable char. The loop continues until no characters are left.

The following example shows how to use concatenation and a for loop to generate an abecedarian series. "Abecedarian" refers to a series or list in which the elements appear in alphabetical order. For example, in Robert McCloskey's book Make Way for Ducklings, the names of the ducklings are Jack, Kack, Lack, Mack, Nack, Ouack, Pack, and Quack. This loop

outputs these names in order:

prefixes = "JKLMNOPQ"

suffix = "ack"

for letter in prefixes:

print letter + suffix

The output of this program is:

Jack

Kack

Lack

Mack

Nack

Oack

Pack

Qack

When known to the interpreter, the script name and additional arguments thereafter are passed to the script in the variable sys.argv, which is a list of strings. Its length is at least one; when no script and no arguments are given, sys.argv[0] is an empty string. When the script name is given as '-' (meaning standard input), sys.argv[0] is set to '-'. When -c command is used, sys.argv[0] is set to '-c'. Options found after -c command are not consumed by the Python interpreter's option processing but left in sys.argv for the command to handle.
About the Author :
Functions and Strings -Python
Articles and Tutorials Directory by www.learnfobia.com
 Rate this tutorial : Rate 1Rate 2Rate 3Rate 4Rate 5
  |    Add to Favorites
  |    Send to Friend
  |    Print
Comments