Vous êtes sur la page 1sur 3

Python 101 The basics

Python is one of those rare languages that claim to be both simple and powerful. Python is an interpreted, high level, object oriented programming language. Was created by Guido van Rossum (currently works for google).

Majority of the Linux users should have python already installed. If not, it should be available for download in you official software repositories. Windows users, visit http://python.org/download/ and download the desired version for windows. DOS Prompt If you want to be able to use Python from the Windows command line i.e. the DOS prompt, then you need to set the PATH variable appropriately. For Windows 2000, XP, 2003 , click on Control Panel -> System -> Advanced -> Environment Variables. Click on the variable named PATH in the 'System Variables' section, then select Edit and add ;C:\Python27 to the end of what is already there. Of course, use the appropriate directory name.

Simple and easy to learn Free and open source High level language Portable Interpreted Extensible Extensive libraries Dynamic type system

The Basics
Start the interpreter on the command line by entering python at the shell prompt. Windows users, who wish to use a GUI can start IDLE by clicking on Start Programs Python 3.0 IDLE (Python GUI). Python programs can also be written using your favourite text editor and these can be run by python filename in the console. The File extension for python files is usually .py

Numbers in python are of three types Integers, floating point and complex numbers. 1, -2, 300, 599 are examples of integer numbers.

Examples of floating point numbers are 3.23 and 52.3E-4. The E notation indicates powers of 10. In this case, 52.3E-4 means 52.3 * 10-4. Examples of complex numbers are (-5+4j) and (2.3 4.6j).

Strings can be single, double or triple quoted. Triple quoted strings can span across multiple lines. E.g. '''A quick brown fox jumps over the lazy dog and said 'hi!' ''' Concatenation is done using the usual '+' operator. Strings can be repeated using the '*' operator. E.g. 'hello '*3 'hello hello hello ' Strings are immutable.

Identifier Naming: First character of an identifier can begin with an alphabet or and underscore. Rest of the identifier may contain alphabets, underscores or digits. Identifiers names are case sensitive.

Whitespace at the beginning of a line is called indentation. In python, indentation is very essential. Indentation is used to determine the logical grouping of statements. E.g. for i in range(1,no_of_lines+1): ispaces = (no_of_lines - stars)/2 print " " * ispaces + "*" * stars stars += (-1 if(i > no_of_lines/2) else 1) * 2 The three statements above, that are at the same indentation level, are part of the for loop.

Apart from the standard operator set, python contains ** , power operator. Returns x to the power y.

//, floor division. Performs division and returns the floor on the quotient. E.g. 4 // 3.0 = 1.0

and, or and not are the boolean and, boolean or and boolean and operators respectively.

Data Structures
A list is a data structure that holds an ordered collection of items i.e. you can store a sequence of items in a list. shoplist = ['apple', 'mango', 'carrot', 'banana']

Tuples are used to hold together multiple objects. Just like strings, tuples are immutable. zoo = ('python', 'elephant', 'penguin')

Dictionaries contain key/value pairs. ab = { 'Swaroop' 'Larry' 'Matsumoto' 'Spammer' : : : : 'swaroop@swaroopch.com', 'larry@wall.org', 'matz@ruby-lang.org', 'spammer@hotmail.com'

} Dictionaries can be indexed by the key values. ab['Swaroop'] == 'swaroop@swaroopch.com' #True