Page 250 - Ai_V1.0_Class9
P. 250
Commands Output
#display the digits of a number in a reverse order Enter a number 594
num=int(input("Enter a number ")) 495
while num>0:
r=num%10
print(r, end="")
num//=10
Program 11: To print the sum of first five natural numbers.
Algorithm Flowchart
Step 1 Start START
Step 2 num=1
Sum=0, Num=1
Step 3 sum=0
Step 4 while num is less than 6
sum=sum + num Is No Display
num = num + 1 Num < 6? "Sum"
Step 5 Display "Sum is ", sum
Yes
Step 6 Stop
Sum = Sum + Num STOP
Source Code:
num=1
Num = Num + 1
sum=0
while num<6:
sum=sum+num
num=num+1
print("The sum of first five natural numbers is",sum)
Output:
The sum of first five natural numbers is 15
Lists in Python
A list in Python is a collection of items that are ordered and mutable (changeable). It can contain elements of
different data types, such as strings, integers, floats, objects, or even other lists. Each value in a list is called an
element, and each element is indexed based on its position in the list. The index starts at 0 for the first element,
1 for the second element, and so on. The number of elements in a list determines its length. Lists are defined by
enclosing the elements within square brackets [], with each element separated by a comma. For example,
my_list = [1, 2, 3, 'hello', 5.0]
Lists are mutable, meaning that you can change the elements in a list after it has been created. You can add
elements to a list, modify existing elements, or remove elements from a list.
248 Artificial Intelligence Play (Ver 1.0)-IX

