Python ASSIGNMENT
Python ASSIGNMENT
INTRODUCTION :
Here we solve all the programs using operators, functions and looping statements in python.
OPERATORS :
Operators are symbols (sometimes keywords) that are predefined to perform a certain most
commonly required operations on one or more operands. In other words, we can say that an
operator operates the operands. In Python, there are seven different types of operators: arithmetic
operators, assignment operators, comparison operators, logical operators, identity operators,
membership operators, and boolean operators.
FUNCTIONS :
A function is a block of code which only runs when it is called. You can pass data, known as
parameters, into a function. A function can return data as a result.
LOOPING STATEMENTS :
Looping statement allows us to modify the flow of the program so that rather than writing
the same code, again and again, we are able to repeat the code a finite number of times. In Python,
there are three different types of loops: for loop, while loop, and nested loop.
PROGRAM 1 (a) :
QUESTION :
Write a Python program to check the priority of the four operators (+,-,*,/)
OBJECTIVE :
To get multiple input values from the user by creating some variables and performing an
arithmetic operation over it to find the priority over those four operators.
SYNTAX (Keywords) :
ALGORITHM :
Step 2 :
Step 3 :
PSEUDOCODE :
BEGIN
END
PROGRAM :
print("Python Program to check the priority of four arithmetic operators (+, -, *, /)")
print("\na * b + c - d / e : ", a * b + c - d / e)
print("\n The priority is given in the order of multiplication, division, addition and subtraction")
print("\n The given values are solved using 'PEMDAS' rule by (a*b)+c-(d/e)")
OUTPUT :
REFERENCE :
https://www.programiz.com/python-programming/precedence-associativity
PROGRAM 1 (b) :
QUESTION :
Write a Python program to find the number of divisor of the given integer is even or odd.
OBJECTIVE :
To create and call a user-defined function by using for and if else loops to find the number of
divisor of the given integer is even or odd.
SYNTAX (Keywords) :
User-defined Function, For Loop, If-Else Loop, Input statement, Output statement, Comment
ALGORITHM :
Step 2 :
Step 3 :
PSEUDOCODE :
BEGIN
END
PROGRAM :
def divisors(n):
result=[]
for i in range(1,n//2+1):
if(n%i==0):
result.append(i)
result.append(n)
a = len(str(result))
b=int(a)
if(b%2==0):
print("Even")
else:
print("odd")
return result
n=int(input("Enter a number to find the number of divisor is whether even or odd : "))
print(divisors(n))
OUTPUT :
REFERENCE :
https://www.geeksforgeeks.org/python-program-for-check-if-count-of-divisors-is-even-or-
odd/
PROGRAM 2 (a) :
QUESTION :
OBJECTIVE :
To get multiple input values from the user to find the sum of multiple numbers.
SYNTAX (Keywords) :
ALGORITHM :
Step 2 :
Step 3 :
PSEUDOCODE :
BEGIN
END
PROGRAM :
total=0
for i in range(0,n):
num=float(input("Enter the Number "+str(i+1)+" : "))
total=total+num
OUTPUT :
REFERENCE :
https://www.codingconception.com/python-examples/add-multiple-numbers-in-python/
PROGRAM 2 (b) :
QUESTION :
OBJECTIVE :
To create an user-defined function and get input from user to find the square of the given
number which illustrates the use of it.
SYNTAX (Keywords) :
ALGORITHM :
Step 2 :
Step 3 :
PSEUDOCODE :
BEGIN
END
PROGRAM :
def square_of(n):
return n**2
OUTPUT :
REFERENCE :
https://www.programiz.com/python-programming/user-defined-function
Program 3 (a) :
QUESTION :
If the alien is green, print a message that the player earned 5 points.
If the alien is yellow, print a message that the player earned 10 points.
If the alien is red, print a message that the player earned 15 points.
Write three versions of this program, making sure each message is printed for the
appropriate color alien.
OBJECTIVE :
To get some color as input from the user and display the points to the user according to the
color.
SYNTAX (Keywords) :
ALGORITHM :
Step 2 :
Step 3 :
PSEUDOCODE :
BEGIN
END
PROGRAM :
if(alien=="green"):
elif(alien=="yellow"):
elif(alien=="red"):
else:
OUTPUT :
REFERENCE :
https://ehmatthes.github.io/pcc/solutions/chapter_5.html
Program 3 (b) :
QUESTION :
Write an if-elif-else chain using python program that determines a person’s stage of life. Set
a value for the variable age, and then :
If the person is less than 2 years old, print a message that the person is a Baby.
If the person is at least 2 years old but less than 4, print a message that the person is
a Toddler.
If the person is at least 4 years old but less than 13, print a message that the person
is a Kid.
If the person is at least 13 years old but less than 20, print a message that the
person is a Teenager.
If the person is at least 20 years old but less than 65, print a message that the
person is an Adult.
If the person is age 65 or older, print a message that the person is an Elder.
OBJECTIVE :
To get the age as input from the user to display the stage of the person’s life.
SYNTAX (Keywords) :
ALGORITHM :
Step 2 :
Step 3 :
PSEUDOCODE :
BEGIN
END
PROGRAM :
person=int(input("Enter the age of the person to find the stage of his life : "))
if(person<2):
elif(4>person>=2):
elif(13>person>=4):
elif(20>person>=13):
elif(65>person>=20):
OUTPUT :
REFERENCE :
https://ehmatthes.github.io/pcc/solutions/chapter_5.html