a = [ ” Chandru ” , 1 , 456 , ” Python ” ]
List is a collection of the datatypes. The value denote the index 0, such that when we print the value a[0], it will gives the output as Chandru. Similarly when we print a[3], it will give the output as 456.
If we give the negative sign it will access the list in the backwise order.
So that when we print a[-1], it will give the output as “Python”. We can aslo search whether the word is present in the list.
>>> ” Chandru ” in a
True
>>> ” Linux ” in a
False
Program to print Power series
To write a program that prints series of 1+x/1+x**2/2!…+x**n/n.
n = int(input( ” Enter the number ” )
sum = term = n = 1
while n < 100:
term *= x / n
sum += term
n = n+1
if sum < 0:
break
print( ” Sum is = %d ” % sum )
When we execute this, the output will be
Enter the number 0
Sum is = 1
For loop
For loop in Python is not same as in c. It will automatically increment the value which is in the range.
a = [ ” Python ” , ” is ” , ” easy’ ]
for i in a:
print(i)
When we execute this program, first it will print the value ” Python ” which has the index 0 and followed by the next index. Similarly we can fix some range in which the for loop has to be executed.
Program to print Asterisk symbol in tringle manner :-
row = int(input(” Enter the number of rows “))
while row >=0:
print( “*” * row)
row = row – 1
The output of this program will be
Enter the number of rows 5
*****
****
***
**
*