Ted Patrick > { Events & Community } > Adobe Systems


Python 101

I am a big fan of the Python language. Having worked with it for years, I keep finding that aspects of the language are well suited to solving real world programming problems. Here is a brief tutorial on Python.&

Download Python
Code Examples

Hello World Example:

print( 'Hello Python World' )

Variable based Hello World Example:

h = 'Hello'
w = 'World'
p = 'Python'
print ( h + ' ' + p + ' ' w )


Python has a interactive command line that ships with all versions. It allows you to type programs into the interpreter line by line and get instant feedback to test assumptions on small parts of programs. As you type lines they execute when you press return.

Read and Write to a file:

file = open('foo2.txt', 'w')
file.write("testing, testing\n")
file.close()
print open('foo2.txt').read()


Download a Web Page and print it:

import urllib
pageFileObject = urllib.urlopen('http://www.macromedia.com')
print( pageFileObject.read() )


There are thousands of libraries for Python and all of them are imported via the import command. Actually any application you write can be imported via import allowing you to reuse code seamlessly. Once you import a library, you can use the class/code objects as needed in your application. Take some time to explore the Python installation especially /lib contains source (.py) and bytecode (.pyc) for all of Python's base types and extended types. If you open .py files in a text editor you can quickly see what the library does. In the above example I imported urllib, which is actually /lib/urllib.py and used the open method to retrieve a web page into a file object. Then using read(), it returned the text of the file to the print method.

One aspect of Python is very confusing to anyone new to Python. Python treats some whitespace characters as valid code to denote code blocks. When you write blocks all code of equal indentation is within the block.

If block example:

a = True

if a:

      print( 'a is True' )
      print( 'Still within the if block' )

else:

      print( 'a is False' )
      print( 'Still within the else block' )


That seems weird for many (Chafic and Darron included!) as it removes most of the {} and ; within code, in Python they just are not needed. The interesting thing is that Python code is very readable because all developers must format software in according to block indentation standard. Python just enforces you to do what you should be doing already.



I wish you all the best on your Python expedition. Should you have any questions you know where to reach me ( ted -AT- powersdk -DOT- com ).

Python Official Tutorial

Cheers,

ted ;)

4 Responses to “ Python 101 ”

  1. # Anonymous Pradeep Gowda

    the ()'s are not needed in Python.

    eg:
    print 'Ted on Flex'

    works just fine
    the same applies to even `if` and other statements...

    if a > b:
    foo

    for i in somelist:
    do_something()

    if a and b:
    c()

    etc...  

  2. # Blogger Parallel Python

    This post has been removed by the author.  

  3. # Blogger Parallel Python

    > h = 'Hello'
    > w = 'World'
    > p = 'Python'
    > print ( h + ' ' + p + ' ' w )
    The last line could be replaced with:
    print h, p, w


    -------------------
    Parallel Python  

  4. # Blogger Nish

    This is a good article on Python. I think Phython hasnt gotten enough and due attention.
    ____________________________________
    Flash Developer  

Post a Comment



© 2008 Ted On Flash