* before iterable inside a print() in Python [duplicate] - python

This question already has answers here:
What does asterisk * mean in Python? [duplicate]
(5 answers)
Closed 5 years ago.
I have a list of names and I want to print each element of the list in a different line without a for loop. So, after some research I found this example: print(*names, sep='\n'), witch results in exactly what I want. But what does this * character before the list name means?

The * is used to unpack argument lists when calling a function. In this case it unpacks your list of names.

Related

How does comprehension list work in multiple variables? [duplicate]

This question already has answers here:
What does it mean to unpack in python?
(5 answers)
List comprehension rebinds names even after scope of comprehension. Is this right?
(6 answers)
Closed 5 months ago.
I was trying to make a code that allows multiple user inputs in different variables in only one line in Python. I've studied a bit of list comprehension, but I can't understand how the code below works.
a,b,c = [int(a) for a in input().split()]
Why is the output an integer value of a, b, and c if only a is being put into the loop?

Why can I change an element of a list within a tuple in Python? [duplicate]

This question already has answers here:
If in Python I put a list inside a tuple, can I safely change the contents of that list? [duplicate]
(2 answers)
Why can tuples contain mutable items?
(8 answers)
a mutable type inside an immutable container
(3 answers)
Closed 4 years ago.
I am new in Python and I need some clarity on this.
I read everywhere that tuples are immutable, so they cannot be changed, however I did this test, and I could change the values inside a list within a tuple, am I doing something wrong? Or why am I able to change this value? Thanks.
This is my test code:
>tuplelist = ('mouse',[1,2,3])
**('mouse', [1, 2, 3])**
>tuplelist[1][0]=999;
**('mouse', [999, 2, 3])**

What does [[]] do ? [duplicate]

This question already has answers here:
Why does this code for initializing a list of lists apparently link the lists together? [duplicate]
(1 answer)
Why does appending to one list also append to all other lists in my list of lists? [duplicate]
(4 answers)
Python: fastest way to create a list of n lists
(5 answers)
Closed 4 years ago.
I don't know python I am working through 'Exercises in Programming Style' and translating to javascript. I can understand most of the python but this line flabbergasts me .
# Let's use the first 25 entries for the top 25 words
data = data + [[]]*(25 - len(data))
some context: the challenge is to use self imposed memory limitations represented here by the data array. So here she just cleared out data that is no longer used to make room for the 25 most frequent word. What is she doing here ?

Python: one variable but different values? [duplicate]

This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
What does __all__ mean in Python?
(10 answers)
Closed 5 years ago.
I have a line of code from Python forbidden fruit module:
__all__ = 'curse', 'curses', 'reverse'
I know what strings are and I know what arrays and tuples are. What kind of variable is this? How can we use this and for what?
It's a tuple. If you want to find out the type of something, use the type function - e.g.
type(__all__)

Python += with a list and a tuple [duplicate]

This question already has answers here:
Why does += behave unexpectedly on lists?
(9 answers)
Why can't I add a tuple to a list with the '+' operator in Python?
(3 answers)
Closed 7 years ago.
I saw someone wrote an interesting python line online, but couldn't understand why it works. So we can try the following lines in python interpreter:
s=[1]
s=s+(1,-1)
This will result in an error "TypeError: can only concatenate list (not "tuple") to list". But if done in another way:
s=[1]
s+=(1,-1)
will result in s = [1,1,-1]
So I used to thought x=x+y is equivalent to x+=y, can someone tell me how they are different and why the second way works? Thanks in advance.
Instead of += use list.extend:
s = [1]
s.extend((1,-1))

Categories

Resources