Methods Of Python List !!

Methods Of Python List !!



In this article we will discuss about all methods of python lists.

Methods of lists are used to perform different operations in python.

The given below List of python methods for different purpose.

Methods


  • append() Method
  • insert() Method
  • clear() Method
  • copy() Method
  • sort() Method
  • count() Method
  • reverse() Method
  • remove() Method
  • len() Method
  • min() and max() Method

Python List


As we discussed in our previous article Python Lists is used for store the sequence data items,values of different datatypes.

It stores data in list as a array.

Values separated by the comma (,) and enclosed with [] brackets.

Index starts from 0 to n.

Program :


Output :

['khush', 'jinu', 'ashu', 'shwetu']
['a', 'b', 'c']
[1, 2, 3]
[1, 'khush']


Append() 


Append is used for insert the data in the list.

It insert data in the last of list.

Append is used the insert data without delete any item of existing list.

We can not insert at any number of index it add only in end of list.

Syntax : list.append()

Insert()


Insert is also used for insert data at any index in list.

when we insert data at any existing index then previous data is not delete only except number of index is changed.

Insert has two argument first is index number and second is data value.

Syntax : list.insert()

Program :


Output :

['khush', 'nishu', 'priti']
['khush', 'nishu', 'priti', 'jinu]
['khush', 'nishu', 'siya', 'priti', 'jinu]

In this example first we use append so, jinu add at the end of list.
second we use insert(2,"siya").so, at n[2] siya is stored and all priti is stored in next index.

Clear() 


clear() method is used for clear all data elements of the list.

list become null. it means there is list exist but no data item is available in the list.

Syntax : list.clear()

Program :


Output :

[1,2,3,4,5]
5
1
[]    ## when clear applied

as shown in above example there is still list exist and all data of list is clear.

Copy()


Python copy method is used to copy the elements of one list to the other list.

Syntax : list.copy() or list [:]

Program :







Output :

list l1 : [1,2,3,4]
after copy list l2 : [1,2,3,4]


Sort()


Sort() method is used for arrange the data of list in order and print in sorted form.

If we use string then it arrange according to alphabets order and if we use number then it sort by numbering.

Syntax : list.sort()

Program :


Output :

['khush' 'keri', 'ketan', 'khushi']
after sorting
['khushi', 'khush', 'keri', 'ketan']

as shown in above program string sorted alphabetically.

we can us also number for sorting or characters.

Count()


Count method returns how many times an element is stored in list.

Syntax : list.count()

Program :





Output :

count of 2 : 3

It returns 3 because in the list number 2 appears for 3 times.

Reverse()


Reverse() method use for store all data items of list in reverse order.

It means the first element store to the second elements stored in second last index and at end last element stored first.

Syntax : list.reverse()

Program :


Output :

['khush', 'keri', 'ketan', 'khushi']
after reverse :
['khushi', 'ketan', 'keri', 'khush']

as shown in program it just store in reverse order.

Remove()


Remove() method is used to remove data from the list.

we can remove with two method :
    1) Using data value
    2) By index number

In using data we directly remove by the value.

Syntax : list.remove("value")

In index number we use number of index for remove data.

Syntax : list.remove(list[number])

Program :


Output :

['khush,' 'nishu', 'priti']
['khush', 'priti']
['priti']

Len() Method 


Len() method gives length of list.

it gives how many number of data stored in list.

Syntax : len(list)

Program :


Output :

length is 5

Max() And Min() Method


This method is used to store the maximum element of the list.

Syntax : max(list)

This method is used to store the minimum element of the list.

Syntax : min(list)

Program :


Output :

['khush', 'keri', 'ketan', 'khushi']
khushi
ketan
maximum and minimum in number
[1, 2, 3, 4, 5]
5
1

As shown in above example it gives maximum and minimum value of both numbers and strings.

In this article we discussed about methods of python lists which is used to do different perations on python list.






Things You Should Know About Python Before Learning Python :


Python is an interpreted, high-level, general-purpose programming language. Developed by Guido van Rossum and first released in 1991,

Basic Programs Of All Python Methods


1) Program :


Python program to print Hello World.

# This prints Hello World on the output screen
print('NibKarma')

Output :


Hello NibKarma

List Programs

2) Program :



def string_length(str):
    count = 0
    for char in str:
        count += 1
    return count
print(string_length('NibKarma'))


Output : 8

Python Dictionary

3) Program :


def char_frequency(str):
    dict = {}
    for n in str:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict
print(char_frequency('NibKarma'))

Output :


{'a': 2, 'N': 1, 'i': 1, 'K': 1, 'r': 1, 'm': 1}

Python Tuple 

4) Program :

tuple=('a','b','c','d','e')
print(tuple)
del tuple[1]
print(tuple)

Output :


('a', 'b', 'c', 'd', 'e')

5) Program :



a = "Hello world"
print(a)
print(a[0])
print(a[1])
print(a[2])

x = "Hello"
y = "Khush"
print(x+y)

s = "Hello Khush"
print(s[0:2])
print(s[2:7])
print(s[4:])

Output :


Hello world
H
e
l
HelloKhush
He
llo K
o Khush

Python Pass

6) Program :


l=[1,2,3,4,5]
for i in l:
    if i==3:
        pass
        print ("Pass execute")
        print("Hello khush")
    print(i)

Output :


1
2
Pass execute
Hello khush
3
4
5

Python List

7) Program :



a1=["khush","jinu","ashu","shwetu"]
print(a1)
a3=['a','b','c']
print(a3)
a5=[1,2,3]
print(a5)
a6=[1,'khush']
print(a6)


a=['a','b','c','d','e']
print(a)
a[2]=3
print(a)
a[1:3]=1,2
print(a)
a[0:]=1,2,3,4,5
print(a)
del a[1]
print(a)

n=["khush","nishu","priti"]
print(n)
n.append("jinu")
print(n)
n.insert(2,"siya")
print(n)

n=["khush","nishu","priti"]
print(n)
n.remove("nishu")
print(n)
n.remove(n[0])
print(n)

n=[1,2,3,4,5]
print(n)
print("length is "+str(len(n)))

n=[1,2,3,4,5]
for i in n:
    print(i)

a=["khush","keri","ketan","khushi"]
print(a)
print("after sorting :")
a.sort()
print(a)

a=["khush","keri","ketan","khushi"]
print(a)
print("after reverse :")
a.reverse()
print(a)

a=["khush","keri","ketan","khushi"]
print(a)
n=max(a)
print(n)
n=min(a)
print(n)

print("maximum and minimum in numbers")

a=[1,2,3,4,5]
print(a)
n=max(a)
print(n)
n=min(a)
print(n)

a=[1,2,3,4,5]
print(a)
n=max(a)
print(n)
n=min(a)
print(n)
a.clear()
print(a)


Output :


['khush', 'jinu', 'ashu', 'shwetu']
['a', 'b', 'c']
[1, 2, 3]
[1, 'khush']
['a', 'b', 'c', 'd', 'e']
['a', 'b', 3, 'd', 'e']
['a', 1, 2, 'd', 'e']
[1, 2, 3, 4, 5]
[1, 3, 4, 5]
['khush', 'nishu', 'priti']
['khush', 'nishu', 'priti', 'jinu']
['khush', 'nishu', 'siya', 'priti', 'jinu']
['khush', 'nishu', 'priti']
['khush', 'priti']
['priti']
[1, 2, 3, 4, 5]
length is 5
1
2
3
4
5
['khush', 'keri', 'ketan', 'khushi']
after sorting :
['keri', 'ketan', 'khush', 'khushi']
['khush', 'keri', 'ketan', 'khushi']
after reverse :
['khushi', 'ketan', 'keri', 'khush']
['khush', 'keri', 'ketan', 'khushi']
khushi
keri
maximum and minimum in numbers
[1, 2, 3, 4, 5]
5
1
[1, 2, 3, 4, 5]
5
1
[]

Python Lambda

8) Program :


a = 5
b = 2

sum = a+b
print("sum is : "+str(sum))

a = 5
b = 2

sub = a-b
print("substraction is : "+str(sub))

a = 5
b = 2

mul = a*b
print("multiplication is : "+str(mul))

a = 5
b = 2

div = a/b
print("divison is : "+str(div))

a = 5
b = 2

mod = a%b
print("modulo is : "+str(mod))

Output :


sum is : 7
substraction is : 3
multiplication is : 10
divison is : 2.5
modulo is : 1

Python Break

9) Program :


a='khush'
print("without break statement")
for i in a:
    print(i,end='')
print()

print ("with break statement")
for i in a :
    if i=='u' :
        break
    print(i)

for i in range (1,11):
    if i==6:
        break
    print(i)

for i in range (1,11):
    if i==6:
        continue
    print(i)

Output :


without break statement
khush
with break statement
k
h
1
2
3
4
5
1
2
3
4
5
7
8
9
10

Python Class

10) Program :


class Student:   
    def __init__(self, name): 
        print("This is parametrized constructor") 
        self.name = name 
    def show(self): 
        print("Hello",self.name) 
student = Student("Nibkarma") 
student.show()

Output :


This is parametrized constructor
Hello Nibkarma

11) Program :


def func():
    print("def called")
func()

def func(name):
    print("hi "+ name)
func('khush')

def value(a):
    print("a is "+str(a))
value(10)

def func(name):
    print("name is :"+name)
s=input("enter name:")
func(s)

def func(name):
    msg=print("hi "+name)
    return msg;
name=input("enter name :")
func(name)

Output :


def called
hi khush
a is 10
enter name:khush
name is :khush
enter name :khush
hi khush

Python Data-types

12) Program :



x=2
y=3.4
z=False

print(x)
print(y)
print(z)


x = 2
y = 3
sum = x+y
print(sum)

a=int(input("Enter a:"))
b=int(input("Enter b:"))
print("\n")

sum = a+b
sub = a-b
mul = a*b
div = a/b
mod = a%b

print(sum)
print(sub)
print(mul)
print(div)
print(mod)

Output :


2
3.4
False
5
Enter a: 3
Enter b:4


7
-1
12
0.75
3

Simple Python Programs :


1) Program :


Python program to print Hello World.

# This prints Hello World on the output screen
print('Hello World')

Output :


Hello World

2) Program : 


For Getting input in Python by user.

str = input("Enter any string: ")
print(str)

Output :


Enter any string: NibKarma
NibKarma

3) Program : 


Python program to check if a number is positive or negative.

number = int(input("Enter number: "))

# checking the number
if number < 0: print("The entered number is negative.") elif number > 0:
    print("The entered number is positive.")
elif number == 0:
    print("Number is zero.")
else:
    print("The input is not a number")

Output :


Enter Number: 200
The entered number is positive.

4) Program :


Python program to check leap year.

year = int(input("Enter Year: "))

# Leap Year Check
if year % 4 == 0 and year % 100 != 0:
    print(year, "is a Leap Year")
elif year % 100 == 0:
    print(year, "is not a Leap Year")
elif year % 400 ==0:
    print(year, "is a Leap Year")
else:
    print(year, "is not a Leap Year")

Output :


Enter Year: 2016
2016 is a Leap Year

5) Program :


Python program to check whether the input character is an alphabet.

ch = input("Enter a character: ")
if((ch>='a' and ch<= 'z') or (ch>='A' and ch<='Z')):
    print(ch, "is an Alphabet")
else:
    print(ch, "is not an Alphabet")

Output :


Enter a character : N
N is an alphabet

6) Program :


Python program to check vowel or consonant.

ch = input("Enter a character: ")

if(ch=='A' or ch=='a' or ch=='E' or ch =='e' or ch=='I'
 or ch=='i' or ch=='O' or ch=='o' or ch=='U' or ch=='u'):
    print(ch, "is a Vowel")
else:
    print(ch, "is a Consonant")

Output :


Enter a character : I
I is a Vowel

The Reasons  For Why Writing Software Applications in Python


1) Readable and Maintainable Code
2) Multiple Programming Paradigms
3) Compatible with Major Platforms and Systems
4) Robust Standard Library
5) Many Open Source Frameworks and Tools
6) Simplify Complex Software Development
7) Adopt Test Driven Development

The Python Applications :


1) Web and Internet Development
2) Applications of Python Programming in Desktop GUI
3)  Science and Numeric Applications
4) Software Development Application
5) Python Applications in Education
6) Python Applications in Business
7) Database Access
8) Network Programming
9) Games and 3D Graphics
10) Console-based Applications
11) Audio – or Video- based Applications
12) Applications for Images
13) Enterprise Applications
14) 3D CAD Applications
15) Computer Vision
16) Machine Learning
17) Robotics
18) Web Scraping
19) Scripting
20) Artificial Intelligence
21) Data Analysis



You Also Can Search For Other Python Keywords Like :


Python , Python For Loop  , Python Loops  , Python Tutor  , Python Tutorial  , Python Dictionary  , Python Learning  , For Python List  , Python List  , Python Anaconda  , Python Download  , Python Range  , Python Regex  , Python Else If  , Python If Else  , Python Programming  , Python Snake  , Python Class  , Python Set  , Python In Function  , Python Function  , Python 3  , Python Pandas  , Python Random  , Python Array  , Python Requests  , Python Tuple  , Python Enumerate  , Python Write To File  , Python IDE  , Python Logger

 Python String  , Python While Loop  , Python Map  , Python Install  , Python Read File  , Python Code  , Python Flask  , Python Json  , Python Operator  , Python Or Operator  , Python Zip  , Python Compiler Online  , Python Online Compiler  , Python Online  , Python Replace  , Python Join  , Python Unit Testing  , Python Unittest  , Python Machine Learning  , Python With Machine Learning  , Python Certification  , Why Python For Machine Learning  , Python Interview Questions  , Python gui  , Python Reverse String  , Python Compiler

The Python Regular Expression  , Python Queue  , Python 2.7  , Python Yield  , Python Web Scraping  , Python IDE Online  , Python Django  , Python Scripting  , Python Numpy  , Python Multithreading  , For Python  , Python Or  , Python Generator  , Python For Windows  , Python 3.7  , With Python  , Python Documentation  , Python Version  , Python Beginners  , Python For Beginners  , Python Language  , Python List Functions  , Python Projects  , Python Data Structures  , Python Mysql  , Python Library  , Python And  , Python w3schools  , Python Modules  , Python Hello World  , Python Data Types  , Python 3.6  , Python Interpreter

Python Keyerror  , Python Ord  , Python Meaning  , Python Ordereddict  , Python Xrange  , Pythonanywhere  , Python Range Function  , Python For Data Analysis  , Python With Data sScience  , Python For Data Science  , Python Basics  , Python Interpreter Online  , Python Online interpreter  , Python Cheat Sheet  , Python Cheat Sheet  , Python vs Java  , Python Syntax  , Python Xml Parser  , Python Xml Parsing  , Python Join List  , Python Editor