Python unit 2 ppt
Python unit 2 ppt
KONGUNADU COLLEGE OF
ENGINEERING AND TECHNOLOGY
Python Programming History
It was created by Guido Van Rossum ,
python is derived from many other language
including ABC , Modula 3, C , C++ , Algol-68 ,
Small talk , Unix. It got name from “Monty
Python Flying Circus”.
‘python’ is a general purpose interpreted ,
interactive , object oriented ,and high level
language,
Released in 1991, it support both 32 and
64 bit integers and wordsize.
Advantages
Python is High level language:
It is look like a normal English. It is most
compatible with human language.
Python is interpreted:
Python is processed at run time by the
interpreter, you do not need to compile your
program before executing it.
Python support Object –Oriented,
Python is a beginners language.
Python is interactive :
you can type into python prompt and interact
with the interpreter directly to write the program.
Features and application of ‘python’
languages
• Python is general purpose ,structured
programming language.
• Easy to learn.
• Easy to maintain.
• Free and open source.
• High level language.
• Extensive libraries.
Why we need python
• Software quality
• Developer productivity
• Support library
• Enjoyment
• Program portability
PYTHON INTERPRETER
Source code
Interpreter
Input data
Output
Interpreter:
An interpreter processes the program a little at a time ,
alternatively reading lines and performing computation.
python Analyze and Executes program statements at the same
time ,
Overall Execution time is high,
Easier to debug the program.
There are two ways use interpreter:
1. Interactive mode - In this to type the program then press enter and
the interpreter displays the result.
>>> 2+2
>>> 4
The chevron , >>> is the prompt the interpreter uses to indicate that it is
ready.
2. Script mode:
If you type multiple statement into a file and save with .py
extension , then the interpreter execute the file is called a script,
Interactive mode
• The interpreter provides an interactive
environment to play with the language
• Results of expressions are printed on the
screen
>>> 3 + 7
10
>>> 3 < 15
True
>>> 'print me'
'print me'
>>> print 'print me'
print me
>>>
Interpreter Vs Compiler
Interpreter Compiler
Translates program one statement at a Scans the entire program and translates it
time. as a whole into machine code.
It takes less amount of time to analyze the It takes large amount of time to analyze
source code but the overall execution time the source code but the overall execution
is slower. time is comparatively faster.
Programming language like Python, Ruby Programming language like C, C++ use
use interpreters. compilers.
Values and Types
Values:
• A value is one of the fundamental things like
word or a number that a program manipulate.
Types:
– Integer
– Float
– Booleans
– String
– List
Integer & Float
• Integer – whole numbers
Ex: a = 10
b = -25
• Float – floating point values
Ex:
a=6.5
b=10.14
• To view the type of data:
>>> c=64
>>> type(c)
<type int>
Boolean
• A Boolean variable can take two values
True
False
Ex:
>>> 34>64
False
>>> 68>45
True
>>> test=(45>32)
>>> test
True
Strings
• Set of characters represented in quotation marks,
either single or double quotes.
• Subsets of string can be taken by using the slice
operator[:] with index 0 is starting.
Character:
• It is a single alphabet represented by single, double
or trible quotes.
Ex: ‘a’ or “a” or ‘“a’’’
String:
• It is represented by more than one character within
single, double or trible quotes.
Ex: ‘Hi’ or “Hi” or ‘“Hi’’’
Operator on strings:
‘+’ and ‘*’
>>> ‘horse ’ + ‘and ’ + ‘dog’
horse and dog
>>> “#” * 5
#####
String operations
• Find() – to find whether the given character is
present in the string or not
• Ex:
>>> a=“hello world”
>>> print(a.find(“w”))
>>> 6
• Split() – to split the strings
• Ex:
>>> print(a.split(“ ”))
(‘hello’,‘world’)
• Slicing – to view particular character or string
Ex: >>> print(a[6:12])
world
• Strip() –to remove a string
Ex: >>> print(a.strip(“hel”))
o world
• lstrip() –to remove the first character from the
string
Ex: >>> print(a.lstrip(“h”))
ello world
>>> b=‘44444hihello222’
>>> print(b.lstrip(‘4’))
hihello222
• upper() – to convert the strings into uppercase
Ex: >>> d=“welcome”
>>> print(d.upper())
WELCOME
• Local variable:
a variable which is declared inside a
function is called as local variable.
• Global variable:
a variable which is declared outside a
function is called as global variable.
Expression
• Expression is the combination of variables
and constants are interconnected with
operators.
• Syntax: variable = expression
• Ex: d = (a+b*c)
Statements
• It is an instruction which is executed by
python interpreter.
@ Print
@ Assign
Ex:
workout this expression and write correct answer
3 - 12/3 + 77 * 3 - 1
Comments
# sign used to write comment line in between the programs.
Cannot be executed.
Types :
– Single line
– Multi line
Single line:
Single line comment starting with # symbol
Ex:
# This is a sample example
a=10
# The value 10 is assigned to a variable
print(a)
Output:
10
Multi line:
Multi line comment starting with # symbol
Ex:
# This is a sample
# to print a string
print(“hello”)
Output:
hello
Module
• a module is a file consisting of Python code. A
module can define functions, classes and variables.
A module can also include runnable code.
Example:
Here's an example of a simple module, support.py
def print_func( par ):
print ("Hello : ", par )
return