Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "# list"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "List is a collection which is ordered and changeable. it is mutable."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "lists = [\"Micheal jackson\", 10.1, 1996]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['Micheal jackson', 10.1, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(lists)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "list"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(lists)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'Micheal jackson'"
- ]
- },
- "execution_count": 5,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "10.1"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists[1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1996"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists[2]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### variable types in a list "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "str"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(lists[0])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "float"
- ]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(lists[1])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "int"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "type(lists[2])"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### list can be listed in negative values."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1996"
- ]
- },
- "execution_count": 13,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists[-1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "10.1"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists[-2]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'Micheal jackson'"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists[-3]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### List can have tuples in them"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [],
- "source": [
- "list1 = [\"micheal jackson\", 10.1, 1996, ('a', 1)]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['micheal jackson', 10.1, 1996, ('a', 1)]\n"
- ]
- }
- ],
- "source": [
- "print(list1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### list slicing"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [],
- "source": [
- "list2 = [\"micheal jackson\", 10.1, 1996, \"mj\" ,1]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['micheal jackson', 10.1, 1996, 'mj', 1]\n"
- ]
- }
- ],
- "source": [
- "print(list2)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['mj', 1]"
- ]
- },
- "execution_count": 20,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "list2[3:5]"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### concatenate two list"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['Micheal jackson', 10.1, 1996]"
- ]
- },
- "execution_count": 21,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [],
- "source": [
- "list3 = lists + list2"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['Micheal jackson', 10.1, 1996, 'micheal jackson', 10.1, 1996, 'mj', 1]\n"
- ]
- }
- ],
- "source": [
- "print(list3)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### list extend"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['Micheal jackson', 10.1, 1996]"
- ]
- },
- "execution_count": 25,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {},
- "outputs": [],
- "source": [
- "lists.extend(['pop', 10])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['Micheal jackson', 10.1, 1996, 'pop', 10]\n"
- ]
- }
- ],
- "source": [
- "print(lists)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### List append"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [],
- "source": [
- "lists.append(['disco', 9])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['Micheal jackson', 10.1, 1996, 'pop', 10, ['disco', 9]]\n"
- ]
- }
- ],
- "source": [
- "print(lists)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### delete a element in list"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [],
- "source": [
- "del(lists[4])"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['Micheal jackson', 10.1, 1996, 'pop', ['disco', 9]]\n"
- ]
- }
- ],
- "source": [
- "print(lists)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### split the element in a list"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 36,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['hello', 'world!']"
- ]
- },
- "execution_count": 36,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "'hello world!'.split()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 38,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['A', 'B', 'C', 'D', 'E']"
- ]
- },
- "execution_count": 38,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "\"A,B,C,D,E\".split(\",\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 39,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'Micheal jackson'"
- ]
- },
- "execution_count": 39,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists[0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 40,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "['Micheal', 'jackson']"
- ]
- },
- "execution_count": 40,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "lists[0].split()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### list aliasing"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 41,
- "metadata": {},
- "outputs": [],
- "source": [
- "a = ['hard rock', 9.5, 1996]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['hard rock', 9.5, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {},
- "outputs": [],
- "source": [
- "b = a"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 44,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['hard rock', 9.5, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(b)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 45,
- "metadata": {},
- "outputs": [],
- "source": [
- "a[0] = 'banana'"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 46,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['banana', 9.5, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(a)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 47,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['banana', 9.5, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(b)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "list aliasing will allow both the variable 'a' and 'b' to have same element if changed"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### list clone"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 48,
- "metadata": {},
- "outputs": [],
- "source": [
- "A = ['hard rock', 9.5, 1996]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 49,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['hard rock', 9.5, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(A)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {},
- "outputs": [],
- "source": [
- "B = A[:]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 51,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['hard rock', 9.5, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(B)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 52,
- "metadata": {},
- "outputs": [],
- "source": [
- "A[0] = 'banana'"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 53,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['banana', 9.5, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(A)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "['hard rock', 9.5, 1996]\n"
- ]
- }
- ],
- "source": [
- "print(B)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### help in list"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 55,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Help on list object:\n",
- "\n",
- "class list(object)\n",
- " | list() -> new empty list\n",
- " | list(iterable) -> new list initialized from iterable's items\n",
- " | \n",
- " | Methods defined here:\n",
- " | \n",
- " | __add__(self, value, /)\n",
- " | Return self+value.\n",
- " | \n",
- " | __contains__(self, key, /)\n",
- " | Return key in self.\n",
- " | \n",
- " | __delitem__(self, key, /)\n",
- " | Delete self[key].\n",
- " | \n",
- " | __eq__(self, value, /)\n",
- " | Return self==value.\n",
- " | \n",
- " | __ge__(self, value, /)\n",
- " | Return self>=value.\n",
- " | \n",
- " | __getattribute__(self, name, /)\n",
- " | Return getattr(self, name).\n",
- " | \n",
- " | __getitem__(...)\n",
- " | x.__getitem__(y) <==> x[y]\n",
- " | \n",
- " | __gt__(self, value, /)\n",
- " | Return self>value.\n",
- " | \n",
- " | __iadd__(self, value, /)\n",
- " | Implement self+=value.\n",
- " | \n",
- " | __imul__(self, value, /)\n",
- " | Implement self*=value.\n",
- " | \n",
- " | __init__(self, /, *args, **kwargs)\n",
- " | Initialize self. See help(type(self)) for accurate signature.\n",
- " | \n",
- " | __iter__(self, /)\n",
- " | Implement iter(self).\n",
- " | \n",
- " | __le__(self, value, /)\n",
- " | Return self<=value.\n",
- " | \n",
- " | __len__(self, /)\n",
- " | Return len(self).\n",
- " | \n",
- " | __lt__(self, value, /)\n",
- " | Return self<value.\n",
- " | \n",
- " | __mul__(self, value, /)\n",
- " | Return self*value.\n",
- " | \n",
- " | __ne__(self, value, /)\n",
- " | Return self!=value.\n",
- " | \n",
- " | __new__(*args, **kwargs) from builtins.type\n",
- " | Create and return a new object. See help(type) for accurate signature.\n",
- " | \n",
- " | __repr__(self, /)\n",
- " | Return repr(self).\n",
- " | \n",
- " | __reversed__(...)\n",
- " | L.__reversed__() -- return a reverse iterator over the list\n",
- " | \n",
- " | __rmul__(self, value, /)\n",
- " | Return value*self.\n",
- " | \n",
- " | __setitem__(self, key, value, /)\n",
- " | Set self[key] to value.\n",
- " | \n",
- " | __sizeof__(...)\n",
- " | L.__sizeof__() -- size of L in memory, in bytes\n",
- " | \n",
- " | append(...)\n",
- " | L.append(object) -> None -- append object to end\n",
- " | \n",
- " | clear(...)\n",
- " | L.clear() -> None -- remove all items from L\n",
- " | \n",
- " | copy(...)\n",
- " | L.copy() -> list -- a shallow copy of L\n",
- " | \n",
- " | count(...)\n",
- " | L.count(value) -> integer -- return number of occurrences of value\n",
- " | \n",
- " | extend(...)\n",
- " | L.extend(iterable) -> None -- extend list by appending elements from the iterable\n",
- " | \n",
- " | index(...)\n",
- " | L.index(value, [start, [stop]]) -> integer -- return first index of value.\n",
- " | Raises ValueError if the value is not present.\n",
- " | \n",
- " | insert(...)\n",
- " | L.insert(index, object) -- insert object before index\n",
- " | \n",
- " | pop(...)\n",
- " | L.pop([index]) -> item -- remove and return item at index (default last).\n",
- " | Raises IndexError if list is empty or index is out of range.\n",
- " | \n",
- " | remove(...)\n",
- " | L.remove(value) -> None -- remove first occurrence of value.\n",
- " | Raises ValueError if the value is not present.\n",
- " | \n",
- " | reverse(...)\n",
- " | L.reverse() -- reverse *IN PLACE*\n",
- " | \n",
- " | sort(...)\n",
- " | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*\n",
- " | \n",
- " | ----------------------------------------------------------------------\n",
- " | Data and other attributes defined here:\n",
- " | \n",
- " | __hash__ = None\n",
- "\n"
- ]
- }
- ],
- "source": [
- "help(A)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python",
- "language": "python",
- "name": "conda-env-python-py"
- },
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.6.7"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement