1 1pythonworld PDF
1 1pythonworld PDF
• Software Engineer
• Python Developer
• Research Analyst
• Data Analyst
• Data Scientist
• Software Developer
Python Salary
payscale.com
TIOBE Index for Aug 2018 – Python
Career
Python in 4 th place for programming community index
Basic Python Interview Questions
LIST TUPLES
Lists are mutable i.e they can be Tuples are immutable (tuples are
edited. lists which can’t be edited).
Lists are slower than tuples. Tuples are faster than list.
• S = 10
• print(S)
Data strucures in Python:
Data Types
Numeric:
A = 10
# Convert it into float type
B = float(A)
print(B)
A = 10.76
# Convert it into float type
B = int(A)
print(B)
List:
Subjects = ['Physics', 'Chemistry', 'Maths', 2]
print(Subjects)
Tuples:
Strings:
S = "Welcome To Saveetah"
D = ' Saveetah ‘
c=a +b
print ( c )
c=a -b
print ( c )
c=a *b
print ( c )
c=a /b
print ( c )
c=a %b
print ( c )
a=2
b=3
c = a**b
print ( c )
if ( a == b ):
print ("a is equal to b")
else:
print ("a is not equal to b")
if ( a != b ):
print ("a is not equal to b")
else:
print ("a is equal to b")
if ( a > b ):
print ("a is greater than b")
else:
print ("a is not greater than b")
cont.....
a=5
b = 20
if ( a <= b ): print ("a is either less than or equal to b")
else: print ("a is neither less than nor equal to b")
if ( a => b ):
print ("a is either greater than or equal to b")
else:
print ("a is neither greater than nor equal to b")
Output = a is not equal to b a is not equal to b a is not less than b a
is greater than b a is either less than or equal to b b is either
greater than or equal to b
Assignment Operators:
a = 21
b = 10
c=0
c=a +b
print ( c )
c += a
print ( c )
c *= a
print ( c )
c /= a
print ( c )
c =2
c %= a
print ( c )
c **= a
print ( c )
Output = 31, 52, 1092, 52.0, 2, 2097152, 99864
Bitwise Operators:
a = 58 # 111010
b = 13 # 1101
c=0
c=a &b
print ( c ) # 8 = 1000
c=a |b
print ( c ) # 63 = 111111
c=a ^b
print ( c ) # 55 = 110111
c = a >> 2
print ( c ) # 232 = 11101000
c = a << 2
print ( c ) # 14 = 1110
Output = 8,63,55,232,14
Logical Operators:
x = True
y = False
print('x or y is',x or y)
print('not x is',not x)
Output = x and y is False x or y is True not x is False
Membership Operators:
X = [1, 2, 3, 4]
A=3
print(A in X)
print(A not in X)
Output = True False
Identity Operators:
X1 = 'Welcome To saveetha'
X2 = 1234
Y1 = 'Welcome To saveetha'
Y2 = 1234
print(X1 is Y1)
print(X1 is X2)
Output = True False True False
Conditional Statements:
Example
X = 10
Y = 12
f = open (“saveetha.txt")
f.close()
Python world
CSE2602/CSE2603
Python programming for Data Analytics
3 Credits
Book2(Unit1-4): Fabio Nelli “Python Data Analytics Data Analysis and Science
Using Pandas, matplotlib, and the Python Programming Language” Apress
Media LLC, 233 Spring Street, New York, 2015.
• Features of Python
• Shell mode
• Interactive mode
• Compare C, Java and Python
• Code
• Advantages of python
Features
• created by Guido Von Rossum in 1991
• starting with the previous language called ABC.
series of features:
• interpreted
• portable
• object-oriented
• interactive
• interfaced
• open-source
• easy to understand and use
features
• Python is a programming language interpreted, that is pseudo-
compiled.
• code of a program, needs an interpreter.
• The interpreter is a program that has the task of interpreting the
source code and run it. Therefore unlike language such as C, C ++,
and Java, there is no compile time.
• Python is a highly portable programming language. The decision
to use an interpreter as an interface for reading and running the
code has a key advantage: portability.
• In fact, you can install on any existing platform (Linux, Windows,
Mac) an interpreter specifically adapted to it while the Python
code to be interpreted will remain unchanged.
• Python also, for this aspect, was chosen as the programming
language for many small-form devices, such as the Raspberry Pi
and other microcontrollers.
oops
• Python is an object-oriented programming
language. In fact, it allows you to specify
classes of objects and implement their
inheritance.
• But unlike C ++ and Java there are no
constructors or destructors.
Interactive
• launch the program, or you can enter a command
line at once and execute it, immediately getting
the results of the command, and depending on
them you can decide what will be the next line of
command to be run.
• This highly interactive mode to execute code
makes Python a computing environment
perfectly similar to Matlab.
• This is a feature of Python that brought the
success of this programming language in the
scientific community.
Interfaced
• Python is a programming language that can
be interfaced.
• This programming language has among its
features the characteristic to be interfaced
with code written in other programming
languages such as C / C ++ and Fortran.
Open source and simple
• Python is an open-source programming language.
CPython, which is the reference implementation of
the Python language is completely free and open-
source.
• Additionally every module or library in the network is
open-source and their code is available online.
• Finally, Python is a simple language to use and learn.
This aspect is perhaps the most important of all
because it is the most direct aspect which a
developers facing difficulties in existing programming
languages due to their complexity.
Modes of python execution
• Shell mode-Interactive
Each time you run the python command the
Python interpreter starts, characterized by a
>>> prompt.
• The Python interpreter is simply a program
that reads and interprets the commands
passed to the prompt.
• Script mode
Entire code in a file can be executed at a time
comparison
➢ Java pgm to print helloworld ➢ C pgm to print helloworld
Public class hello{ #include<stdio.h>
Public static void main(String[] args) Void main()
{ {
System.out.println(“hello world”); printf(“hello world”);
}} }
# Splits at space
print(text.split())
word = ‘apple,orange,mango'
# Splits at ','
print(word.split(', '))
word = ‘name:age:dob'
# Splitting at ':'
print(word.split(':'))
word = 'CatBatSatFatOr'
# Splitting at 3
print([word[i:i+3] for i in range(0, len(word), 3)])
output
• Jython
• PyPy
Cython
• The project Cython is based on creating a compiler
that translates Python code into C code equivalent.
• This code is then executed within a Cython
environment at runtime.
• This type of compilation system has made possible
the introduction of C semantics within the Python
code to make it even more efficient.
• This system has led to the merging of two worlds of
programming language with the birth of Cython that
can be considered a new programming language.
• URL is http://docs.cython.org
Jython
• In parallel to Cython, there is a version totally
built and compiled in Java, named Jython.
• It was created by Jim Hugunin in 1997
• Jython is a version of implementation of the
Python programming language in Java;
• it is further characterized by using Java
classes instead of Python modules to
implement extensions and packages of
Python.
• (http://www.jython.org).
PyPy
• The PyPy interpreter is a JIT (just-in-time)
compiler, which converts the Python code
directly in machine code at runtime.
• This choice was made to speed up the
execution of Python.
• However, this choice has led to the use of a
small subset of Python commands, defined as
RPython.
• http://pypy.org.
Summary
• Open source
• Simple
• Easy to learn
python2 vs python 3
why version 2.x is still being released if it is distributed
around a much more enhanced version such as 3.x?
• Guido Van Rossum decided to bring significant
changes to the Python language, but he soon found
that these changes would make new Python
incompatible with a lot of existing code. Thus he
decided to start with a new version of Python called
Python 3.0.
• To overcome the problem of compatibility and create
huge amounts of code unusable spread to the
network, it was decided to maintain a compatible
version, 2.7 to be precise.
Compare python 2 & 3
Example 1: Array of bytes from a
string-Bytearray()
• The bytearray() method returns a bytearray
object which is an array of the given bytes.
string = "Python is interesting.“
# string with encoding 'utf-8‘
arr = bytearray(string, 'utf-8')
print(arr)
Output:
bytearray(b'\x01\x02\x03\x04\x05')
Bytes()
• The bytes() method returns a immutable bytes object initialized
with the given size and data.
• The syntax of bytes() method is:
bytes([source[, encoding[, errors]]])
The bytes() method returns a bytes object which is an immmutable
(cannot be modified) sequence of integers in the range 0 <=x <
256.
bytes() Parameters
The bytes() takes three optional parameters:
• source (Optional) - source to initialize the array of bytes. And it
can be string,integer,iterable objects like list,tuple etc
• encoding (Optional) - if source is a string, the encoding of the
string.
• errors (Optional) - if source is a string, the action to take when
the encoding conversion fails (Read more: String encoding)
Example 1: Convert string to bytes
string = "Python is interesting.“
# string with encoding 'utf-8‘
arr = bytes(string, 'utf-8')
print(arr)
Output:
b'Python is interesting.'
Ex: unordered types
Python cannot order str values and int values
together.
Ex:
x=0
time=input(“enter year”)
while (x < time):
statements
TypeError: unorderable types: int() < str()
To fix this, simply use int( input(“enter year”)) to
convert your string to an integer
Important differences
Python 2.x Vs Python 3.x
• Division operator
• Print function
• Unicode
• Xrange
• Error handling
Important differences
Python 2.x Vs Python 3.x
• Division operator
In python 3.x
Ex:print 7 / 5 output is 1.4
print -7 / 5 output is -1.4
But in python 2.x ,the same code will output as 1 and -2
5/2=2 in python2 where as 5/2=2.5 in python 3
• Print function
The print function in Python 2.x is replaced by print() function in Python 3.x.i.ePython
3.x need an extra pair of parenthesis.
• Unicode
In Python 2, implicit str type is ASCII(8 bit). But in Python 3.x implicit str type is Unicode.
(16 bit)
• Xrange
Python 3.x’s range function is xrange from Python 2.x.Python 3 gives name error for
xrange.
• Error handling
In python 3.x, ‘as’ keyword is required in except Nameerror as err
Instead python 2 uses except Nameerror, err
Installing Python
On Debian-Ubuntu Linux systems
$ apt-get install python
(or)
$ sudo apt-get update
$ sudo apt-get install python3.7
Python2
Print “hello world”
Python3
Print(“hello world”)
Python Interpreter Interactively
Windows
• Start menu and select All Programs-> IDLE
(Python 3.x 32-bit)
• Click on the icon to start IDLE.
import random
#input number
n=int(raw_input("enter a number"))
if n>99:
print n,"is anumber too high"
elif n<=1:
print n," is a number too low"
else:
print n,"is a correct number"
print "the random number generated for you is ",random.randrange(1,n)
Python3 pgm
# Program to generate a random number between 1 and 99
# import the random module
import random
n=int(input("enter a number"))
if n>99:
print (n,"is anumber too high“)
elif n<=1:
print (n," is a number too low“)
else:
print (n,"is a correct number“)
def ishashad(a):
n=a
d1=n%10
d2=n/10
div=d1+d2
if a%div==0:
print "number is harshad "
else:
print "number is not harshad"
f()
ans
a
d
3. What is output?
try:
print "a"
raise Exception("doom")
except:
print "b"
else:
print "c"
finally:
print "d"
ans
a
b
d
Escape sequences
• >>> print("a\tb")
a b
• >>> print("a\nb")
a
b
• >>> print("a\\tb")
Print multiple lines with tripple quote
• >>> print("""This is a string
that spans across
several lines""")
This is a string
that spans across
several lines
Type() function
• >>> type(22)
<class ‘int'>
• >>> type(4.2)
<class ‘float'>
Other hints
• specify the string literal hi‘john in Python
‘hi\’john
OR
“””hi’john”””