Sunday, September 23, 2018

Python Ninja Bootcamp 20- List Comprehension

Learn Python like a Professional! Take You from 0 to Hero






In this Python lecture, we will learn about the List Comprehension.



"List comprehension is the concise & elegant way to create the list by using for loop".


List Comprehension in python        

Let's see some example-

# Create a list having square of a number from 0 to 5
l=[var**2 for var in range(0,6)]
print(l)
>> [0,1,4,9,16,25]


# create a list having even numbers from 0 to 10
l=[var for var in range(0,11) if var%2==0 ]
print(l)
>>[0, 2, 4, 6, 8, 10] 

# create list of name starts with the letter 'p' from the list given list 
l=['Tamta','Pandita','Alok','Sauru','Pakiya','Gaurav','Piku','Dheru','Pasha','Akshay','Shishir','Dharmu','Piyush']
print([var for var in l if var[0]=='P'])
>> ['Pandita', 'Piku', 'Pasha', 'Piyush']

# Garb the letter from the word 'hello and convert into uppercase
l=[var.upper() for var in 'hello']
print(l)
>> ['H','E','L','L','O']



EDITS ARE WELCOMED!!

In the next Blog, we will discuss Q&A 2nd 

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-21-q-second.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/channel/UCENc9qI7_r8KMf6-_1R1xnw

Instagram-
https://www.instagram.com/python.india/

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 




Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment