KEMBAR78
Python intro | PDF
Introduction to Python


                 Abhinav Upadhyay
           <abhinav@socialtwist.com>
What is Python
●   A dynamically typed, object oriented
    programmnig language
●   Created by Guido van Rossum in 1989
Features
●   High-level
●   Object Oriented
●   Scalable
●   Extensible
●   Portable
●   Readable
●   Managed runtime
●   Interpreted and Byte-compiled.
Difference from C Style Languages
●   Forget ; and {}
●   Embrace indentation
Getting Started
●   “Hello, World” in Python:

        print 'Hello, world!'
Getting Started
●   Redirecting your output

        file = open('output', 'w')
        print >> file, 'Hello, world!'
Making your program interactive
●   Getting user input:

        name = raw_input('Enter your name: ')
        print name
Comments in Python
●   Single line comments --> #
●   Multiline comments --> ''' your comment '''
●   Docstrings
Operators
●   Numerical operators: +, -, /, //, *, **, %

●   Comparision operators: <, >, <=, >=, ==, !=, <>

●   Logical operators: and, or, not
Numerical types in Python
●   int
    –     long
    –     float


●   bool

●   complex
String handling in Python
●   Creating a string:

         my_string = “this is a string”

         my_2nd_string = 'string using single
         quotes'

         my_3rd_string = ''' triple quotes also
         work'''
String handling in Python
●   String operators:
        + --> Performs concatenation
              str1 = 'a'
              str2 = 'b'
              print a + b
              Output: 'ab'
        * --> Performs repitition
              str1 = 'a'
              print a * 5
              Output: 'aaaaa'
String handling in Python
●   String operators:

        % --> Format operator
              age = 20
              str1 = 'Your age is %dn' % age
              Output: 'Your age is 20'
String handling in Python
●   String Manipulation:
    –   Indices starting from 0
    –   Indices from last element starting with -1

          str1 = 'hello, world'
          print str1[0]
          Output: 'h'
          print str[-1]
          Output: 'd'
          Print str[-2]
          Output: 'l'
String handling in Python
Slicing:
  ●   Use the slicing operator [] to slice the string into smaller
      parts
  ●   Syntax: str[m:n]
       –   This will print the characters of the string starting from the mth position till
           the nth position (but excluding the nth character)

           str1 = '0123456789'
           print str1[1:4]
           Output: 123
Built-in functions of Python
●   Functions which are built-in to the Python
    interpreter and are always available
●   Examples: pow(), print(), open(), int(), str(),
    etc.

●   Some useful BIFs:
    –   type(), help(), dir()
Common Python Data-Structures
●   Lists:
    –   Array like data-structure
    –   Objects stored in sequential order
    –   Indices starting from 0
    –   Can store arbitrary number of objects (unlike fixed
        length arrays in other languages)
    –   Can also store objects of different types in the
        same list.
Common Python Data-Structures
●   Tuples:
    –   Similar to lists, with two visible differences:
         ●   Lists use [], while tuples use () in their syntax
         ●   Tuples are immutable data-structures
Common Python Data-Structures
●   Dictionaries:
    –   Hash tables
    –   Can store any number of objects
    –   {} are used for creating dictionaries
Loops in Python
●   For loop:
      Used for iterating over a sequence of items
      for item in list:
        print item
Loops in Python
●   While loop:
      Used for executing a suite of code a number
      of times.
      Count = 0
      while count < 10:
        print count
        count += 1
Conditionals
●   If-elif-else
      if <condition>:
         #statement1
         #statement2
      elif <condition 2>:
         #statement3
         #statement4
      else:
         pass
File handling

F = open('file1.txt', 'r')
for eachLine in F:
  print F
F.close()

f = open('file2.txt', 'w')
f.write('hello, worldn')
f.close()
Functions in Python
●   The 'def' keyword
●   The return value
●   Returning multiple values
Python intro

Python intro

  • 1.
    Introduction to Python Abhinav Upadhyay <abhinav@socialtwist.com>
  • 2.
    What is Python ● A dynamically typed, object oriented programmnig language ● Created by Guido van Rossum in 1989
  • 3.
    Features ● High-level ● Object Oriented ● Scalable ● Extensible ● Portable ● Readable ● Managed runtime ● Interpreted and Byte-compiled.
  • 4.
    Difference from CStyle Languages ● Forget ; and {} ● Embrace indentation
  • 5.
    Getting Started ● “Hello, World” in Python: print 'Hello, world!'
  • 6.
    Getting Started ● Redirecting your output file = open('output', 'w') print >> file, 'Hello, world!'
  • 7.
    Making your programinteractive ● Getting user input: name = raw_input('Enter your name: ') print name
  • 8.
    Comments in Python ● Single line comments --> # ● Multiline comments --> ''' your comment ''' ● Docstrings
  • 9.
    Operators ● Numerical operators: +, -, /, //, *, **, % ● Comparision operators: <, >, <=, >=, ==, !=, <> ● Logical operators: and, or, not
  • 10.
    Numerical types inPython ● int – long – float ● bool ● complex
  • 11.
    String handling inPython ● Creating a string: my_string = “this is a string” my_2nd_string = 'string using single quotes' my_3rd_string = ''' triple quotes also work'''
  • 12.
    String handling inPython ● String operators: + --> Performs concatenation str1 = 'a' str2 = 'b' print a + b Output: 'ab' * --> Performs repitition str1 = 'a' print a * 5 Output: 'aaaaa'
  • 13.
    String handling inPython ● String operators: % --> Format operator age = 20 str1 = 'Your age is %dn' % age Output: 'Your age is 20'
  • 14.
    String handling inPython ● String Manipulation: – Indices starting from 0 – Indices from last element starting with -1 str1 = 'hello, world' print str1[0] Output: 'h' print str[-1] Output: 'd' Print str[-2] Output: 'l'
  • 15.
    String handling inPython Slicing: ● Use the slicing operator [] to slice the string into smaller parts ● Syntax: str[m:n] – This will print the characters of the string starting from the mth position till the nth position (but excluding the nth character) str1 = '0123456789' print str1[1:4] Output: 123
  • 16.
    Built-in functions ofPython ● Functions which are built-in to the Python interpreter and are always available ● Examples: pow(), print(), open(), int(), str(), etc. ● Some useful BIFs: – type(), help(), dir()
  • 17.
    Common Python Data-Structures ● Lists: – Array like data-structure – Objects stored in sequential order – Indices starting from 0 – Can store arbitrary number of objects (unlike fixed length arrays in other languages) – Can also store objects of different types in the same list.
  • 18.
    Common Python Data-Structures ● Tuples: – Similar to lists, with two visible differences: ● Lists use [], while tuples use () in their syntax ● Tuples are immutable data-structures
  • 19.
    Common Python Data-Structures ● Dictionaries: – Hash tables – Can store any number of objects – {} are used for creating dictionaries
  • 20.
    Loops in Python ● For loop: Used for iterating over a sequence of items for item in list: print item
  • 21.
    Loops in Python ● While loop: Used for executing a suite of code a number of times. Count = 0 while count < 10: print count count += 1
  • 22.
    Conditionals ● If-elif-else if <condition>: #statement1 #statement2 elif <condition 2>: #statement3 #statement4 else: pass
  • 23.
    File handling F =open('file1.txt', 'r') for eachLine in F: print F F.close() f = open('file2.txt', 'w') f.write('hello, worldn') f.close()
  • 24.
    Functions in Python ● The 'def' keyword ● The return value ● Returning multiple values