l = [(0,23,34), (2,34,23), (1,34,23)]
l.sort(reverse=False)
x = l.sort()
print(l)
print(x)
Output:
[(0, 23, 34), (1, 34, 23), (2, 34, 23)]
None
Why i am getting None for print(x)
l = [(0,23,34), (2,34,23), (1,34,23)]
l.sort(reverse=False)
x = l.sort()
print(l)
print(x)
Output:
[(0, 23, 34), (1, 34, 23), (2, 34, 23)]
None
Why i am getting None for print(x)
Hi, Himanshu.
Good question!
l = [(0,23,34), (2,34,23), (1,34,23)]
l.sort() # No need of writing reverse=False as by default sort will do in ascending order.
x = l.sort() # It is just a operations on the list.
y=sorted(l) # The return value of the sorted() is a modified new list.
print(l)
print(id(l))
print(x)
print(y)
print(id(y))
O/P :-
[(0, 23, 34), (1, 34, 23), (2, 34, 23)]
139852524836232
None
[(0, 23, 34), (1, 34, 23), (2, 34, 23)]
139852523979080
refer this : https://cloudxlab.com/assessment/displayslide/904/python-programming-sort-and-sorted?course_id=71&playlist_id=429
Hope this helped.
All the best!
Sort() sorts the array inplace on which it is called. It does not return anything.
Thank you. One more query.
t1 = tuple(β1,2,13,4,5β)
print(len(t1))
out: 10
please explain why 10 not 5
Also, in the below function what does mean by sum = sum + integer. Please explain:
def dict_tuple_func(list_of_integers):
sum = 0
d = dict()
for integer in list_of_integers:
if integer in d:
d[integer] = d[integer] + 1
else:
d[integer] = 1
sum = sum + integer
return (d, sum)
Hi, HImanshu.
Here you are passing a string to a Tuple so it is taking each elements as character wise even the two digits as separate one characters and β,β comma also characters.
print(t1)
(β1β, β,β, β2β, β,β, β1β, β3β, β,β, β4β, β,β, β5β) so length is 10.
You are inerate the integer(int) from loop for integer in list_of_integers:
this you are adding with sum already defined=0.
This will do the adding operations on all the elements.
Kindly try to run program and understand the operations by dry run it.
Also watch Sandeep sir Python video again, loops are explained very well.
All the best!
Thank you for your clarification.
x[1:4:2, 0:2, 0:3]
slicing with the help of above formula in numpy array, 1:4:2 refering to what⦠not able to understand
Please reply on above post
Hey Himanshu,
This has been discussed in details in numpy chapter.
Here are brief descriptions:
1:4:2
It slices array such that it start at position 1 till 4 and skipps 2 numbers. See this example:
[sandeep@cxln4 ~]$ export PATH=/usr/local/anaconda/bin:$PATH
[sandeep@cxln4 ~]$ python
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.array(range(10))
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> x = np.array(range(10))
>>> x[1:4:2]
array([1, 3])
>>>
x[1:4:2, 0:2, 0:3]
It slices the array in all three dimensions.
>>> a = np.array(range(27))
>>> a = np.array(range(27)).reshape((3,3,3))
>>> a
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
>>> a[0:2:2, 0:2, 0:2]
array([[[0, 1],
[3, 4]]])
>>>
i want to know that when we use definite loop in iterating over a file,then whenever we write word ,it iterate word by word and when we write line,it iterates line by line.
my doubt is that how the loop came to know that how it should iterate?
do it came to know by just defining the variable name?
Please see below examples.
In case of an array, it iterates element by element.
# Example of 1d array - each element is a number
>>> a = [1,2,3,4]
>>> for e in a:
... print(e)
...
1
2
3
4
# Example of 2d array - each element is another array
>>> a = [[1,2], [3,4]]
>>> for e in a:
... print(e)
...
[1, 2]
[3, 4]
If a string has multiple lines or multiple words, the for loop does not know. It only iterates character by character.
# Example of string - each element is a character of string
>>> a = 'a\nbcde'
>>> for e in a:
... print(e)
...
a
b
c
d
e
To iterate a string word by word, you can use split which by default splits on whitespace.
>>> a = 'word1 word2 word3'
>>> for e in a.split():
... print(e)
...
word1
word2
word3