UNIT-2
UNIT-2
Program:
a = True Output:
b = False True
print (a) False
print (b)
Strings
• Strings in Python are identified as a
contiguous set of characters represented in
the quotation marks.
• Python allows for either pairs of single or
double quotes.
• Subsets of strings can be taken using the slice
operator ([ ] and [:] ) with indexes starting at 0
in the beginning of the string and working
their way from -1 at the end.
Strings
Program: Output:
str ="WELCOME"
print (str)
WELCOME
W
print (str[0])
LCO
print (str[2:5])
LCOME
print (str[2:]) WELCOMEWELCOME
print (str * 2) WELCOMECSE
print (str + "CSE“)
Strings Methods
• capitalize()
str1=“hello"
print (str1.capitalize() ) # Hello
• center(width, fillchar)
str1="welcome“
print (str1.center(15,"*") ) # ****welcome****
• len(string)
str1="welcome"
print (len(str1)) #7
Strings Methods
• count(str, beg= 0, end=len(string))
str1="welcome"
print (str1.count('e',0,len(str1))) #2
• endswith(suffix, beg=0, end=len(string))
str1="welcome"
print (str1.endswith('me',0,len(str1))) # True
• startswith(str, beg=0, end=len(string))
str1="welcome"
print (str1.startswith('me',0,len(str1))) # False
Strings Methods
• replace(old, new)
str5="welcome to java“
print (str5.replace("java", "python“))
#welcome to python
• find(str, beg=0, end=len(string))
str1="welcome“
print (str1.find('e',0,len(str1))) #1
Strings Methods
• isalnum()
str2="welcome2017"
print (str2.isalnum()) # True
• isalpha()
str2="welcome2017“
print (str2.isalpha()) # False
Strings Methods
• islower()
str2="welcome2017"
print (str2.islower()) # True
• isupper()
str2="welcome2017"
print (str2.isupper()) # False
DataType Conversion
• Sometimes, you may need to perform conversions
between the built-in types. To convert between
types, you simply use the type name as a function.
– int(x)-Converts x to an integer.
– float(x) -Converts x to float.
– str(x) -Converts x to string.
– tuple(s)-Converts s to tuple.
– hex(x)-Converts an integer to a hexa decimal
string.
Python Operators
• Arithmetic Operators
• Comparison (Relational) Operators
• Assignment Operators
• Logical Operators
• Bitwise Operators
• Membership Operators
• Identity Operators
Arithmetic Operators
a = 10
b = 20
Operator Description Example
+ Addition Adds values on either side of the operator. a + b = 30
- Subtraction Subtracts right hand operand from left hand operand. a – b = -10
If values of two operands are not equal, then condition (a <> b) is true. This is
<>
becomes true. similar to != operator.
nested if statements You can use one if or else if statement inside another if or else if statement(s).
if Statement
• The if statement contains a logical expression using
which data is compared and a decision is made based
on the result of the comparison.
• The condition is tested. If the condition is True, then
the statements given after colon (:) are executed.
• Syntax:
if Condition:
Statements
if Statement
Program:
a=10 Output:
b=15 B is Big
if a < b : The value is 15
print ("b is Big“)
print ("The value is", b)
if….else Statement
• An else statement contains the block of code that
executes if the conditional expression in the if
statement resolves to 0 or a FALSE value.
• Syntax:
if Condition:
Statements
else:
Statements
if Statement
Program:
a=48
b=34 Output:
if a < b: a is big
print ("b is big“) a value is 48
print ("b value is", b) END
else:
print ("a is big “)
print ("a value is", a)
print ("END“)
elif Statement
• The elif statement allows you to check multiple expressions for True and
execute a block of code as soon as one of the conditions evaluates to
True.
• Syntax:
if Condition1:
Statements
elif Condition2:
Statements
else:
Statements
elif Statement
Program:
a=20
b=10 Output:
c=30 c is big
if a >= b and a >= c:
print ("a is big“)
elif b >= a and b >= c:
print ("b is big“)
else:
print( "c is big“)
Iteration Statements
• In general, statements are executed sequentially: The
first statement in a function is executed first,
followed by the second, and so on. There may be a
situation when you need to execute a block of code
several number of times.
• A loop statement allows us to execute a statement or
group of statements multiple times.
Loop Type Description
Repeats a statement or group of statements while a given condition is TRUE.
while loop
It tests the condition before executing the loop body.
Executes a sequence of statements multiple times and abbreviates the code
for loop
that manages the loop variable.
nested loops You can use one or more loop inside any another while, for loop.
while Loop
• A while loop statement in Python programming
language repeatedly executes a target statement as
long as a given condition is True.
• Syntax:
while Condition:
Statements
while Loop
Program: Program:
i=1 i=1
while i <= 3 : while i <= 3 :
print (i) print (i)
i+=1 i+=1
print ("END") print ("END“)
Output: Output:
1 1
END 2
2 3
END END
3
END
for Loop
• The for loop is useful to iterate over the elements of a
sequence.
• It means, the for loop can be used to execute a group of
statements repeatedly depending upon the number of
elements in the sequence.
• The for loop can work with sequence like string, list, tuple,
range etc.
• Syntax:
for variable in sequence:
Statements
for Loop
Program: Program:
for i in range(1,4): for i in range(1,4):
print(i) print (i)
print ("END") print ("END“)
Output: Output:
1 1
END 2
2 3
END END
3
END
Nested Loops
• It is possible to write one loop inside another
loop.
• For example, we can write a for loop inside a
while loop or a for loop inside another for
loop.
Nested Loops
Program: Output
for i in range(1, 5): 1
for j in range(i): 22
print(i, end=' ') 333
4444
print()
Jump Statements
• There are Three jump statements,
break
continue
pass
break statement
• The break statement provides you with the opportunity to exit
out of a loop when an external condition is triggered.
Program:
n=int(input("Enter the n value"))
count=0 Output:
for i in range(2,n): Enter the n value: 7
if n%i==0: Prime Number
count=count+1
break
if count==0:
print ("Prime Number")
else:
print ("Not Prime Number")
continue statement
• The continue statement gives you the option to skip over
the part of a loop where an external condition is
triggered, but to go on to complete the rest of the loop.
Program:
for number in range(1,7):
Output:
Number is 1
if number == 4: Number is 2
continue Number is 3
print ( 'Number is ' + str(number)) Number is 5
Number is 6
pass statement
• When an external condition is triggered, the pass
statement allows you to handle the condition
without the loop being impacted in any way.
Program:
Output:
for number in range(1,7): Number is 1
if number == 4: Number is 2
pass Number is 3
print ('Number is ' + str(number)) Number is 4
Number is 5
Number is 6
O U
K Y
A N
TH