0% found this document useful (0 votes)
15 views15 pages

ES Lecture 4

The document shows examples of using lists in Python. It demonstrates how to create lists, access elements, modify lists by adding/removing elements, check if an element is in a list, get the length of a list, concatenate lists, copy lists, and more. It also provides help documentation on the list type in Python and its common methods.

Uploaded by

Khadija Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

ES Lecture 4

The document shows examples of using lists in Python. It demonstrates how to create lists, access elements, modify lists by adding/removing elements, check if an element is in a list, get the length of a list, concatenate lists, copy lists, and more. It also provides help documentation on the list type in Python and its common methods.

Uploaded by

Khadija Faisal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

In [17]: L=[1,2,3,4]

In [18]: L
Out[18]: [1, 2, 3, 4]

In [19]: L=[1,3.6,'a']

In [20]: L
Out[20]: [1, 3.6, 'a']

In [21]: L[0]
Out[21]: 1

In [22]: L[2]
Out[22]: 'a'

In [23]: L[-1]
Out[23]: 'a'

In [24]: L=[1,5.8,'hello',2>3]

In [25]: L
Out[25]: [1, 5.8, 'hello', False]

In [26]: L[2]
Out[26]: 'hello'

1
In [27]: L[2][1]
Out[27]: 'e'

In [28]: L=[1,2,[4,6.9,'world'],5]

In [29]: L[2][2]
Out[29]: 'world'

In [30]: L[2][2][3]
Out[30]: 'l'

In [31]: L=[[1,2],[1.5,-1]]

In [32]: L
Out[32]: [[1, 2], [1.5, -1]]

In [33]: L=[0,'a',3,-1]

In [34]: L[1:2]
Out[34]: ['a']

In [35]: L[1:3]
Out[35]: ['a', 3]

In [36]: L=[[1,2],[1.5,-1]]

In [37]: L[0:1][0:1]
Out[37]: [[1, 2]]

2
In [38]: M=[L[0][0],L[1][0]]

In [39]: M
Out[39]: [1, 1.5]

In [40]: help(list)
Help on class list in module builtins:

class list(object)
| list(iterable=(), /)
|
| Built-in mutable sequence.
|
| If no argument is given, the constructor
creates a new empty list.
| The argument must be an iterable if
specified.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __delitem__(self, key, /)
| Delete self[key].
|
3
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See
help(type(self)) for accurate signature.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
4
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(self, /)
| Return a reverse iterator over the
list.
|
| __rmul__(self, value, /)
| Return value*self.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(self, /)
| Return the size of the list in
5
memory, in bytes.
|
| append(self, object, /)
| Append object to the end of the
list.
|
| clear(self, /)
| Remove all items from list.
|
| copy(self, /)
| Return a shallow copy of the list.
|
| count(self, value, /)
| Return number of occurrences of
value.
|
| extend(self, iterable, /)
| Extend list by appending elements
from the iterable.
|
| index(self, value, start=0,
stop=9223372036854775807, /)
| Return first index of value.
|
| Raises ValueError if the value is
not present.
|
| insert(self, index, object, /)
| Insert object before index.
6
|
| pop(self, index=-1, /)
| Remove and return item at index
(default last).
|
| Raises IndexError if list is empty
or index is out of range.
|
| remove(self, value, /)
| Remove first occurrence of value.
|
| Raises ValueError if the value is
not present.
|
| reverse(self, /)
| Reverse *IN PLACE*.
|
| sort(self, /, *, key=None,
reverse=False)
| Sort the list in ascending order and
return None.
|
| The sort is in-place (i.e. the list
itself is modified) and stable (i.e. the
| order of two equal elements is
maintained).
|
| If a key function is given, apply it
once to each list item and sort them,
7
| ascending or descending, according
to their function values.
|
| The reverse flag can be set to sort
in descending order.
|
|
--------------------------------------------
--------------------------
| Class methods defined here:
|
| __class_getitem__(...) from
builtins.type
| See PEP 585
|
|
--------------------------------------------
--------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from
builtins.type
| Create and return a new object. See
help(type) for accurate signature.
|
|
--------------------------------------------
--------------------------
| Data and other attributes defined here:
8
|
| __hash__ = None

In [41]: L=[1,2,3,'b']

In [42]: L.append(8.9)

In [43]: L
Out[43]: [1, 2, 3, 'b', 8.9]

In [44]: x='iba'

In [45]: x.capitalize()
Out[45]: 'Iba'

In [46]: x
Out[46]: 'iba'

In [47]: # list is mutable while strn is


immutable in python

In [48]: L
Out[48]: [1, 2, 3, 'b', 8.9]

In [49]: list.append(L,'q')

In [50]: L
Out[50]: [1, 2, 3, 'b', 8.9, 'q']
9
In [51]: M=L

In [52]: M
Out[52]: [1, 2, 3, 'b', 8.9, 'q']

In [53]: L.append('c')

In [54]: L
Out[54]: [1, 2, 3, 'b', 8.9, 'q', 'c']

In [55]: M
Out[55]: [1, 2, 3, 'b', 8.9, 'q', 'c']

In [56]: id(L)
Out[56]: 2057074238464

In [57]: id(M)
Out[57]: 2057074238464

In [58]: K=[1,2]

In [59]: id(K)
Out[59]: 2057077331264

In [60]: help(list.copy)
Help on method_descriptor:

copy(self, /)

10
Return a shallow copy of the list.

In [61]: L
Out[61]: [1, 2, 3, 'b', 8.9, 'q', 'c']

In [62]: M=L.copy()

In [63]: M
Out[63]: [1, 2, 3, 'b', 8.9, 'q', 'c']

In [64]: id(M)
Out[64]: 2057077365248

In [65]: id(L)
Out[65]: 2057074238464

In [66]: L.pop()
Out[66]: 'c'

In [67]: L
Out[67]: [1, 2, 3, 'b', 8.9, 'q']

In [68]: L.pop()
Out[68]: 'q'

In [69]: L
Out[69]: [1, 2, 3, 'b', 8.9]

11
In [70]: L=[1,2,3];M=[2,3,4]

In [71]: L+M
Out[71]: [1, 2, 3, 2, 3, 4]

In [72]: L
Out[72]: [1, 2, 3]

In [73]: L[2]='w'

In [74]: L
Out[74]: [1, 2, 'w']

In [75]: runfile('C:/Users/amajid/
untitled0.py', wdir='C:/Users/amajid')
the value of x is: 2

In [76]: runfile('C:/Users/amajid/
untitled0.py', wdir='C:/Users/amajid')

In [77]: runfile('C:/Users/amajid/
untitled0.py', wdir='C:/Users/amajid')
x is not 2

In [78]: runfile('C:/Users/amajid/
untitled0.py', wdir='C:/Users/amajid')
the value of x is: 2

In [79]: rem(6,3)
12
Traceback (most recent call last):

File "C:
\Users\amajid\AppData\Local\Temp\ipykernel_2
3444\2801924801.py", line 1, in <module>
rem(6,3)

NameError: name 'rem' is not defined

In [80]: %(6,3)
UsageError: Line magic function `%(6,3)` not
found.

In [81]: 6%3
Out[81]: 0

In [82]: 5%3
Out[82]: 2

In [83]: runfile('C:/Users/amajid/
untitled0.py', wdir='C:/Users/amajid')
plz enter an ineger5
the number 5 is not integer

In [84]: runfile('C:/Users/amajid/
untitled0.py', wdir='C:/Users/amajid')
plz enter an ineger: Traceback (most recent
call last):
13
File "C:\Users\amajid\Anaconda3\lib\site-
packages\spyder_kernels\py3compat.py", line
356, in compat_exec
exec(code, globals, locals)

File "c:\users\amajid\untitled0.py", line


14, in <module>
x=int(input('plz enter an ineger: '))

File "C:\Users\amajid\Anaconda3\lib\site-
packages\ipykernel\kernelbase.py", line
1175, in raw_input
return self._input_request(

File "C:\Users\amajid\Anaconda3\lib\site-
packages\ipykernel\kernelbase.py", line
1217, in _input_request
raise KeyboardInterrupt("Interrupted by
user") from None

KeyboardInterrupt: Interrupted by user

In [85]: runfile('C:/Users/amajid/
untitled0.py', wdir='C:/Users/amajid')
plz enter temperature: 32
Do you want to convert into F or C: F
the converted temp in F is 89.6
14
In [86]:

15

You might also like