0% found this document useful (0 votes)
22 views

Python ASSIGNMENT

The document discusses Python programs involving operators, functions, looping statements and conditional statements. It includes the objectives, algorithms, pseudo codes and programs for different problems related to arithmetic operations, finding divisors of a number, calculating sum of multiple numbers, user-defined functions, scoring based on alien color and determining life stage based on age.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Python ASSIGNMENT

The document discusses Python programs involving operators, functions, looping statements and conditional statements. It includes the objectives, algorithms, pseudo codes and programs for different problems related to arithmetic operations, finding divisors of a number, calculating sum of multiple numbers, user-defined functions, scoring based on alien color and determining life stage based on age.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PP 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) :

Arithmetic Operators, Data Types, Input statement, Output statement, Comment

ALGORITHM :

Step 1 : Start the program

Step 2 :

Step 3 :

Step 4 : Stop the program

PSEUDOCODE :

BEGIN
END

PROGRAM :

print("Python Program to check the priority of four arithmetic operators (+, -, *, /)")

# Let us assume some values to get much more clarity

a = int(input("\nEnter Number 1 : "))

b = int(input("Enter Number 2 : "))

c = int(input("Enter Number 3 : "))

d = int(input("Enter Number 4 : "))

e = int(input("Enter Number 5 : "))

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 1 : Start the program

Step 2 :

Step 3 :

Step 4 : Stop the program

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 :

Write a Python program to find sum of multiple numbers.

OBJECTIVE :

To get multiple input values from the user to find the sum of multiple numbers.

SYNTAX (Keywords) :

For Loop, Input statement, Data Types, Output statement, Comment

ALGORITHM :

Step 1 : Start the program

Step 2 :

Step 3 :

Step 4 : Stop the program

PSEUDOCODE :

BEGIN

END

PROGRAM :

n=int(input("Enter the number of values to sum : "))

total=0

for i in range(0,n):
num=float(input("Enter the Number "+str(i+1)+" : "))

total=total+num

print("The sum of the given",n,"numbers is :",total)

OUTPUT :

REFERENCE :

https://www.codingconception.com/python-examples/add-multiple-numbers-in-python/

PROGRAM 2 (b) :

QUESTION :

Write a Python program to illustrate the use of user-defined functions.

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) :

User-Defined Function, Input statement, Data Types, Output statement, Comment

ALGORITHM :

Step 1 : Start the program

Step 2 :

Step 3 :

Step 4 : Stop the program

PSEUDOCODE :

BEGIN

END

PROGRAM :
def square_of(n):

return n**2

n=int(input("Enter a number to find it's square : "))

print("The square of",n,"is",square_of(n))

OUTPUT :

REFERENCE :

https://www.programiz.com/python-programming/user-defined-function

Program 3 (a) :

QUESTION :

Write a Python program for the below conditions,

 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) :

Input Statement, Data Types, If-Elif-Else Statement, Output statement

ALGORITHM :

Step 1 : Start the program

Step 2 :

Step 3 :

Step 4 : Stop the program

PSEUDOCODE :

BEGIN
END

PROGRAM :

alien=input("Enter the color of the alien : ")

if(alien=="green"):

print("Player earned 5 points")

elif(alien=="yellow"):

print("Player earned 10 points")

elif(alien=="red"):

print("Player earned 15 points")

else:

print("Enter any other color among green,yellow,red")

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) :

Input Statement, Data Types, If-Elif Statement, Output statement

ALGORITHM :

Step 1 : Start the program

Step 2 :

Step 3 :

Step 4 : Stop the program

PSEUDOCODE :

BEGIN

END

PROGRAM :

person=int(input("Enter the age of the person to find the stage of his life : "))

if(person<2):

print("The person is a Baby")

elif(4>person>=2):

print("The person is a Toddler")

elif(13>person>=4):

print("The person is a Kid")

elif(20>person>=13):

print("The person is a Teenager")

elif(65>person>=20):

print("The person is an Adult")


elif(person>=65):

print("The person is an Elder")

OUTPUT :

REFERENCE :

https://ehmatthes.github.io/pcc/solutions/chapter_5.html

You might also like