Professional Documents
Culture Documents
PYTHONPPT
PYTHONPPT
PYTHONPPT
Basics
Basics of Python
Introduction
Python Interpreter
Comments
Variables and Data Types[Numbers and Strings]
Operators
Data Structures- List, Tuple and Dictionary
Control Flow Statements
Functions
Files
Exception Handling
Example
# This is used as a single-line comment and Multiline Comment
Example:
And
exec
not
Assert
finally
Or
if True:
print "True“
else:
print "False"
a=b=c=1
a, b, c = 1, 2, "john"
PYTHON Dec 19, 2023
Python Data Types
Python supports the following data types:
Data type-To identify the type of data
Number --- int, float, long and complex --- Ex: 13, 12.5, 2i+3j, 23456789L
String---- Sequence of characters within “”----Ex: “Welcome”
List --- Sequence of items within [] ---Ex: L1=[56,70,90]
Tuple --- Sequence of items within () ---Ex: T1=(56,70,90)
Dictionary --- Sequence of items within {} ---Ex: D1={ ‘name’: ’kavi’, ’age’:17}
Arithmetic operators
x=20;y=10;m=9;n=2;
+ ------- x + y ----- 30
- ------- x - y ----- 10
* ------- x *y ----- 200
/ ------- x / y ----- 2
% ------- x % y ----- 0
** -------- m**n ---- 81
// -------- m//n ---- 4
Assignment operators
x=y x=y
x += y x=x+y
x -= y x=x- y
x *= y x = x * y
x /= y x = x / y
x %= y x = x % y
x**=y x=x**y
x//=y x=x//y
Comparison operators
x=20 y=10 z=10
== x == y False
!= x !=y True
<> x <> y True
> x>y True
< x<y False
>= x >= y True
<= x <= y False
Logical operators
and x and y
or x or y
xor x xor y
Identity operators
x=10
y=20
is x is y
is not x is not y
Membership operators
L1=[‘x’,’y’,’z’]
in x in L1
not in x not in L1
Logical operators
and x and y
or x or y
xor x xor y
Syntax
if condition:
code to be executed if condition is true;
Example
t = 15;
if t < 20:
print "Have a good day!"
Output:
Have a good day!
•if...else statement
Syntax:
if condition:
code to be executed if condition is true;
else :
code to be executed if condition is false;
Example
t = 25
if t < 20:
Print "Have a good day!“
else:
print "Have a good night!"
Output:
Have a good night!
•if...elif....else statement
Syntax:
if condition:
elif condition:
else:
•Example
a=20
b=200
if a > b:
print "a is bigger than b”
elif a == b:
print "a is equal to b“
else :
print “a is smaller than b“
Output:
a is smaller than b
Syntax:
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
elif expression4:
statement(s)
else:
statement(s)
•While statement
while - loops through a block of code as long as the specified
condition is true
Syntax
while condition :
code to be executed
Example
x=1
while x <= 3:
print "The number is: “,x
x=x+1
Output:
The number is: 1
The number is: 2
The number is: 3
For Loop
Ability to iterate over the items of any sequence,
such as a list or a string.
Syntax:
List Functions:
1. cmp(list1, list2)
Compares elements of both lists.
2. len(list)
Gives the total length of the list.
3.max(list)
Returns item from the list with max value.
4.min(list)
Returns item from the list with min value.
5.list(seq)
Converts a tuple into list.
Functions
•Block of statements that can be used repeatedly in a
program.Not execute immediately .
•Executed by a call to the function.
Syntax:
def functionName():
“function documentation string”
code to be executed
return(expression)
Functions
Example:
def writeMsg():
“print one message”
print "Hello world!“
return
#main
writeMsg() #call the function
Output: Hello World
Function Arguments:
•Required arguments
•Keyword arguments
•Default arguments
•Variable-length arguments
Function Arguments:
•Required arguments
Example:
def writeMsg(s):
“print one message”
print s
return
#main
writeMsg(“Hello World”) #call the function
Output: Hello World
Function Arguments:
• Keyword arguments
Example:
def writeMsg(age, name):
“print one message”
print age
print name
return
#main
writeMsg(name=“ShanmugaPriyan”, age=17)
Output: 17
ShanmugaPriyan
Output is:
10
Output is:
70 60 50