3 Python 3 Lecture 1-2
3 Python 3 Lecture 1-2
- An Introduction
M. Shubhakanta Singh
Department of Physics, Manipur University
Why Python?
• Python is Object Oriented, Open source, Flexible and easy to
learn.
• It has a rich set of libraries and tools
• It has a huge community base(for queries and answers)
• Interactive, Modular, Dynamic, Portable, Extensible to C &C++
• Code readability (in fewer steps compare to Java or C++)
• Python is dynamically typed language, so the variables are
defined automatically.
Why software development companies prefer Python?
-Gaming, Web frameworks and applications, language
development, graphic design applications
• Integration Feature – Jython, Cython, etc
• Data science – Python, R, C++
• Google has made it one of its Official languages.
• Yahoo search engine
Dr. M. Shubhakanta Singh
Topics to be covered
• Python Environments : IDLE & Enthought Canopy/Spyder
• Basics of Python:
– Keywords & Identifiers
– Operators & Expressions
– Managing Input & Output operations
– Functions
– Decision making & Branching
– Decision making & Looping
• Python Sequences:
– Lists
–Tuples
• Python Dictionaries
• File managements
Dr. M. Shubhakanta Singh
Some Integrated Development
Environments (IDLEs) for Python
Environment Variable Web Site
IDLE The standard Python environment http://www.python.org/idle
__future__module
>>> -7/3
-2.3333
>>> -7//3
-3 ??? Why ???
Dr. M. Shubhakanta Singh
Important differences between Python 2.x and
Python 3.x
In Python 2.x print is a keyword
Division Operator
but it is replaced by a print()
print function function in Python 3.x
However, parentheses work in
Unicode Python 2.x if a space is added
xrange after print keyword because the
interpreter evaluates it as an
Error Handling expression.
In Python 2.x:
__future__module print “Hello world”
print (“Hello World”)
In Python 3.x:
print (“Hello World”)
Dr. M. Shubhakanta Singh
Important differences between Python 2.x and
Python 3.x
In Python 2.x: implicit str type is ASCII whereas
xrange
<class ‘str’>
<class ‘bytes’> # Bytes and str are different
Error Handling
Python 2.x also supports Unicode:
print (type(" String by Default"))
print (type(u" String by Default. "))
Division Operator
If we need to iterate over the same sequence
multiple times we prefer range() function as it
print function
provides a static list. xrange() reconstructs the
sequence every time.
The advantage of xrange() is, it saves memory
Unicode
when the task is to iterate over a large range.
In Python 3.x:
xrange
The range() function now does what xrange()
function does in Python 2.x. This helps us to
keep our code portable from Python 2.x to
__future__module
print (x)
for x in xrange(1,6):
print (x)
Output in Python 2.x:
12345
12345
Output in Python 3.x: ‘xrange’ is not defined
Dr. M. Shubhakanta Singh
Important differences between Python 2.x and
Python 3.x
Unicode
print (err, 'Error Caused')
In Python 3.x:
xrange try:
trying_to_check_error
Error Handling except NameError as err:
print (err, 'Error Caused')
__future__module
Unicode
Output:
3.5 (which is compatible with Python 3.x)
Example 1: In Python 2.x
xrange from __future__ import print_function
print('abc', 'def', '123', sep=' + ')
Error Handling
Output:
abc + def + 123 (which is compatible with
__future__module
Python 3.x)
Sequence Types
String A sequence of characters. Immutable (not ‘This is a string’
changeable in - place). It can be “This is also a string”
represented by single quotes or double
quotes.
List Mutable (changeable) sequence of data [1, 2, 3]
types. List elements may be of same or [1.2, 4, ‘c’]
different data types. [4.5, 6.7, 7.8]
Tuple An immutable sequence of data types. It (1, 2, 3)
works just like a list. (1.2, 4, ‘c’)
(4.5, 6.7, 7.8)
Dictionary A list of items indexed by keys. It has a { 1: 3, 2 : 8, 3 :10}
value corresponding to its key. {‘a’ : 11, ‘b’ : 22}
Dr. M. Shubhakanta Singh {2 : ‘d’, 5 :’k’, 6 : ‘t’}
Data Types in Python 3.x
• >>>int(12.34)/int(4.67)
3