Introduction to Python Outline Python IS History Installation

  • Slides: 13
Download presentation
Introduction to Python

Introduction to Python

Outline Python IS ……. History Installation Data Structures Flow of Control Functions Modules References

Outline Python IS ……. History Installation Data Structures Flow of Control Functions Modules References

 Python IS …. . Interpreted Like pseudo code, hence development-friendly Intuitive, easy to

Python IS …. . Interpreted Like pseudo code, hence development-friendly Intuitive, easy to learn, understandable White-space sensitive; acts as delimiter Portable Extensive standard libraries Compatible with other languages It is OPEN source

 History Invented in 1990 by Guido Van Rossum • The name ‘Python’ stems

History Invented in 1990 by Guido Van Rossum • The name ‘Python’ stems from "Monty Python's Flying Circus“ Currently, he works for Google (since 2005). He wrote a web based code review tool for Google in Python.

 Installation Available for FREE online IDLE is a known editor $cmd> python >>

Installation Available for FREE online IDLE is a known editor $cmd> python >> print “hello” hello >> import “filename” …. // opens the console

 Data Structures Variables - need not be declared String - comes with usual

Data Structures Variables - need not be declared String - comes with usual string manipulations “hello” + “ 123” – gives “hello 123” // Concat “Hello”*3 – gives “Hello” // Repetition len(“array_name”) – gives 10 Lists list = [‘a’, ’b’, ’c’] Dynamic array Items can be of any type Indexed based print list[0] // prints ‘a’ Operations such as append(), insert(), pop(), reverse(), sort()

 Dictionary Map of keys and values E. g. dictionary = {“key 1: value

Dictionary Map of keys and values E. g. dictionary = {“key 1: value 1”, “key 2: value 2”, …. . } Operations such as insert, delete, has_key(), keys(), values(), …. Example >> Polygons = {“triangle: 3”, “rectangle: 4”, “pentagon: 5”} >> print Polygons[“triangle”] 3

 References Address to the object referred Pointer_a = Pointer_b // doesn’t copy value

References Address to the object referred Pointer_a = Pointer_b // doesn’t copy value Both now point to same object >> Pointer_a = [1, 2] >> Pointer_b = Pointer_a >> Pointer_a. append(3) >> print b [1, 2, 3]

 Tuples Consists of values separated by commas Nesting allowed E. g. >> tuple

Tuples Consists of values separated by commas Nesting allowed E. g. >> tuple = “ 12”, “abc”, 890 >> tuple[0] 12 T = ((“ 1”, “abc”, 1234), (“ 2”, “pqr”, 5678)) Acts like a DB Tuples are immutable x, y, z = T // unpacking Special case for tuple with 0 or 1 items >> empty = () // 0 items >> single = (‘ 123’, ) // trailing comma

 Flow of Control (no braces) If (condition): statement 1 statement 2 else: statement

Flow of Control (no braces) If (condition): statement 1 statement 2 else: statement 1 while(condition): statement for var in seq: statement // Mark the indentation // Grouping

 break Continue Functions def name(arg 1, arg 2, …. ): statement… return //

break Continue Functions def name(arg 1, arg 2, …. ): statement… return // optional Example: def sum(num 1, num 2): return (num 1 + num 2)

 Modules/Scripts: File containing statements and functions Comes with “. py” suffix Invoked by

Modules/Scripts: File containing statements and functions Comes with “. py” suffix Invoked by “import filename” Object-oriented

 References: Python Homepage • http: //www. python. org Python Tutorial • http: //www.

References: Python Homepage • http: //www. python. org Python Tutorial • http: //www. python. org/tut Python Documentation • http: //www. python. org/doc