Why Learn Python?

Prentice Wongvibulsin <me@prenticew.com>

http://talks.prenticew.com/pytalk09

Comic by XKCD (#353)

Motivation

Why bother learning a new language?

The right language for the right job.

--

Knowing more languages simply makes your life easier. A carpenter doesn't just learn to use a saw attempt to complete all his tasks with just that one tool. He does not attempt to drive a nail with his saw but he learns how to use a hammer.

To be a more competitive programmer, you'll want to be familiar with many languages and know which language is best suited for which job.

Overview

Why use Python?

  • Quick and easy
  • Built-in high level datatypes
  • Excelent library support

Why NOT Python?

  • Slower than C/C++
  • Potentially difficult to maintain large projects

Objective

Lets get started...

Note: Many parts of this presentation include interactive examples which probably are not included in this presentation.

Numbers

Here we talk about how Python can be used as a calculator and the basic math functions Python provides.

Comments

Variables

Structure

C/C++

Bracket delimited blocks

Python

Whitespace delimited blocks
if (x)
{
   if (y)
   {
      f1();
   }
   f2();
}
if x:
   if y:
      f1()
   f2()

Import

Strings

Lists

This is the most exciting part of Python!

Example:

>>> l = [1,2,3,4,5,6,7,8]
>>> l[2:3]
[3]
>>> l[:3]
[1, 2, 3]
>>> l[4:]
[5, 6, 7, 8]
>>> l[:-1]
[1, 2, 3, 4, 5, 6, 7]

Map

Example:

>>> l = [1,-2,3,-5,10,-99]
>>> map(abs,l)
[1, 2, 3, 5, 10, 99]

Tuples

len

Dictionaries

Example:

>>> dict = {"key1":100,"key2":200}
>>> dict["key1"]
100

Formatting Strings

Example:

str1 = "hello"
str2 = "world"
str3 = "%s, %s." % (str1,str2)

print

Example:

>>> print "hello world"
hello world
>>> i=1
>>> print "the answer is %d" % i
the answer is 1

if

Example:

if b1:
	a=1
elif b2:
	a=2
else:
	a=3

for

Example:

>>> for i in range(5):
...     print i
... 
0
1
2
3
4
>>> for i in [1,2,4,5]:
...     print i
... 
1
2
4
5
>>> import random
>>> [random.randint(0,10) for i in range(5)]
[2, 5, 8, 5, 6]

try-except and raise (Exception Handling)

Example:

try:
	#something potentially bad
except:
	#catch all
    
try:
	#something potentially bad
except ExceptionClass:
	#catch ExceptionClass only

functions

Example:

>>> def myFc(arg1, arg2, arg3="blah"):
...     return arg1, arg2, arg3
... 
>>> print myFc(1,2)
(1, 2, 'blah')
>>> print myFc(arg1=1,arg2="hello")
(1, 'hello', 'blah')

lambda (Anonymous Functions)

Example:

>>> increment = lambda x:x+1
>>> increment(1)
2
>>> def longFc(a,b,c,d):
...     pass
... 
>>> shortFc = lambda x,y:longFc(1,x,y,4)
>>> 
>>> myList = [1,2,3]
>>> map(lambda x:x**2,myList)
[1, 4, 9]

Sorting Lists

Example:

>>> print sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]

>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> print a
[1, 2, 3, 4, 5]

>>> a = [5, 2, 3, 1, 4]
>>> a.sort(lambda x, y: x-y)
>>> print a
[1, 2, 3, 4, 5]

Classes

Example:

class myClass:
	def __init__(self):
    	self.classVar1 = 1
    
    def myFc(self,arg1):
    	pass
        
    def __str__(self):
    	return "myClass"

Inheritance

Example:

class derivedClass(myClass):
	def __init__(self):
    	myClass.__init__(self)

exec

Example:

>>> python_code = "print 'hello'"
>>> exec python_code
hello

Modules and Packages

Package Structure

Example:

package structure image