Top Banner
Introduction to Python Abhinav Upadhyay <[email protected]>
25

Python intro

May 19, 2015

Download

Technology

A rough presentation on the basics of Python, which I gave to my team at SocialTwist.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Python intro

Introduction to Python

Abhinav Upadhyay <[email protected]>

Page 2: Python intro

What is Python

● A dynamically typed, object oriented programmnig language

● Created by Guido van Rossum in 1989

Page 3: Python intro

Features

● High-level● Object Oriented● Scalable● Extensible● Portable● Readable● Managed runtime● Interpreted and Byte-compiled.

Page 4: Python intro

Difference from C Style Languages

● Forget ; and {}● Embrace indentation

Page 5: Python intro

Getting Started

● “Hello, World” in Python:

print 'Hello, world!'

Page 6: Python intro

Getting Started

● Redirecting your output

file = open('output', 'w')

print >> file, 'Hello, world!'

Page 7: Python intro

Making your program interactive

● Getting user input:

name = raw_input('Enter your name: ')

print name

Page 8: Python intro

Comments in Python

● Single line comments --> #● Multiline comments --> ''' your comment '''● Docstrings

Page 9: Python intro

Operators

● Numerical operators: +, -, /, //, *, **, %

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

● Logical operators: and, or, not

Page 10: Python intro

Numerical types in Python

● int– long

– float

● bool

● complex

Page 11: Python intro

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'''

Page 12: Python intro

String handling in Python

● String operators:+ --> Performs concatenation

str1 = 'a'str2 = 'b'print a + bOutput: 'ab'

* --> Performs repititionstr1 = 'a'print a * 5Output: 'aaaaa'

Page 13: Python intro

String handling in Python

● String operators:

% --> Format operatorage = 20str1 = 'Your age is %d\n' % ageOutput: 'Your age is 20'

Page 14: Python intro

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'

Page 15: Python intro

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

Page 16: Python intro

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()

Page 17: Python intro

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.

Page 18: Python intro

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

Page 19: Python intro

Common Python Data-Structures

● Dictionaries:– Hash tables

– Can store any number of objects

– {} are used for creating dictionaries

Page 20: Python intro

Loops in Python

● For loop:

Used for iterating over a sequence of items

for item in list:

print item

Page 21: Python intro

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

Page 22: Python intro

Conditionals

● If-elif-elseif <condition>:

#statement1

#statement2

elif <condition 2>:

#statement3

#statement4

else:

pass

Page 23: Python intro

File handling

F = open('file1.txt', 'r')

for eachLine in F:print F

F.close()

f = open('file2.txt', 'w')

f.write('hello, world\n')

f.close()

Page 24: Python intro

Functions in Python

● The 'def' keyword● The return value● Returning multiple values

Page 25: Python intro