PyCodePro

Previously LP16 - A website that teaches Python programming language through interactive tutorials, practice exercises, and real-world projects.

Python Lists

List in Python


Hey Guys Welcome to our New blog post today we will discuss about the list in python and in the list we will learn many functions of the list and also learn how to use functions.



Python Lists



In Previous Blog Post


In previous blog post we discuss about the DML - Data Manipulation Language in which we learn various commands so if you want to learn SQL or DML commands You can visit Our previous Blog.

In Today's Blog


We will learn the list in python now without any delay let's start today's Blog. 

Contents -

  • List
  • Create a list
  • To access items
  • negative indexing
  • ranges of index
  • python for loops
  • looping through string
  • change item value
  • to check if item exist
  • list length
  • Add items
    • Using Append
    • Using Insert
  • Remove Items
    • Using Remove
    • Using Pop
    • Using Del
    • Clear
  • To Copy a list


List


List is a collection of data values under one name.

Create a List


To create a list we need to enter the values in square brackets and by also using single quotation or by using double quotation.

Mostly we use double quotation for entering the values.

For Example - There is a list of fruits and vegetables and we need to make a list in python, to make a list in python we use square brackets as shown below.

list=["apples","banana","cherry","kiwi"]

in the above example - 

list is a variable 

and we need to create a list so we use square brackets 

and the values are string so we use double quotation.

To Access the list items

we can access the list items by referring to index numbers.

list=["apples","banana","cherry","kiwi"]

to index the numbers in the list the index numbers start from zero (0).

In the above list the index number of "apple" is zero and the index number of "kiwi" is 3.

for example - 

list=["apples","banana","cherry","kiwi"]
print(list[2])

in this example the output is "cherry" because the index number of cherry is 2.

Negative Indexing

In short language the negative indexing means the beginning from end.

for example 

list=["apples","banana","cherry","kiwi"]
print(list[-1])

in this example the output is "kiwi"

Ranges Of Index

In short language it means that where to start and where to end range.

For example - 

list1=[1,2,3,4,5,6,7,8,9]
print(list1[2:5])

in this example the output is [3,4,5] we did not include 6 because in python steps one step less so that we did not include 6.

Python For Loops

for example 

list=["apples","banana","cherry","kiwi"]
for i in list:
    print(i)

in this example the output is

apple
banana
cherry
kiwi

as you understand this by above example.

Looping through string

for example -
for i in "banana":
    print(i)

in this example the output is 

b
a
n
a
n
a

as you understand this by above example.

To change item Value

for example - 

list=["apples","banana","cherry","kiwi"]
list[1]= "Black-current"
print(list)

in the above example after input the list we need to give the index number after the variable then under quotation we need to enter the changed value then print the list.

output of the above example is -

list=["apples","Blackcurrant","cherry","kiwi"]

To check if item exist in the list

To check if the item exist we need to use if statement of the python.

if you don't no the if statement you can read our blog post on if statement of python.

list=["apples","Blackcurrant","cherry","kiwi"]


if "apples" in list:
    print("yes, apples is in the list)

List Length

to determine the length of the list we need to use the len function.

len function is use to determine how many items are in the list.

for example - 

list=["apples","banana","cherry","kiwi"]
print(len(list))

To Add items in the list

  • Using Append Function

for example -

list=["apples","banana","cherry","kiwi"]
list.append("Orange")
print(list)

by using append function the item will be added in the last of the list

in the above example the output is - 

["apples","banana","cherry","kiwi","orange"]

  • By Using Insert

for example - 

list=["apples","banana","cherry","kiwi"]
list.insert(1,"orange")
print(list)

by using insert function we can add the item where we want to place by giving the proper indexing number.

in the above example the output is - 

['apples', 'orange', 'banana', 'cherry', 'kiwi']

To Remove Item

  • By remove

for example 


list=["apples","banana","cherry","kiwi"]

list.remove("cherry")

print(list)

in the above example the remove function will remove the particular item you write after the remove function.

  • By Pop
For Example -

list=["apples","banana","cherry","kiwi"]
list.pop( )
print(list)

pop will remove the item from the last 

in the above example the pop will remove the kiwi and print all items except kiwi.

  • By Del
For Example - 

list=["apples","banana","cherry","kiwi"]

del list[0]

print(list)

the del method will remove the item which is indexed after the del function.

in the above example the del function will remove apples and print all other elements.

  • By Del *
For Example

list=["apples","banana","cherry","kiwi"]
del list

in the above example the del function will delete all the elements if we did not give the index number.

there is no output of the above code.

  • Clear Keyword
For Example -

list=["apples","banana","cherry","kiwi"]
list.clear( )
print(list)

the clear keyword will clear all the elements of the list but the list exist.list is not permanently deleted by clear keyword.

Copy a list

For Example -

list=["apples","banana","cherry","kiwi"]
list2=list.copy( )
print(list2)

by using copy function you can copy a list but you need to apply copy function in the list.





IN THE NEXT BLOG WE LEARN MORE ABOUT THE TUPLES IN PYTHON.


THANK YOU FOR VISITING , KEEP LEARNING STAY TUNED FOR NEXT BLOG.


FOLLOW OUR BLOG FOR REGULAR UPDATES.


VIEW OUR PREVIOUS BLOG FOR THE BASIC CONCEPTS OF PYTHON AND OTHER BASIC CONCEPTS OF COMPUTER.


VISIT OUR SECOND BLOG –incredible2facts.blogspot.com


VISIT OUR YOUTUBE CHANNEL -  
Click Here


Our YouTube channel will give you the short motivational speeches of the great personalities in the present there is only seven motivational videos on channel but in future I will upload the motivational videos soon .


VISIT OUR INSTAGRAM PAGE –instagram.com/learnpython16


Our Instagram page is also a new page i just created few days ago but stay tuned I will upload the post very soon.


VISIT OUR TWITTER PAGE - 


Please support us by following our blog and by subscribing our YouTube channel and by following our Instagram page.








Python Lists Python Lists Reviewed by Learn Python on March 22, 2020 Rating: 5

No comments:

Powered by Blogger.