Nothing Special   »   [go: up one dir, main page]

Python: By: Deepak Malusare

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

Python

By : Deepak Malusare
Introduction
• Python is a general purpose high level programming
language.
 
• Python was developed by Guido Van Rossam in 1989 while
working at National Research Institute at Netherlands.

• But officially Python was made available to public in 1991.


The official Date of Birth for Python is : Feb 20th 1991.

• Python is recommended as first programming language for


beginners.
Eg1: Program To print Helloworld:
print("Hello World")

Eg2: To print the sum of 2 numbers


1) a=10
2) b=20
3) print("The Sum:",(a+b))
The name Python
• The name Python was selected from the TV
Show
"The Complete
Monty Python's
Circus",
which was broadcasted in BBC from 1969 to 1974.
Python language
Guido developed Python language by taking almost
all programming features from different languages

• Functional Programming Features from C


• Object Oriented Programming Features from C++
• Scripting Language Features from Perl and Shell
Script
• Modular Programming Features from Modula-3
• Most of syntax in Python Derived from C and ABC
languages.
Where we can use Python ???
• We can use everywhere. The most common important
application areas are
•  
– For developing Desktop Applications
– For developing web Applications
– For developing database Applications
– For Network Programming
– For developing games
– For Data Analysis Applications
– For Machine Learning
– For developing Artificial Intelligence Applications
– For IOT
– …. And Many more
Note:
• Internally Google and Youtube use Python
coding
• NASA and Newyork Stock Exchange Applications
developed by Python.
• Top Software companies like Google, Microsoft,
IBM, Yahoo using Python.
Features of Python:
1. Simple and easy to learn:

• Python is a simple programming language. When we


read Python program, we can feel like reading English
statements.
• The syntaxes are very simple and only 30+ keywords are
available.
• When compared with other languages, we can write
programs with very less number of lines. Hence more
readability and simplicity.
• We can reduce development and cost of the project.
Features of Python:
2. Freeware and Open Source:

• We can use Python software without any


license and it is freeware.
• Its source code is open, so that we can we
can customize based on our requirement.
Eg: Jython is customized version of Python
to work with Java Applications.
Features of Python:
3. High Level Programming language:

• Python is high level programming


language and hence it is programmer
friendly language. Being a programmer
we are not required to concentrate low
level activities like memory management
and security etc..
Features of Python:
4. Platform Independent:

• Once we write a Python program, it


can run on any platform without
rewriting once again. Internally PVM
(Python Virtual Machine) is responsible
to convert into machine
understandable form.
Features of Python:
5. Portability
• Python programs are portable. i.e. we
can migrate from one platform to
another platform very easily. Python
programs will provide same results on
any platform.
Features of Python:
6. Dynamically Typed:
• In Python we are not required to declare type for
variables. Whenever we are assigning the value,
based on value, type will be allocated automatically.
Hence Python is considered as dynamically typed
language.
• But Java, C etc are Statically Typed Languages because
we have to provide type at the beginning only.
• This dynamic typing nature will provide more
flexibility to the programmer.
Features of Python:
7. Both Procedure Oriented and Object
Oriented:

• Python language supports both Procedure


oriented (like C, Pascal etc) and object oriented
(like C++,Java) features. Hence we can get
benefits of both like security and reusability etc
Features of Python:
8. Interpreted

• We are not required to compile Python


programs explicitly. Internally Python
interpreter will take care that compilation.
• If compilation fails interpreter raised syntax
errors. Once compilation success then PVM
(Python Virtual Machine) is responsible to
execute.
Features of Python:
9. Extensible

• We can use other language programs in Python.


The main advantages of this approach are:
– We can use already existing legacy non-
Python code
– We can improve performance of the
application
Features of Python:
10. Embedded

• We can use Python programs in any other


language programs.
• i.e we can embedd Python programs anywhere.
Features of Python:
11. Extensive Library:

• Python has a rich inbuilt library.


• Being a programmer we can use this library
directly and we are not responsible to
implement the functionality.

• etc...
Limitations of Python:
• Performance wise not up to the
mark because it is interpreted
language.
• Not using for mobile Applications
Flavors of Python:
•  CPython:
It is the standard flavor of Python. It can be used to work with C language
Applications
• Jython or JPython:
It is for Java Applications. It can run on JVM
• IronPython:
It is for C#.Net platform
• PyPy:
The main advantage of PyPy is performance will be improved because JIT
compiler is available inside PVM.
• RubyPython
For Ruby Platforms
• AnacondaPython
It is specially designed for handling large volume of data processing.
Python Versions:
• Python 1.0V introduced in Jan 1994
• Python 2.0V introduced in October 2000
• Python 3.0V introduced in December 2008
•  Note: Python 3 won't provide backward
compatibility to Python2
• i.e there is no guarantee that Python2 programs
will run in Python3.
Pending Topics

Pending Topics
Complex Data Type
• A complex number is of the form
str type:
•  str represents String data type.
• A String is a sequence of characters enclosed
within single quotes or double quotes.
s1='durga'
• s1="durga"
Slicing of Strings:
•  slice means a piece
• [ ] operator is called slice operator, which can be
used to retrieve parts of String. In Python Strings
follows zero based index.
• The index can be either +ve or -ve.
• +ve index means forward direction from Left to
Right
• -ve index means backward direction from Right
to Left
Example

Check the output for following


s="durga“
print( s[0])
print( s[1])
print( s[-1])
 print(s[40])
 
pass statement:
•  pass is a keyword in Python.
•  In our programming syntactically if block is
required which won't do anything then we can
define that empty block with pass keyword.
Example : Try this program
for i in range(100):
if i%9==0:
print(i)
else:pass
STRING : Accessing characters by using slice operator:

s="Learning Python is very very easy!!!“


print(s[1:7:1])
print(s[1:7])
Print(s[1:7:2])
print(s[:7])
print(s[7:])
print(s[::])
print(s[:])
print(s[::-1])
Mathematical Operators for String:
•  We can apply the following mathematical
operators for Strings.
1. + operator for concatenation
2. * operator for repetition

print("durga"+"soft")
print("durga"*2)
len() in-built function:
•  We can use len() function to find the number of
characters present in the string.
 
Eg:
s='durga'
print(len(s))
Write a program to access each character of string in
forward and backward direction by using while loop  
s="Learning Python is very easy !!!“
n=len(s)
i=0
print("Forward direction")
while i<n:
print(s[i],end=' ')
i +=1
print("Backward direction")
i=-1
while i>=-n:
print(s[i],end=' ')
i=i-1
Alternative ways:
 
s="Learning Python is very easy !!!“
print("Forward direction")
for i in s:
print(i,end=' ')

print("Forward direction")
for i in s[::]:
print(i,end=' ')

print("Backward direction")
for i in s[::-1]:
print(i,end=' ')
Checking Membership:
•  We can check whether the character or string is
the member of another string or not by using in
and not in operators

s='durga'
print('d' in s)
print('z' in s)
Check substring
s=input("Enter main string:")
subs=input("Enter sub string:")
if subs in s:
print(subs,"is found in main string")
else:
print(subs,"is not found in main string")
Comparison of Strings:
 We can use comparison operators (<,<=,>,>=) and equality operators(==,!
=) for strings. Comparison will be performed based on alphabetical
order.
Eg:
 
s1=input("Enter first string:")
s2=input("Enter Second string:")
if s1==s2:
print("Both strings are equal")
elif s1<s2:
print("First String is less than Second String")
else:
print("First String is greater than Second String")
• Replacing a string with another string:
 
s.replace(oldstring,newstring)
 inside s, every occurrence of oldstring will be
replaced with newstring.
 Eg1:
s="Learning difficult Python is very difficult“
s1=s.replace("difficult","easy")
print(s1)
Changing case of a String:
We can change case of a string by using the following methods.

s='learning Python is very Easy'


print(s.upper())
print(s.lower())
print(s.swapcase())
print(s.title())
print(s.capitalize())
Exception Handling
 In any programming language there are 2 types of
errors are possible.
 
1. Syntax Errors
2. Runtime Errors
Syntax Errors:
 
The errors which occurs because of invalid syntax
are called syntax errors.
Eg 1:
x=10
if x==10
print("Hello")
SyntaxError: invalid syntax
Eg 2:
print "Hello"
 
SyntaxError: Missing parentheses in call to 'print'
 
Note:
Programmer is responsible to correct these syntax
errors. Once all syntax errors are corrected then
only program execution will be started.
Runtime Errors:
•  Also known as exceptions.
• While executing the program if something goes wrong
because of end user input or programming logic or
memory problems etc then we will get Runtime Errors.
•  
Eg:
print(10/0)
==>ZeroDivisionError: division by zero
print(10/"ten")
==>TypeError: unsupported operand type(s) for /: 'int' and 'str'
x=int(input("Enter Number:")) # enter a string for input
print(x)

You might also like