Python Study Material

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

Python

Agenda
Why Python?  Installing
What is Python?  Basic and String operations
 Basic Programming
History  Control flow: if
Features  Loops: break, continue, else
 Lists, Tuples, Dictionaries
Uses  Modules
Comparison to Other  Classes & Notes on classes
Technologies  Inheritance
Structure of Python Programming  Outcome of Python Learning
Why Python ?
• Designed to be easy to learn.
• Clean & Clear syntax.
• Very few key & few keywords.
• Not hard to read, write and maintain
• Power & Rapid Development
• Increases productivity.
• Finally, it is Platform Independent.
What is Python ?

• It is an Interpreted Language.
• Object-oriented & high-level programming language
• It is dynamic typing and dynamic binding, make it very attractive for
Rapid Application Development.
• It is Syntax free Language.
History

• It is a popular programming language.


• It was created by Guido van Rossum.
• It was founded in 1989.
• It was released in 1991.
• Python is originated from the famous movie name called Monty Python
Flying Circus.
Notorious Features
• Less Data types, but High-Level
Performance
 Automatic memory
• Leads to fast coding management
(First language in many development circle).  Simpler, shorter, more flexible
 Object-oriented programming
• Uses white-space to delimit blocks.
 Fewer restrictions and rules
• Variables do not need declarations.  Interactive, dynamic in nature
• No compiling or linking
What is it used for?
• Steering Scientific Applications, Database Applications, GUI applications.
• Web Application Development, Software Development.
• Education.
• Business Applications  to build ERP and e-Commerce systems.
• Used alongside software to create workflows
• Used on a server to create web applications.
• Robotics
Python Compared to other Languages
Tcl Perl Python JavaScript Visual
Basic
Speed development     
regexp   
breadth extensible   
embeddable  
easy GUI   (Tk) 
net/web     
enterprise cross-platform    
I18N    
thread-safe   
database access     
Structure of Python Programming
 Import Modules/Packages
 Class
 Variables
 Functions
 Operators
 Control Structures
 Objects
Installing
Installing Python is generally easy, and nowadays many Linux and UNIX
distributions include a recent Python. Even some Windows computers
(notably those from HP) now come with Python already installed. If you do
need to install Python and aren't confident about the task you can find a few
notes on the Beginners Guide / Download wiki page, but installation is
unremarkable on most platforms.
Basic Operations
• Assignment:
• size = 40 • Strings
• a=b =c=3 • 'hello world', 'it\'s hot'
• "bye world"
• Numbers
• continuation via \ or use """ long text """"
• integer, float
• complex numbers: 1j+3,
abs(z)
String Operations
• concatenate with + or neighbors
• word = 'Help' + x
• word = 'Help' 'a'
• subscripting of strings
• 'Hello'[2]  'l'
• slice: 'Hello'[1:2]  'el'
• word[-1]  last character
• len(word)  5
• immutable: cannot assign to subscript
Basic Programming
a,b = 0, 1
# non-zero = true
while b < 10:
# formatted output, without \n
print b,
# multiple assignment
a,b = b, a+b
Control flow: if
x = int(raw_input("Please enter #:"))
if x < 0:
x=0
print 'Negative changed to zero'
elif x == 0:
print 'Zero'
elif x == 1:
print 'Single'
else:
print 'More'
• no case statement
Loops: break, continue, else
• break and continue
• else after loop exhaustion for n in range(2,10):
for x in range(2,n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is prime'
Lists
• lists can be heterogeneous
• a = ['spam', 'eggs', 100, 1234, • Lists can be manipulated
2*2] • a[2] = a[2] + 23
• a[0:2] = [1,12]
• Lists can be indexed and sliced:
• a[0:0] = []
• a[0]  spam • len(a)  5
• a[:2]  ['spam', 'eggs']
List Methods
• append(x)
• index(x)
• extend(L)
• return the index for value x
• append all items in list • count(x)
• insert(i,x) • how many times x appears in list
• remove(x) • sort()
• pop([i]), pop() • sort items in place
• reverse()
• create stack (FIFO), or queue (LIFO)  pop(0)
• reverse list
Tuples and Sequences
• lists, strings, tuples: examples of sequence type
• tuple = values separated by commas
>>> t = 123, 543, 'bar'
>>> t[0]
123
>>> t
(123, 543, 'bar')
Tuples
• Tuples may be nested
>>> u = t, (1,2)
>>> u
((123, 542, 'bar'), (1,2))
• kind of like structs, but no element names:
• (x,y) coordinates
• database records
• like strings, immutable  can't assign to individual items
Tuples
• Empty tuples: ( )
>>> empty = ( )
>>> len(empty)
0
• one item  trailing comma
>>> singleton = 'foo',
Tuples
• sequence unpacking  distribute elements across variables
>>> t = 123, 543, 'bar'
>>> x, y, z = t
>>> x
123
• packing always creates tuple
• unpacking works for any sequence
Dictionaries
• indexed by keys
• keys are any immutable type: e.g., tuples
• but not lists (mutable!)
• uses 'key: value' notation
>>> tel = {'hgs' : 7042, 'lennox': 7018}
>>> tel['cs'] = 7000
>>> tel
Dictionaries
• no particular order
• delete elements with del
>>> del tel['foo']
• keys( ) method  unsorted list of keys
>>> tel.keys()
['cs', 'lennox', 'hgs']
• use has_key( ) to check for existence
>>> tel.has_key('foo')
0
Modules
• collection of functions and variables, typically in scripts
• definitions can be imported
• file name is module name + .py
• e.g., create module fibo.py
def fib(n): # write Fib. series up to n
...
def fib2(n): # return Fib. series up to n
Modules
• import module:
import fibo
• Use modules via "name space":
>>> fibo.fib(1000)
>>> fibo.__name__
'fibo'
• can give it a local name:
>>> fib = fibo.fib
>>> fib(500)
Modules
• function definition + executable statements
• executed only when module is imported
• modules have private symbol tables
• avoids name clash for global variables
• accessible as module.globalname
• can import into name space:
>>> from fibo import fib, fib2
>>> fib(500)
• can import all names defined by module:
>>> from fibo import *
Classes
• mixture of C++ and Modula-3
• multiple base classes
• derived class can override any methods of its base class(es)
• method can call the method of a base class with the same name
• objects have private data
• C++ terms:
• all class members are public
• all member functions are virtual
• no constructors or destructors (not needed)
Classes
• classes (and data types) are objects
• built-in types cannot be used as base classes by user
• arithmetic operators, subscripting can be redefined for class instances (like C++, unlike Java)

Class definitions
Class ClassName:
<statement-1>
...
<statement-N>
• must be executed
• can be executed conditionally
creates new namespace
Notes on classes
• Data attributes override method attributes with the same name
• no real hiding  not usable to implement pure abstract data types
• clients (users) of an object can add data attributes
• first argument of method usually called self
• Self is a default object receiver.
Inheritance
class DerivedClassName(BaseClassName)
<statement-1>
...
<statement-N>
• search class attribute, descending chain of base classes
• may override methods in the base class
• call directly via BaseClassName.method
Multiple Inheritance

class DerivedClass(Base1,Base2,Base3):
<statement>
• depth-first, left-to-right
• problem: class derived from two classes with a common base class
Outcome of Python Learning
1. Python Developer
Becoming a Python developer is the most direct job out there for someone who knows the Python
programming language.
A Python developer can be expected to:
 Build websites and Optimize data algorithms A wide array of companies are looking for
 Solve data analytics problems Python developers. Learn Python today, and
 Implementing security and data protection tomorrow you could end up as a Python
developer working at a startup or larger
 Writing reusable, testable and efficient code company
Outcome of Python Learning

2. Product Manager
 Product managers are responsible for researching new user features,
find gaps in the market, and make an argument for why certain
products should be built.
 Data plays a huge role in their work, so many companies are now
seeking product managers who know Python.
Outcome of Python Learning

3. Data Analyst
 Does the idea of finding meaning in large amounts of information
appeal to you?
 Many companies are looking for someone who can sift through large
sets of data — and a popular way to accomplish that is using Python
libraries such as SciPy and Pandas.
Outcome of Python Learning
4. Machine learning engineer:
 This position have increased by more than 330% in the last couple of
years. If you are skilled in python, you will be given preference over
other candidates.
 A machine learning engineer builds and trains machines, programs,
and other computer-based systems to apply their learned knowledge
for making predictions. Python’s ability to work with data automation
and algorithms makes it the ideal programming language that can be
used in machine learning.
Outcome of Python Learning
5. Others
 Web Testing
 Data Science
 AI
 Web Scripter
 Quality Assurance Engineer
 GIS Analyst
 Data Scientist

You might also like