Chapter -8 (More with 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.
To understand this chapter , please read previous chapter link here:-chapter-7- list
Example-1: Some important expression
Example-2: Write a program and choose a random word from the list.
Code:
from random import choice
names= ['Python', 'World', 'India', 'Hello']
player= choice(names)
print(player)
Result:
World
If you restart the shell then it show different result.
Hello
Explanation:- As similar to randint it remains same. In this we import choice instead of randint.
Example-3: Write a program and choose two random word from the list.
Code:
from random import sample
names= ['Python', 'World', 'India', 'Hello']
player= sample(names,2)
print(player)
Result:
['Hello', 'India']
Explanation:- It is similar to previous example but here use sample and sample import more than 1 word at a time.
But choice import only 1 word at a time.
In the 3rd line you see (names,2) means import 2 word if you use here 3 then it import random 3 word from the list.
Example-4: Write a program and shuffle the list.
Code:
from random import shuffle
names= ['Python', 'World', 'India', 'Hello']
shuffle(names)
print(player)
Result:
['World', 'Python', 'India', 'Hello']
If you restart the shell then result change
['World', 'India', 'Python' ,'Hello']
Explanation:- Similar to previous example but here choose shuffle as per name it shuffle the content. But the difference is in 3rd line here use singly.
Example-5: Some important expression
Example-6: Write a program and convert the string into list.
Code:
s= 'Hi! This is a test.'
print(s.split())
Result:
['Hi!', 'This', 'is', 'a', 'test.']
Explanation:- Use the split command to convert all the word into the list.
< The main difference between the list and string. List occur in square bracket and all the word are in quotes, but string is in quotes>
Example-7: Write a program and remove the punctuation from the list.
Code:
s = 'Hi! This is a test.'
print(s.split())
from string import punctuation:
s=s.replace(c,'')
print(s)
Result:
['Hi!', 'This', 'is', 'a', 'test.']
Hi This is a test
Explanation:-First we convert string into list. Then import punctuation, then use replace command to convert punctuation into without space
<If you use (c,' ') then it give one more space between Hi and This.>
Example-8: Split '-' into list.
Code:
s ='1-600-291-8288'
print(s.split('-'))
Result:
['1', '600' ,'291', '8288']
Explanation:- In the 2nd line use split command and then in bracket '-' means split this and convert into list.
Example-9: Convert string into list.
Code:
word= input('Enter a word:')
x=list(word)
print(x)
Result:
Enter a word: python
['p', 'y', 't', 'h', 'o', 'n']
Explanation:- Use list command to convert string into list, You use split when it is in paragraph but if a word convert into list then use list.
Example-10 :Write a program and shuffle the letter.
Code:
from random import randint
word = input('Enter a word:')
letter_list= list(word)
shuffle(letter_list)
anagram=''.join(letter_list)
print(anagram)
Result:
Enter a word: python
hnptoy
If restart the shell
Enter a word: python
pytonh
Explanation:- When you give input, then input in form of string to use shuffle command you have to convert the string into list. You can see in previous example.
Example-11:Print the word which length is 3.
Code:
B= ['one', 'two', 'three', 'four']
for b in B:
if len(b)==3:
print(b)
Result:
one
two
Explanation:- In 1st line, it contain the list.
In 2nd line use for loop and here small 'b' use for one, two, three ,four. You can use any word here.
In 3rd line use len command if length of word in list is 3 then print the word.
Example-12: Create a two dimensional list.
Code:
L= [[i,j] for i in range(2) for j in range(2)]
print(L)
Result:
[[0,0] , [0,1] , [1,0], [1,1]]
Explanation:- Please memorise it, but careful here we square bracket not simple bracket if you simple bracket then it show error.
Example-13: Create a two dimensional list.
Code:
L=[]
for i in range(2):
for j in range(2):
L.append([i,j])
print(L)
Result:
[[0,0] , [0,1] , [1,0], [1,1]]
Explanation:- In first line we use empty list.
In 2nd line use for loop which use for row and again use for loop in 3rd line which is for column.
In 4th line we use append to add all these 2 dimensional list into main list.
Example-14: Create a list of 50 random number between 1 and 99.
Code:
from random import randint
L= [randint(1,100) for i in range(50)]
print(L)
Result:
[17, 9, 4, 17,29,36,98,3,59,87,2,89,.............]
Explanation:- Import randint and then use for loop to get the result. There are more many way to import into list.
Example-15: Square the number present in list.
Code:
L=[1,2,3,4]
L= [i**2 for i in L]
print(L)
Result:
[1,4,9,16]
Explanation:- This is another way to square the number present in list. You read another method in previous chapter.
Example-16: Print the number which is less than 5
Code:
L=[1,2,3,4,7,8]
x=len([i for i in L if i<5])
print(x)
Result:
4
Explanation:- Another way to count the number which is less than 5. You can see the previous method in previous chapter 7.
Example-17: Import 10 random number from the string.
Code:
from random import choice
alphabet= 'abcdefghijklmnopqrstuvwxyz'
s=''.join([choice(alphabet) for i in range(10)])
print(s)
Result:
ynbmasxywj
Explanation:- Result may be change because these are random alphabet from the string. In the 3rd line we use join because choice use in list and when we write in square bracket then it automatically convert into list and if we not use join then all these letter are written separate like ['y', 'n', 'b'......] to join them use join command.
Example-18: To flip the letter/word in two dimensional list.
Code:
L= [[1,2], [3,4], [5,6]]
x= [[y,x] for x,y in L]
print(x)
Result:
[[2,1], [4,3], [6,5]]
Explanation:- No explanation only memorise it.
Example-19: Print the specify word/letter in two dimensional list.
Code:
L= [1,2,3],
[4,5,6],
[7,8,9]]
print(L[1][0])
Result:
4
Explanation:- Here use first create two dimensional list.
In 2nd line use L[1] means in the second row and then [0] means in the first column. If you see 2nd row and 1st column then here 4 written that is the answer.
<It is similar to matrix which you read in 12 th class in Mathematics.>
Example-20: Print more than 1 word/letter in two dimensional list.
Code:
L= [1,2,3],
[4,5,6],
[7,8,9]]
for r in range(2):
for c in range(2):
print(L[r][c], end='')
print()
Result:
12
45
Explanation:- First we create a list.
In second line use for loop 'r' here we use (2) it means row i.e., first and second
In 3rd line again use for loop 'c' it means for column i.e., first and second column and then use end to combine them
<If you see first two row and first two column then you see 12 and 45 that the answer>
Example-21: Count the number in 2 dimensional list which complete divide by 2.
Code:
L= [1,2,3],
[4,5,6],
[7,8,9]]
count=0
for r in range(3):
for c in range(3):
if L[r][c]%2==0:
count=count+1
print(count)
Result:
4
Explanation:- First create list then use for loop in both loop use (3) it means three row and three column means complete list include in it. Then easily find only 4 number complete divide by 2.
Example-22: Count the number in 2 dimensional list which complete divide by 2.
Code:
L= [1,2,3],
[4,5,6],
[7,8,9]]
count=sum([1 for r in range(3) for c in range(3) if L[r][c]%2==0])
print(count)
Result:
4
Explanation:- Another way to find the number, same as previous example
Example-23: Flattened the two dimensional list.
L= [1,2,3],
[4,5,6],
[7,8,9]]
count=[j for M in L for j in M]
print(count)
Result:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Explanation:- Please memorise this code.
Example-24: Create 3 dimensional list.
Code:
L=[[[i]*3 for i in range(3)] for j in range(3)]
print(L)
Result:
[[[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 0, 0], [1, 1, 1], [2, 2, 2]]]
Explanation:- You understand easily if you understand previous topic.
No comments:
Post a Comment
If you have any doubt, let me know.