List, Sort, Reverse, Adding, removing & modifying elements of List
PYTHON
COMMENT
Comments are an extremely useful feature in most programming
languages. Everything you’ve written in your programs so far is Python code. As
your programs become longer and more complicated, you should add notes within your
programs that describe your overall approach to the problem you’re solving. A
comment allows you to write notes in English within your programs.
How to
write comment
In Python, the hash mark (#) indicates a comment. Anything
following a hash mark in your code is ignored by the Python interpreter.
For example:
#This is comment line
LIST:
A list is a collection of items in a particular order. You
can make a list that includes the letters of the alphabet, the digits from 0–9,
or the names of all the people in your family. You can put anything you want
into a list, and the items in your list don’t have to be related in any
particular way.
In Python, square brackets ([]) indicate a list, and
individual elements in the list are separated by commas.
Example
Color=[‘blue’,’red’,’yellow’,’black’]
print(Color)
Run and See the output
Accessing
Element of List
print(Color[0])
output will be ‘blue’
Meanwhile you can apply methods here
print(Color[0].title())
output: Blue (First
letter of blue is capital now)
Python has a special syntax for accessing the last element in
a list. By asking for the item at index -1, Python always returns the last item
in the list:
print(Color[-1])
Output: black
This code returns the value 'specialized'. This syntax is
quite useful, because you’ll often want to access the last items in a list
without knowing exactly how long the list is. This convention extends to other
negative index values as well. The index -2 returns the second item from the
end of the list, the index -3 returns the third item from the end, and so
forth.
Try to compose this message to use only one value from the list
Color=[‘blue’,’red’,’yellow’,’black’]
Message=”I
have “+Color[1]+” Shirt”
Print(Message)
Changing,
Adding and Removing Elements
Modifying Elements in the list
Color=[‘blue’,’red’,’yellow’,’black’]
print(Color)
Color[0]=’violet’
print(Color)
Now Output Will Be : violet,
red, yellow, black
You can change the value of any item in a list, not just the
first item.
Adding
Elements to the list
Appending Elements to the End of the list
Mobile=[‘apple’,’techno’,’samsung’]
Print(Mobile)
Mobile.append(‘vivo’)
Print(Mobile)
Output: apple, techno,
samsung, vivo
You add in
Empty list
Car=[]
Car.append(‘BMW’)
Car.append(‘Audi’)
Car.append(‘Mercedes’)
print(Car)
output:
[‘BMW’, ‘Audi’, ‘Mercedes’]
Removing
Elements from the list
Removing an
items using the del statement
Bike=[‘honda’,’hero’,’suzuki’]
print(Bike)
del Bike[0]
print(Bike)
Output: [‘hero’,’suzuki’]
Removing
items using pop() Methods
Sometimes you’ll want to use the
value of an item after you remove it from a list. For example, you might want
to get the x and y position of an alien that was just shot down, so you can
draw an explosion at that position. In a web application, you might want to
remove a user from a list of active members and then add that user to a list of
inactive members. The pop() method removes the last item in a list, but it lets
you work with that item after removing it. The term pop comes from thinking of
a list as a stack of items and popping one item off the top of the stack. In
this analogy, the top of a stack corresponds to the end of a list.
Bike=[‘honda’,’hero’,’suzuki’]
print(Bike)
popped_Bike=Bike.pop()
print(Bike)
print(popped_Bike)
Output:
[‘honda’,’hero’,’suzuki’]
[‘honda’,’hero’]
[‘suzuki’]
You can pop any element using index number
Removing
Items by value
Bike=[‘honda’,’hero’,’suzuki’]
Bike.remove(‘hero’)
print(Bike)
Output:
[‘honda’,’suzuki’]
Sort()
Method
Python’s sort() method makes it relatively easy to sort a
list. Imagine we have a list of cars and want to change the order of the list
to store them alphabetically. To keep the task simple, let’s assume that all
the values in the list are lowercase.
cars =
['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
Output: ['audi',
'bmw', 'subaru', 'toyota']
You can also sort this list in reverse alphabetical order by
passing the argument reverse=True to the sort() method. The following example
sorts the list of cars in reverse alphabetical order:
cars =
['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
Again, the order of the list is permanently changed:
['toyota',
'subaru', 'bmw', 'audi']
Sorted()
for Temporarily
cars =
['bmw', 'audi', 'toyota', 'subaru']
print(cars)
print(sorted(cars))
print(cars)
Output:
['bmw', 'audi', 'toyota', 'subaru']
['audi', 'bmw', 'subaru', 'toyota']
['bmw', 'audi', 'toyota', 'subaru']
Finding the
Length of a List
You can quickly find the length of a list by using the len()
function. The list in this example has four items, so its length is 4:
cars =
['bmw', 'audi', 'toyota', 'subaru']
len(cars)
4
Printing
List in reverse order
cars =
['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
Output:
['bmw', 'audi', 'toyota', 'subaru']
[‘subaru’,’toyota’,’audi’,’bmw’]
Comments
Post a Comment