Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Python Assignment 1

This document contains an assignment for the subject Programming Concept with Python. It includes questions about why Python is commonly used for first time learners, arithmetic operators in Python with examples, the role of indentation in Python with an example, a program to calculate the area of a rectangle and circle by taking user input, a program to swap two numbers without a temporary variable, and an explanation of if-elif conditions with an example.

Uploaded by

atanu bhowmik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Python Assignment 1

This document contains an assignment for the subject Programming Concept with Python. It includes questions about why Python is commonly used for first time learners, arithmetic operators in Python with examples, the role of indentation in Python with an example, a program to calculate the area of a rectangle and circle by taking user input, a program to swap two numbers without a temporary variable, and an explanation of if-elif conditions with an example.

Uploaded by

atanu bhowmik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment for CA2

NAME: ATANU BHOWMIK


ENROLLMENT NO: KGEC/MCA/22/08
DEPARTMENT: MCA
SUBJECT: Programming Concept with
Python
SUBJECT CODE: MCAN-101
1. a) What are the reasons for python being the first programming language of learners?
Ans: Python features English syntax and was designed to be concise and easy to read all of
which makes it more accessible to first-time coders than many other languages. Python is
also easy to learn because it is an interpreted programming language.
Reasons for Using Python as a Beginner:

i. Easy-to-learn − Python has few keywords, simple structure, and a clearly


defined syntax. This allows the student to pick up the language quickly.
ii. Easy-to-read − Python code is more clearly defined and visible to the eyes.
iii. Easy-to-maintain − Python's source code is fairly easy-to-maintain.
iv. Portable − Python can run on a wide variety of hardware platforms and has
the same interface on all platforms.
v. Type and Run – In the most of the projects testing something new requires
scores of changes and hence recompilation and re-runs. This makes testing of
code a difficult and time-consuming task. In Python, a code can be run easily.
vi. Free – Python is not propriety software. One can download Python
compilers; from amongst the various available choices. Moreover, there are
no known legal issues involved in the distribution of the code developed in
Python.
1. b) What are the arithmetic operators in python? Explain with example.

Ans: Assume variable a holds 20 and variable b holds 10, then –


i. Addition(+) - Adds values on either side of the operator.
Example : a + b =3 0

ii. Subtraction(-) - Subtracts right hand operand from left hand operand.
Example : a – b = 10

iii. Multiplication(*) - Multiplies values on either side of the operator.


Example : a * b = 200

iv. Division(/) - Divides left hand operand by right hand operand


Example : a / b = 2

v. Modulus(%) - Divides left hand operand by right hand operand and returns
remainder
Example : a % b = 0

vi. Exponent(**) - Performs exponential (power) calculation on operators


Example : a**b =20 to the power 10

vii. Floor Division(//) - The division of operands where the result is the quotient
in which the digits after the decimal point are removed. But if one of the
operands is negative, the result is floored, i.e., rounded away from zero
(towards negative infinity).
Example : 9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -11.0//3 = -4.0
1. c) What are the role of indentation in python? Provide example to support your answer.
Ans : Many a times it is required to treat more than one statements in a program as a block.
Different programming languages use different techniques to define scope and extent of
block of statements in constructs like class, function, conditional and loop. In C and C++ for
example, statements inside curly brackets are treated as a block. Python uses uniform
indentation to mark block of statements.
Before beginning of block symbol : is used. First and subsequent statements in block are
written by leaving additional (but uniform) whitespace (called indent) . In order to signal end
of block, the whitespace is deducted.
For Example :

age = int(input('Enter your age :'))

if(age==18):

print('issue a driving license')

elif(age>18):

print(‘allowed to drive')

else:

print(‘not allowed to drive’)


2. a) Write a python program to calculate the area of rectangle and circle and print the
result. Take input from user (length, breadth and radious).
Ans:
radious = int(input("Enter radious : "))
pi = 3.14
area = pi*radious*radious

print("The area is",area)


Output :

length = int(input("Enter length : "))


width = int(input("Enter width : "))

area = length*width
print("The area is",area)
Output :

2.b) Write a python program to swap two numbers without temporary variable.
Ans :
A = int(input("Enter value for A : "))
B = int(input("Enter value for B : "))
print("Before swaping : ",A,B)
A = A + B
B = A - B
A = A - B
print("After swaping : ",A,B)

Output :

2.c) Explain if-elif condition. When do we use it? Explain with example.
Ans :
 The elif is short for else if. It allows us to check for multiple expressions.If the
condition for if is False, it checks the condition of the next elif block and so on.If all
the conditions are False, the body of else is executed.Only one block among the
several if...elif...else blocks is executed according to the condition.The if block can
have only one else block. But it can have multiple elif blocks.

 The elif statement enables us to check multiple conditions and execute the specific
block of statements depending upon the true condition among them. We can have
any number of elif statements in our program depending upon our need. However,
using elif is optional.The elif statement works like an if-else-if ladder statement in C.
It must be succeeded by an if statement.
Example :
a=int(input("enter first number\n"))
b=int(input("enter second number\n"))
c=int(input("enter third number\n"))
if(a>b and a>c):
print(a,"is greatest")
elif(b>a and b>c):
print(b,"is greatest")
else:
print(c,"is greatest")

Output

You might also like