I have several questions regarding to following PYTHON code segments. -


*****actually question how python automatically assign values 'i' in following different python code segments.

data=[5,1,23,10] datacount=len(data) print ('value of datacount is',datacount) in range(datacount-1):     print ('value of is',i)     k in range(i,datacount):         print ('value of k is', k,'value of is', i, 'value of datacount is', datacount)         print(data)         if data[i]>data[k]:             temp=data[i]             data[i],data[k]=data[k],temp in range(datacount):     print(data[i]) 

the result is:

>>>  ('value of datacount is', 4) ('value of is', 0) ('value of k is', 0, 'value of is', 0, 'value of datacount is', 4) [5, 1, 23, 10] ('value of k is', 1, 'value of is', 0, 'value of datacount is', 4) [5, 1, 23, 10] ('value of k is', 2, 'value of is', 0, 'value of datacount is', 4) [1, 5, 23, 10] ('value of k is', 3, 'value of is', 0, 'value of datacount is', 4) [1, 5, 23, 10] ('value of is', 1) ('value of k is', 1, 'value of is', 1, 'value of datacount is', 4) [1, 5, 23, 10] ('value of k is', 2, 'value of is', 1, 'value of datacount is', 4) [1, 5, 23, 10] ('value of k is', 3, 'value of is', 1, 'value of datacount is', 4) [1, 5, 23, 10] ('value of is', 2) ('value of k is', 2, 'value of is', 2, 'value of datacount is', 4) [1, 5, 23, 10] ('value of k is', 3, 'value of is', 2, 'value of datacount is', 4) [1, 5, 23, 10] 1 5 10 23 >>>  

consider following code

data = [5,1,23,10,-3] def fun(a):     i,c=1,a[0]     print ('value of is', i)     while i<len(a):         print ('the value of len(a) is', len(a))         if(a[i]>c):             c=a[i]             print ('the value of is',i ,'the value of c is',c)         i=i+1     return  print (fun(data)) 

result is:

>>>  ('value of is', 1) ('the value of len(a) is', 5) ('the value of len(a) is', 5) ('the value of is', 2, 'the value of c is', 23) ('the value of len(a) is', 5) ('the value of len(a) is', 5) 5 >>>  

at of above codes did not assign value 'i'. in first method takes value 0, second time takes value 1. ok, edit code follows

now not pass parameter method

def fun():     i,c=1     print ('value of is', i)      return  print (fun()) 

it gives following error

    >>>   traceback (most recent call last):   file "c:\users\e.s.kaushalya\desktop\tempory\kk2", line 8, in -toplevel-     print (fun())   file "c:\users\e.s.kaushalya\desktop\tempory\kk2", line 3, in fun     i,c=1 typeerror: unpack non-sequence >>>  

*****my question not understand how python assigning values 'i' in first 2 coding segments, , why python not assign value third coding segment?

let's start first one:

for in range(datacount-1):     print ('value of is',i) 

by doing for...in... loop, assigning value i.

try this

for in range(0, 10):     print 

then you'll understand.

the second one:

i,c = 1, a[0] 

what you're doing called tuple unpacking. speaking, make i = 1 , c = a[0]. check out python doc more details.

together third one, if way:

i,c = 1, 2 

you have i = 1 , c = 2.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -