Kalyani Government Engineering College
Department of Computer Application
PROGRAMMING CONCEPT THROUGH PYTHON - MCAN101
YEAR - 2023-2025
Sem - 1st Semester
Assignment 2
Name – AYUSH DAS
Roll - 910271023009
[1]
1
a) What are the arithmetic operators in python? Explain with example.
Arithmetic operators in Python are used to perform mathematical operations on numbers.
The available arithmetic operators are:
Addition (+): Adds two operands.
Subtraction (-): Subtracts the second operand from the first operand.
Multiplication (*): Multiplies the two operands.
Division (/): Divides the first operand by the second operand.
Modulus (%): Finds the remainder when the first operand is divided by the second
operand.
Exponentiation (^): Raises the first operand to the power of the second operand.
Floor division (//): Divides the first operand by the second operand and returns the
integer quotient.
Here are some examples of how to use arithmetic operators in Python:
# Addition
print(5 + 3) # Output: 8
# Subtraction
print(10 - 2) # Output: 8
# Multiplication
print(4 * 5) # Output: 20
# Division
print(12 / 3) # Output: 4.0
# Modulus
print(11 % 3) # Output: 2
# Exponentiation
print(2 ** 3) # Output: 8
# Floor division
print(10 // 3) # Output: 3
[2]
b) What are compilers and interpreters? Explain the difference with example
Indentation is a crucial aspect of Python programming, unlike other programming
languages where it's merely for readability. Python utilizes indentation to define the
structure and scope of code blocks. It's the consistent spacing of code lines,
typically using four spaces or one tab, to group statements that belong together.
Indentation plays a pivotal role in Python by:
Defining Code Blocks: Indentation clearly separates code blocks, such as the body of an
if statement, for loop, or function. It helps the Python interpreter determine which
statements belong to which block.
Example: Let's consider This Python program:
Code:
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
the 'print' statements belong to their respective 'if' and 'else' blocks due to indentation.
Code:
if x > 5:
print("x is greater than 5") # Missing indentation
This code will throw an indentation error because the 'print' statement is not indented.
Indentation is fundamental to Python programming. It's not just about aesthetics; it's about
ensuring the code's structure, readability, and correct execution. By following consistent
indentation guidelines, programmers can write clear, maintainable, and bug-free Python
code.
[3]
2
a) Write the key points for writing a good algorithm.
Source Code:
# Take input from the user
length = float(input("Enter the length of the rectangle: "))
breadth = float(input("Enter the breadth of the rectangle: "))
radius = float(input("Enter the radius of the circle: "))
# Calculate the area of the rectangle
rectangle_area = length * breadth
# Calculate the area of the circle
circle_area = 3.14 * radius * radius
# Print the results
print("The area of the rectangle is:", rectangle_area)
print("The area of the circle is:", circle_area)
Output:
Enter the length of the rectangle: 5
Enter the breadth of the rectangle: 8
Enter the radius of the circle: 9
The area of the rectangle is: 40.0
The area of the circle is: 254.34
[4]
b) Write a python program to swap two numbers without temporary variable.
Source Code:
a = int(input("enter first number"))
b = int(input("enter Second number"))
print("Before swapping:")
print("a =", a)
print("b =", b)
a^=b
b^=a
a^=b
print("After swapping:")
print("a =", a)
print("b =", b)
Output:
enter first number5
enter Second number6
Before swapping:
a=5
b=6
After swapping:
a=6
b=5
a) The first line of the performs a bitwise XOR operation on a and b. This operation stores
the result in a.
b) The second line of the function performs a bitwise XOR operation on a and b. This
operation stores the result in
c) The third line of the function performs a bitwise XOR operation on a and b. This
operation stores the result in b.
As a result of these three operations, the values of a and b are swapped.
[5]
c) Explain if-elif condition. When do we use it? Explain with example.
The if-elif statement is a powerful control flow construct in Python that allows you to
execute different blocks of code based on multiple conditions. It's an extension of the
simple if-else statement, allowing you to check for more than two possible conditions.
Structure of if-elif Statement
if condition1:
# Code block 1
elif condition2:
# Code block 2
elif condition3:
# Code block 3
else:
# Default code block
When to Use if-elif Statement
The if-elif statement is particularly useful when you have multiple possible conditions that
need to be checked. It's more efficient than using multiple nested if-else statements, which
can become cumbersome and difficult to read.
Example: Let's consider This Python program:
Code:
marks = 85
if marks >= 90:
grade = "A"
elif marks >= 80:
grade = "B"
elif marks >= 70:
grade = "C"
elif marks >= 60:
grade = "D"
else:
grade = "F"
print("Your grade is:", grade)
[6]
OutPut:
Your grade is: A
In this example, the if-elif statement efficiently checks for the different marks ranges and
assigns the corresponding grade. The else block handles the case when the marks fall
below 60.
Conclusion
The if-elif statement is an essential tool for writing clear, concise, and efficient Python
code. It allows you to handle multiple conditions without resorting to complex nested
structures. By using if-elif statements, you can ensure that your code is easy to
understand, maintain, and modify.
[7]