Assign 3 Python

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Name:Rohit Bairwa

Roll no.: 5
Class: T.Y.BCS
Assignment 3

Q.1) Explain the concept of type conversion(Explicit Type Conversion)


along with functions and examples.

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.2) Explain conditional statements (if, if else, nested if, if elif,


shorthand if) with examples.
Decision making is required when we want to execute a code only if a certain condition is satisfied.

The if..else..if statement is used in Python for decision making.


Here, the program evaluates the test exppresion and will execute statement(s) only if the test
expression is True.
If the test expression is False, the statement(s) is not executed.
In Python, the body of thes if tatement is indicated by the indentation. The body starts with an
indentation and the first unindented line marks the end.
Python interprets non-zero values asTrue.None and 0 are interpreted as False
Q.3 What is range() function in Python ? Explain with syntax and
example.
The range function simplifies the process of writing a for loop.
* range can be used to create sequence
*range returns an iterable object
Iterable- contains a sequence of values that can be iterated over.
Characteristics of range :-
*range(stop) -takes one argument
*range(start,stop)-takes two arguments
*range(start,stop,step)- takes three arguments
Syntax-
range(lower limit, upper limit, increment/decrement)
examples-
1.for value in range(1,6) :
print(value)
output:
12345
2.for value in range(0,6,2):
print(value)
output:
024

Q.4 Explain the loop control statements (break, continue, pass) along
with syntax and example.

The break statement is used to terminate the loop or statement in which it is


present. After that, the control will pass to the statements that are present
after the break statement, if available. If the break statement is present in the
nested loop, then it terminates only those loops which contains break
statement.
# Python program to demonstrate
# break statement
# Python program to
# demonstrate break statement

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.

upper() : Converts string to upper case.


a="Hello World"
x = a.upper()
print(x)
Output : HELLO WORLD
lower(): Converts string in lower case.
a="HELLO WORLD"
x=a.lower()
print(x)
Output: hello world
title():Converts string
a="Welcome to my world"
x = a.title()
print(x)
Output:

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

translate():Returns a translated string


mydict = {83: 80}
txt = "Hello Sam!"
print(txt.translate(mydict))

You might also like