+ 11
Question on python lists and boolean logic and the next function
que. 1--> a = [1,2,3] b = a a.append(4) print(a) #output : [1,2,3,4] print(b) #output : [1,2,3,4] a = a + [5] print(a) #output : [1,2,3,4,5] print(b) #output : [1,2,3,4] why did that happen? que. 2--> print(bool([()]) #output : True print(bool(([])) #output : False why the bool of ([]) is False? que. 3--> what is the next() func? What's it's use? please answer if you know any single one of them.
4 Respuestas
+ 7
Let me go backwards.
3.) next gets you the next element from a generator object/iterator.
For loops get an iterator of any collection you loop over, and automatically call next on it until nothing's left.
2.) Empty iterables are False, filled ones are true.
[()] is a list with a tuple in it, so it's true.
([]) however is just an empty list, so it's false.
You probably wanted to put a list in a tuple, but tuples with one element are written like this:
([],)
Otherwise the parentheses will just be dissolved, like when you write (1+2)*3.
Only the comma makes the tuple.
1.) b=a creates a new reference to the object.
a = a + [5] on the other hand first makes a new list of a and [5], then gives this new list the name a.
So the name a is stripped off the old object (b) and pasted onto a new one.
For details I want to link you to a tutorial about references I made that should leave close to no confusion undissolved.
https://code.sololearn.com/c89ejW97QsTN/?ref=app
If it's *not* enough, please ask.
+ 5
1) append is used to add one element to existing list.
In the first condition the value of b=a
so in first condition a and b are having same elements
+ 1
Ans 2. bool([()] )is a list having an element which is an empty tuple
,Whereas bool([]) is an empty list...
0
heri secondary school tt hiyo kwa hiyo kama hivyo