## Why Python is a Great Language to Learn?

### Yusuke Shinyama

• "Runnable Pseudocode" - great to implement your algorithms.
• Has a garbage collector.
• Lots of convenient types. See below.
• Slew of libraries, well documented.
• Interactive Features. See below.
• GUI. Screenshot.
• Runs on Linux, Solaris, Windows and Mac.
• Of course, it's free!

## Clean and Visible Structure

Python recognizes blocks by indentation. (No worry, you will love it.)

```# Simple implementation of

Mint problem.

# Define constants.
INF = 99999
denom = [1, 5, 10, 25, 50]

# Initialize the table.
table = [0] * 100

# Recursively compute the number of coins needed
for exact change: can't make change for negative numbers; no change
for 0; otherwise recurse.
def coins(n):
if n < 0:
return INF
elif n == 0:
return 0
else:
if table[n] == 0:
table[n] = min([ coins(n-d) for d in denom ]) + 1
return table[n]

# Sample run
print coins(77)
print [ coins(i) for i in range(1,100) ]
```

## Functions can Return Multiple Values

```# Return the quotient and the remainder at once.
def div(x, y):
quotient = int(x / y)
remainder = x % y
return (quotient, remainder)

# Assign two values to different variables.
(q, r) = div(33, 5)

print q
print r
```

## Elegant String/List Handling

Strings and Arrays can be handled in the same syntax.
```a = "0 p 1 y 2 t 3 h 4 o 5 n 6"```
• `a[0]` == `"p"`
• `a[-1]` == `"n"`
• `a[2:4]` == `"th"`
• `a[1:]` == `"ython"`
• `a[:-3]` == `"pyt"`
• `a[2:-1]` == `"tho"`

## Interactive Features

Python has nice interactive features (Bash-like line editing, function completion and history), which make it easy to play with built-in functions and libraries. It also includes online help function for almost every function in the library.

Here is a sample session:

```\$ python
Python 2.3.3 (#1, Feb  1 2004, 23:04:28)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
>>> fp=file("/etc/hosts") (get a file object first)
>>> fp. (press Tab key here, it tries to expand the available methods)
fp.__class__         fp.__new__           fp.closed            fp.newlines          fp.softspace
fp.__delattr__       fp.__reduce__        fp.encoding          fp.next              fp.tell
fp.__iter__          fp.close             fp.name              fp.seek

readline([size]) -> next line from the file, as a string.

Retain newline.  A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.

'127.0.0.1 localhost\n'
>>>
```

## Python Types

• Number
• Int: `4*33`
• Float: `1.0/3.0` --> `0.33333333333`
• Complex: `(1+2j)*(3+4j)`--> `(-5+10j)`
• String
• `x = 'abc'`
• `x[1]` --> `'b'`
• `x[1:3]` --> `'bc'`
• `'abc' + 'def'` --> `'abcdef`
• `'abc'*3` --> `'abcabcabc'`
• Python strings are immutable. (to guerantee of use as a hash key)
• Python has Unicode supports (for Europian and Asian languages).
• List (Sequence)
• `x = [1,2,3,4,5]`
• `x[1]` --> `2`
• `x[1:3]` --> `[2,3]`
• `x[3] = 3.5`
• `x.append(6)`
• `del x[1]`
• `[0]*100` --> `[0,0,0,0, ..., 0,0,0]` (100 elements)
• `[1,2] + [3,4]` --> `[1,2,3,4]`
• `range(10)` --> `[0,1,2,3,4,5,6,7,8,9]`
• `[ n*n for n in range(10) ]` --> `[0,1,4,9,16,25,36,49,64,81]`
• `[ n*n for n in range(10) if n%2 == 0]` --> `[0,4,16,36,64]`
• Hash (Dictionary, Mapping)
• `x = {"a":1, "b":2}`
• `x["a"]` --> `1`
• `x["nosuchkey"]` --> Exception
• `x["newkey"] = 4`
• `del x["newkey"]`
• Class
• `myobj = MyClass()` (Unlike Java, there's no need for a "`new`" operator)
• `myobj.method(args)`
• `print myobj` (Customizable print function)

Yusuke Shinyama