Python
Python is a programming language.It's easy to use, learn and read.
Thursday, December 15, 2016
Python list
Python List
1st part
Today we discuss list. This topic is very important. When we
go to the real programming world, the list is our daily friend. So,
please ask anything if you don’t understand and write all the code
idle. Always write down all the code. Let’s get the start.
If you know c programming language or c++, then you know the name of
array. List and array are the same things. List is a basic data structure
of python. In my table, I have a book, pen, glass, water, laptop, phone.
Now I want to make a list all of these items and print these
items. The first question you asked how I store all the data in a
list. Give you an example.
>>> li = [‘lamp’,’laptop’,’cell phone’,’pen’,’stapler’,’water’]
>>> li
[‘lamp’,’laptop’,cell phone’,’pen’,’stapler’,’water’]
These all items are the string. We use square brackets [] to store all
these items. Do you want to ensure that variable li are a list?
Use type() method. If you want to see the type of li, then you saw the list.
>>> type(li)
<type ‘list’>
>>> len(li)
6
li length is 6. Because here are 6 items.
Python list index
python list index are start from zero(0). Array index also start
from zero(0).
>>> li[0]
‘lamp’
>>> li[1]
‘laptop’
>>> li[2]
‘cell phone’
>>> li [3]
‘pen’
>>> li [4]
‘stapler’
>>> li [5]
‘water’
>>>li[6]
it will give you indexError. List index out of range.
Because in my list, I have 6 item. But you give index number 6 to
access seven number item but seven number item does not exist in
your list.
Now, you ask me can I use negative number?. Yes, you can. Using
the negative number, you can access list item. See that:
>>> li[-1]
‘water’
Type every code. If you don’t code, just read. Then just get lost.
Get the fuck out of here. I don’t want to. Get lost, fucker!!!
when you give -1 it starts with last index. [-1] that always give
you last index value. let’s coding:
>>>li[-2]
‘stapler’
>>>li[-3]
‘pen’
>>>li[-4]
‘cell phone’
>>> li[-5]
‘laptop’
>>> li[-6]
‘lamp’
we can store anything in list not only string. Another Example:
>>> li2 = [‘Bangladesh’, 100, 3.5, ‘Laptop’]
>>> li2
[‘Bangladesh’, 100, 3.5, ‘Laptop’]
what types of items in the list?
>>> type (li2[0])
<type ‘str’>
>>>type(li2[1])
<type ‘int’>
>>> type (li2[2])
<type ‘float’>
so, you can store different types of item in the list.
Now, if we want sublist from the list, you can do that. Example:-
>>> list3 = [“joy”, 23, ”bangladesh”, “Harry Potter”]
>>> list3
[“joy”, 23, ”Bangladesh”, “Harry Potter”]
if we want first three item from list3 then we use slice method.
>>> list3[0:3]
[‘joy’,23,’Bangladesh’]
if I give list3[1:3], what we found?
>>> list3[1:3]
[23,’Bangladesh’]
Now if I give list3[-1:] then -1 to end means ‘Harry Potter’ to ‘Harry Potter’.
>>> list3[-1:]
>>> list3[-1:1]
>>>list3[:-1]
>>>list3[0:4]
>>> list3[0:4:2]
Here two indicate that it increment by 2.
>>> list3[0:4:1]
>>> list3
>>> list3[::1]
>>>list3[::2]
>>>list[::-1]
That’s enough for today. See you soon. Eat, sleep and code.
Tuesday, December 13, 2016
Subscribe to:
Posts (Atom)