Python Classes and Objects | Looping | Adding | Removing | method push(), pop(), index(), remove() ,etc


Python Classes and Objects | Looping  | Adding  | Removing | method push(), pop(),index(),remove() 

Python Classes and Objects

Note: Python doesn’t have inbuilt support for Arrays, however Python list will be used instead.

Arrays

Arrays square measure wont to store multiple values in one single variable:

Example

Create associate array containing automotive names:


cars = [“Ford”, “Volvo”, “BMW”]

What is associate Array?
An array may be a special variable, which might hold over one price at a time.

If you’ve got an inventory of things (a list of automotive names, for example), storing the cars in single variables might seem like this:


car1 = “Ford”

car2 = “Volvo”

car3 = “BMW”

However, what if you wish to loop through the cars and notice a selected one? And what if you had not three cars, but 300?

The solution is associate array!

An array will hold several values below one name, associateyou’ll access the values by bearing on an indicator.

Access the weather of associate Array
You consult with associate array part by bearing on the indicator.

Example
Get the worth of the primary array item:

x = cars[0]

Example
Modify the worth of the primary array item:

cars[0] = “Toyota”

The Length of associate Array
Use the len() technique to come the length of associate array (the variety of parts in associate array).

Example
Return the amount of parts within the cars array:

x = len(cars)


Note: The length of associate array is often an extra than the very best array index.

Looping Array parts

You can use the for in loop to loop through all the weather of associate array.

Example
Print every item within the cars array:

for x in cars:
print(x)

Adding Array parts

You can use the append() technique to feature part to associate array.

Example
Add an extra part to the cars array:

cars.append(“Honda”)

Removing Array parts

You can use the pop() technique to get rid of part from the array.

Example
Delete the second part of the cars array:

cars.pop(1)

You can conjointly use the remove() technique to get rid of part from the array.

Example
Delete the part that has the worth “Volvo”:

cars.remove(“Volvo”)

Note: The list’s remove() technique solely removes the primary incidence of the desired price.

Array strategies

Python encompasses a set of inbuilt strategies that you simply will use on lists/arrays.

Method


Description

append()

Adds part at the top of the list

clear()

Removes all the weather from the list

copy()

Returns a replica of the list

count()

Returns the amount of parts with the desired price

extend()

Add the weather of an inventory (or any iterable), to the top of the present list

index()

Returns the index of the primary part with the desired price

insert()

Adds part at the desired position

pop()

Removes the part at the desired position

remove()
Removes the primary item with the desired price

reverse()

Reverses the order of the list

sort()
Sorts the list

Note: Python doesn’t have inbuilt support for Arrays, however Python Lists will be used instead.

Python Classes and Objects | Looping  | Adding  | Removing | method push(), pop(), index(), remove() 

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Domain & free Hosting

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.