Assign 3 Python
Assign 3 Python
Assign 3 Python
Roll no.: 5
Class: T.Y.BCS
Assignment 3
Users convert the data type of an object to the data type required by Explicit Type Conversion. To
perform explicit type conversion, we use predefined functions such as int(), float(), str().
This conversion form is also called typecasting as the user casts (changes) the object data type.The
conversion of the explicit type happens when the programmer specifies the program clearly and
explicitly. There are several built-in Python functions for explicit form conversion.
Example Code:
try:
a = "10010"
# string converting to int base 2
c = int(a,2)
print('string converted to int base 2 :',c)
# string converting to int base 10
c = int(a)
print('string converted to int base 10:',c)
except TypeError as typeError:
print(typeError)
Output:
string converted to int base 2 : 18
string converted to int base 10: 10010
Q.4 Explain the loop control statements (break, continue, pass) along
with syntax and example.
s = 'geeksforgeeks'
# Using for loop
for letter in s:
print(letter)
# break the loop as soon it sees 'e'
# or 's'
if letter == 'e' or letter == 's':
break
print("Out of for loop")
print()
i=0
# Using while loop
while True:
print(s[i])
# break the loop as soon it sees 'e'
# or 's'
if s[i] == 'e' or s[i] == 's':
break
i += 1
print("Out of while loop")
Output:
g
e
Out of for loop
g
e
Out of while loop
Q.5 Explain the use of format() function and write an example for using
format function in different ways.
The format( ) function used with strings is very versatile and powerful function used for
formatting strings.
The curly braces { } are used as placeholders or replacement fields which get replaced
along with format( ) function.
Example:
num1 = int (input (“Number 1: “))
num2 = int (input (“Number 2: “))
print (“The sum of { } and { } is { }”.format (num1, num2, (num1 + num2)))
Output:
Number 1 : 34
Number 2 : 54
The sum of 34 and 54 is 88.
Q.6 Explain any five methods belonging to strings in Python with
examples.
swapcase(): Make the lower case letters upper case and the upper
case letters lower case
txt ="Hello My Name Is ROHIT"
x = txt.swapcase()
print(x)
Output:
hELLO mY nAME iS rohit