ASTER PUBLIC SCHOOL, GR.
NOIDA WEST, HS-1, SECTOR 3
Class - IX
SUBJECT: ARTIFICIAL INTELLIGENCE
UNIT -5 (Introduction to Python)
A. Short answer type questions.
1. What is an algorithm?
Ans. An algorithm is a set of sequential steps to solve any logical or mathematical problem.
It is always written in simple and precise manner so that anyone can understand the steps.
2. What is a flowchart?
Ans. A flowchart is a diagrammatic representation of the steps of an algorithm, which are
used for solving a particular problem. It makes the programs easy to understand.
3. List all the standard graphics for making a flowchart.
Ans. The standard graphics for making a flowchart are:
- Start/Stop
-
Input / Output
Processing Box
Decision Box
Flow lines
Connector, Off-page connector
4. Define membership operators.
Ans. The in and not in operators in Python checks the element present or not present in the
sequence. They are called Membership operators which are used in any loop to create and
iterate the sequence.
5. What is an infinite loop?
Ans. Infinite loop are the loops in which the test expression in the while or for loop never
become false and loop keeps on repeating the block of statements infinitely. Such a loop
never ends.
6. Write the difference between the ‘else’ and ‘elif’ statements.
Ans. else statement block executed when if statement expression becomes false whereas
elif statement block check for another expression when if statement expression becomes
false.
7. What are nested loops?
Ans. A loop inside a loop is called a nested loop.
8. Write the syntax for a nested ‘if-else’.
Ans. if <condition>:
Statement(s)
……….
else:
statement(s)
……….
B. Long answer type questions.
1. Write algorithms to display how a traffic signal works.
Ans.
1. Initialize Timer Variables:
Set greenTime = 60 seconds
Set yellowTime = 5 seconds
Set redTime = 55 seconds
2. Start Traffic Signal Loop:
While true, repeat steps 3 to 7.
3. Green Light:
Display "Green Light - Go"
Wait for greenTime seconds
4. Yellow Light:
Display "Yellow Light - Slow Down"
Wait for yellowTime seconds
5. Red Light:
Display "Red Light - Stop"
Wait for redTime seconds
6. End of Cycle:
Return to step 3
2. Explain ‘if-else’ statement with the help of a flowchart.
3. What is the difference between a division operator and a floor division operator?
Ans. Division operator divides the left operand by the right operand and gives quotient
whereas Floor Division operator divides the value and then truncates the quotient value
after decimal point. It returns only the integer number.
4. Describe the use of the input() and print function().
Ans. The input() function is used to take input from the user. It accepts the input from the
console as one string argument.
The print() function prints the message or value. It converts the message or value into a
string before writing it on the screen. It can have multiple parameters or arguments.
5. What is the difference between ‘for’ loop and ‘while’ loop? Explain with the help of
a flowchart.
Ans. The for loop is used when you are sure about the number of times a loop body will
be executed. It is also known as a definite loop.
The while loop in Python executes a set of statements based on a condition. If the test
expression evaluates to true, then the body of loop gets executed Otherwise, the loop stops
iterating and comes out of the loop body.
6. What are Python nested-if statements? Explain with an example.
Ans. Nested-if statements means an if statement inside another if statement, i.e. an if body
has another if-else in its body. The inner if block condition executes only when outer if
block condition is true.
Example:
num = 36
print ("num = ", num)
if num % 2 == 0:
if num % 3 == 0:
print ("Divisible by 3 and 2")
print("....execution ends....")
7. What is the difference between a tuple and a list data type?
Ans. The only difference between a tuple and a list is that a tuple is a read-only data
structure. It does not allow you to modify the size and value of the items of a tuple. That is
why, it is known as an immutable data type. However, you can easily modify the elements
in a list. That is why, it is known as a mutable data type.
8. What is typecasting? Explain its types.
Ans. The process of converting the value of one data type(int, float, string etc) to another
is called Type casting or type conversion.
Python supports two types of conversion:
i) Implicit type conversion: It is the automatic conversion of one data type to another
by Python, which simplifies programming.
ii) Explicit type conversion: This type of typecasting is done manually i.e. you can
convert the data type of one object to required type using int(), float(), str()
functions.
9. Write a program to find numbers that are divisible by 7 and multiples of 5 between
1200 and 2200.
Ans. for num in range(1200,2201):
if num%7 == 0 and num%5 == 0:
print(num, end=" ")
10. Write a program to input the monthly income of an employee between 40 and 60
years old and calculate the annual income tax on the basis of the following:
Tax Slab Rates
3 lakhs NIL
3 lakhs to 5 lakhs 5.00%
5 lakhs to 10 lakhs 20.00%
10 lakhs and more 30.00%
Ans. tax=0
name=input("Enter name :")
age=int(input("Enter age:"))
sal=int(input("Enter annual salary:"))
if(age>60)or (age<40):
print("Wrong Category!")
else:
if sal<=300000:
tax=0
elif sal<=500000:
tax=sal*5/100
elif sal<=1000000:
tax=sal*20/100
elif sal>1000000:
tax=sal*30/100
print("Name :",name)
print("Age :",age)
print("Tax Payable =₹",tax)