py2
py2
UNIT-II
PRESETED BY
E.RAJALAKSHMI
PYTHON PROGRAMMING
2
Repetition - used for looping, i.e., repeating a piece of code multiple times.
PYTHON PROGRAMMING
4
Sequential
Selection
Simple if
if-else
nested if
if-elif-else
PYTHON PROGRAMMING
6
Simple if
if expression:
statement
Example 1
num = int(input("enter the number?"))
if num%2 == 0:
print("Number is even")
Output:
enter the number?10
Number is even
PYTHON PROGRAMMING
The if-else statement 7
Output:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
PYTHON PROGRAMMING
9
The elif statement
Repetition
Syntax
for item in sequence:
statements(s)
PYTHON PROGRAMMING
12
for loop
while loop
Output:
s
t
r
The end
PYTHON PROGRAMMING
15
Syntax
#block of code
except Exception1:
#block of code
except Exception2:
try:
#block of code
#other code
PYTHON PROGRAMMING
19
try:
a = int(input("Enter a:")) Output:
b = int(input("Enter b:")) Enter a:10
Enter b:0
c = a/b Can't divide with zero
except:
print("Can't divide with zero")
PYTHON PROGRAMMING
20
try:
#block of code
except Exception1:
#block of code
else:
#this code executes if no except block is executed
PYTHON PROGRAMMING
21
try:
a = int(input("Enter a:"))
Output:
b = int(input("Enter b:")) Enter a:10
c = a/b Enter b:0
can't divide by zero
print("a/b = %d"%c) <class 'Exception'>
# Using Exception with except statement.
If we print(Exception) it will return exception class
except Exception:
print("can't divide by zero")
print(Exception)
else:
print("Hi I am else block")
PYTHON PROGRAMMING
22
Syntax Error
Python
String Operations
String Operations
String Operations
str1="Hello"
str2="Hello"
str3="HELLO"
print(str1 == str2)
print(str1 == str3)
Output:
True
False
PYTHON PROGRAMMING
28
Built in functions using strings
String Upper()
Output
this is string example....wow!!!
String Lower ()
Python string method lower() returns a copy of the string in which all case-based characters
have been lowercased.
str = "THIS IS STRING EXAMPLE....WOW!!!";
print str.lower()
Output
this is string example....wow!!!
PYTHON PROGRAMMING
29
Built in functions using strings
string_name.islower()
string_name.isupper()
isupper() is used to check if the given string is in uppercase or not. This function also returns a boolean
value as result, either True or False.
Example
>>> s = "Hello"
>>> print (len(s))
Output
Length of the string: 5
PYTHON PROGRAMMING
31
Built in functions using strings
replace() function will first of all take a string as input, and ask for some subString within
it as the first argument and ask for another string to replace that subString as the second
argument. For Example,
Python string method join() returns a string in which the string elements of sequence have
been joined by str separator.
Syntax: str.join(sequence)
s = "-";
seq = ("a", "b", "c"); # This is sequence of strings.
print s.join( seq )
Output:
a-b-c
PYTHON PROGRAMMING
33
Built in functions using strings
string traversing.
fruit = "fruit"
index = 0
while index < len(fruit):
letter = fruit[index]
print(letter)
index = index + 1
E.RAJALAKSHMI-RAAK ARTS AND SCIENCE COLLEGE
34
PYTHON PROGRAMMING