view · edit · attach · print · history

Links

Basics

In this part you will learn how to use python interpreter and also about writing basic constructs like for if else and functions

Classes and Objects

Basic Algorithms

Bubblesort Example

  def bubblesort(arr):
      for i in range(len(arr)-1):
          for j in range(len(arr)-1):
              if(arr[j]>arr[j+1]):
                  temp=arr[j]
                  arr[j]=arr[j+1]
                  arr[j+1]=temp

  arr=[5,1,1,45,233,2,34,343]
  print arr
  bubblesort(arr)
  print arr

Dynamic Execution in python

You can execute stuff dynamically using exec function

  exec("x=1;print x")

Database Connections

There is a lot of support for databases using python.

Connecting to a database and retrieving results

  import MySQLdb
  conn=MySQLdb.connect("localhost","test","test123","Blog")
  sql="SELECT * FROM USERS"
  cursor=conn.cursor()
  cursor.execute(sql)
  rows=cursor.fetchall()
  for i in range(len(rows)):
  	for j in range(len(rows[i])):
  		print (str)(rows[i][j]) +",",
  	print "\n"

You will need MySQLdb which can be downloaded from sf.net

view · edit · attach · print · history
Page last modified on November 18, 2006, at 04:30 PM