CT-1 - Paper (Python-BCC302) - Solution
CT-1 - Paper (Python-BCC302) - Solution
➢ Comments start with a #, and Python will render the rest of the line as a comment:
EX:
# This is a comment.
print("Hello, World!")
Sol: Arithmetic Operators are used to perform mathematical operations like addition,
subtraction, multiplication, and division.
Operator Description
It is used to add two operands. For example, if a = 20, b = 10
+ (Addition)
=>a+b = 30
====================================================================
num_int = 123
num_flo = 1.23
print("datatype of um_int:",type(num_int))
print("datatype of um_flo:",type(num_flo))
print("Value of num_new:",num_new)
print("datatype of um_new:",type(num_new))
Explicit Type Conversion:
In Explicit Type Conversion, users convert the data type of an object to
required data type.
We use the predefined functions (Constructors) like int(), float(), str(), etc to
perform explicit type conversion.
This type of conversion is also called typecasting because the user casts
(changes) the data type of the objects.
num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))
num_sum = num_int + num_str
print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
Qu-2(b) What are local and global variables in Python? Explain with
example.
Sol:
Python Global variables are those which are not defined inside any function and have
a global scope whereas Python local variables are those which are defined inside a
function and their scope is limited to that function only.
Python Local Variables: Local variables in Python are those which are initialized
inside a function and belong only to that particular function. It cannot be accessed
anywhere outside the function. Let’s see how to create a local variable.
def f():
# local variable
s = "I love Geeksforgeeks"
print(s)
f()
Python Global Variables: These are those which are defined outside any function and
which are accessible throughout the program, i.e., inside and outside of every function.
Let’s see how to create a Python global variable.
# This function uses global variable s
def f():
print("Inside Function", s)
# Global scope
s = "I love Geeksforgeeks"
f()
print("Outside Function", s)
Ex:
x = ["Java", "Python"]
print("Python" in x) # True
print("PHP" in x) # False
====================================================================
O/P: apple
for i in range(10): ``
print(i,end = ' ')
O/P: 0 1 2 3 4 5 6 7 8 9
The pass statement is a null statement. But the difference between pass and comment
is that comment is ignored by the interpreter whereas pass is not ignored.
The pass statement is generally used as a placeholder i.e. when the user does not know
what code to write. So, user simply places pass there as empty code is not allowed in
loops, function definitions, class definitions, or in if statements. So, using pass
statement user avoids this error.
Ex:
a = 10
b = 20
if(a<b):
``
pass
else:
print("b<a")
Qu-3(d) Write a program to take three numbers from the user and
print the greatest number.
Sol:
x = int(input("Enter First number"))
y = int(input("Enter Second number"))
z = int(input("Enter Third number"))
``
a = x if x>y else y
b = a if a>z else z
print("Largest Number is :", b)
====================================================================
Python if statement:
The if statement is the most simple decision-making statement. It is used to decide
whether a certain statement or block of statements will be executed or not.
Syntax:
``
if expression:
statement
num = 3
if num > 0: ``
print(num, "is a positive number.")
print("This is always printed.")
Python If-Else Statement:
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t. But if we want to do something else if
the condition is false, we can use the else statement with if statement to execute a block
of code when the if condition is false.
Syntax:
if condition:
#block of statements``
else:
#another block of statements (else-block)
Ex:
Syntax:
if expression 1:
# block of statements
elif expression 2:
# block of statements ``
elif expression 3:
# block of statements
else:
# block of statements
Ex:
if x==rev:
print("Palindrome")
else:
print("Not a Palindrome")