2. Why Python?
Python is object-oriented
Structure supports such concepts as polymorphism, operation overloading, and multiple inheritance
It's free (open source)
Downloading and installing Python is free and easy
Source code is easily accessible
Free doesn't mean unsupported! Online Python community is huge
It's portable
Python runs virtually every major platform used today
As long as you have a compatible Python interpreter installed, Python programs will run in exactly
the same manner, irrespective of platform
It's powerful
Dynamic typing
Built-in types and tools
Library utilities
Third party utilities (e.g. Numeric, NumPy, SciPy)
Automatic memory management
3. Why Python? (ContdâŚ)
It's mixable
Python can be linked to components written in other languages easily
Linking to fast, compiled code is useful to computationally intensive problems
Python is good for code steering and for merging multiple programs in otherwise conflicting languages
Python/C integration is quite common
It's easy to use
Rapid turnaround: no intermediate compile and link steps as in C or C++
Python programs are compiled automatically to an intermediate form called bytecode, which the interpreter then
reads
This gives Python the development speed of an interpreter without the performance loss inherent in purely
interpreted languages
It's easy to learn
Structure and syntax are pretty intuitive and easy to grasp
4. Compiling and interpreting
ďľ Many languages require you to compile (translate) your program into a form that the machine
understands.
ďľ Python is instead interpreted into machine instructions everytime read by PVM implemented
independent from hardware.(Bytecode/.pyc)
compile execute
output
source code
Hello.java
byte code
Hello.class
interpret
output
source code
Hello.py
5. Which IDE to use?
ďľ IDLE (Integrated DeveLopment Environment) â Included with Python
download from www.python.org
ďľ Eclipse with PyDev
ďľ Pycharm (Community Edition)
ďľ Komodo Edit
ďľ âŚ. And a lot more.
6. IDLE Modes
ďľ IDLE provides two kinds of modes:
ďľ Interactive mode â Good for beginners and for testing small snippets of code
and getting an overall feel of the programming language.
ďľ Script mode â The next logical step after learning a few of the basics of the
language is creating programs/ snippets.
ďľ We will use IDLE/Spyder/Anaconda in class.
7. Lets practice:-
Lets write a program to say hello.
print is the command⌠but waitâŚ.
i) print (âHelloâ) #3.x
or
ii)print âhelloâ #2.x
8. Important differences between Python 2.x and Python
3.x with examples
ďľ Division operator
ďľ print function
ďľ Unicode
ďľ xrange
ďľ Error Handling (as keyword)
ďľ _future_ module
9. Comments
ďľ Start comments with # â the rest of line is ignored.
ďľ Can include a âdocumentation stringâ as the first
line of any new function or class that you define.
ďľ The development environment, debugger, and other
tools use it: itâs good style to include one.
âââThis is the docstring. This
function does blah blah blah.âââ
# The code would go here...
10. Keywords
ďľ They are the words used by Python interpreter to recognize the structure of
program.
As these words have specific meaning for interpreter, they cannot be
used for any other purpose.
ďľ A partial list of keywords in Python 2.7 is
11. Operators
ďľ Operators are special symbols which represents computation. They are
applied on operand(s), which can be values or variables. Same operator can
behave differently on different data types. Operators when applied on
operands form an expression.
ďľ Operators are categorized as Arithmetic, Relational, Logical and Assignment.
Value and variables when used with operator are known as operands.
19. Prerequisites:-
ďľ Concept of variable in respect of python. (not a container anymore)
ďľ Concept of mutable/immutable datatype.(modifiable/not modifiable objects)
20. Number
ďľ Number data type stores Numerical Values. This data type is immutable i.e.
value of its object cannot be changed (we will talk about this aspect later).
ďľ These are of three different types:
a) Integer & Long
b) Float/floating point
c) Complex
d) Boolean
BACK
21. None
ďľ This is special data type with single value. It is used to signify the absence of
value/false in a situation. It is represented by None
BACK
22. Sequence
ďľ A sequence is an ordered collection of items, indexed by positive integers. It
is combination of mutable and non mutable data types. Three types of
sequence data type available in Python are :
a) Strings
b) Lists
c) Tuples.
BACK
23. String
ďľ String is an ordered sequence of letters/characters. They are enclosed in
single quotes (ââ) or double quotes (ââ). The quotes are not part of string. They
only tell the computer where the string constant begins and ends. They can have
any character or sign, including space in them. These are immutable data
types. We will learn about immutable data types while dealing with third
aspect of object i.e. value of object.
Example
>>> a = 'Ram'
ďľ A string with length 1 represents a character in Python. BACK
24. List
ďľ List is also a sequence of values of any type. Values in the list are called
elements / items. These are mutable and indexed/ordered. List is enclosed in
square brackets.
Example
l = [âspam , 20.5,5]
â
BACK
25. Tuples
ďľ Tuples are a sequence of values of any type, and are indexed by
integers. They are immutable. Tuples are enclosed in ().
BACK
26. Sets
ďľ Set is an unordered collection of values, of any type, with no duplicate entry.
Sets are immutable.
Example
s = set ([1,2,34])
BACK
27. Mapping and Dictionaries
ďľ Mapping
This data type is unordered and mutable. Dictionaries fall under
Mappings.
Dictionaries: Can store any number of python objects. What they store is a
key â value pairs, which are accessed using key. Dictionary is enclosed in
curly brackets.
Example
d = {1:'a',2:'b',3:'c'}