Wednesday, December 18, 2019

Chapter - 7 (List)


For learn how to download and install python link here: How to download and install python in window


For learn how to run and execute the code in python link here:How to write and run the code


In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.



Example-1: Write the program and print the second number of list


Code:
list = [0,1,2,3,4,5,29,30,31,32,33,34,40]
print('The second element is', list[1])

Result:
The second element is 1

Explanation:- In the first line we first make the list. The list occur in square bracket not in simple bracket.
As like the string same code use in this use list[1] means it print the second number in list, if use list[3] then it print fourth number i.e.,3


Example-2: Write a program and find the number 2 is present in list.


Code:
list = [0,1,2,3,4,5,29,30,31,32,33,34,40]
if 2 in list:
    print('Your list contains the number 2.')

Result:
Your list contains the number 2.

Explanation:- In the first line simple first form the list.
Then in 2nd line use if command, as per name if 2 is exist in list then print it.

Example-3: Write a program and make list of 50 random number


Code:
from random import randint
L=[]
for i in range(50):
    x=L + [randint(1,100)]
    print(x)

Result:
[32]
[24].....


Explanation:- In the 1st line we call for random command.
In 2nd line we create empty list
In 3rd line we use for loop to create 50 number
In 4th line use randint which in between 1 and 100, also use 'x=L+randint(1,100)', in this L+ to add all the number in list.

Example-4: Some important expression


Example-5: Import list help


Code:
help(list)

Result:  Click on squeeze text then it show list.

To download the help list click here:-help list

Example-6: Write a program and square the number present in list


Code:
L=[1,2,3,4]
for i in range(len(L)):
     print(L[i]**2)

Result:
1
4
9
16

Explanation:- In the 2nd line we use for loop if you not use this then not, python not able to find the i command which is present in 3rd line ,and show error.

Example-7: Write a program and count the number which above 50.


Code:
L=[1,2,3,4,8,9,10,51,56,57,95]
count=0
for item in L:
    if item>50:
        count=count+1
print(count)

Result:
4

Explanation:- In first line create list
In 2nd line first the counting is 0
In 3rd line use for loop and then if command to see number greater than 50 then count 1.

Example-8: Write a program and play quiz and if the answer is correct then print correct


Code:
questions= ['What is the capital of India?']
print(questions)
z=input('enter your answer:')
x=z.lower()
if c=='delhi':
    print('Correct')

Result:
['What is the capital of India?']
enter your answer: Delhi
Correct

Explanation:- In first 2 line we add question
Then in 3rd line ask for input and then change the input into lowercase in next line, because if user write in uppercase then it not match with result.
In the 5th line use if input equal to delhi then the answer is correct.

Practice Questions


No comments:

Post a Comment

If you have any doubt, let me know.

How and Why you should learn python? About Python Programming Language Python is the programming language. By the help of Python you can ...