Python: one variable but different values? [duplicate] - python

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__)

Related

Python3 Sorted function not behaving as expected, getting different output [duplicate]

This question already has answers here:
Why does Python sort put upper case items first?
(3 answers)
Closed 3 years ago.
Why is this weird behaviour:
a = ['This','is','some','banana']
"_".join(sorted(a)).
Output -
This_is_banana_some
It should give the output -
is_banana_some_this
Am I missing something?
You need to specify the sorting key - lowercase str in your case.
"_".join(sorted(a, key=str.lower))
This works. By default python places uppercase first.

Python List as List Index [duplicate]

This question already has answers here:
Unpacking tuples/arrays/lists as indices for Numpy Arrays
(4 answers)
Closed 3 years ago.
Really not sure the right question to ask for this, but is it possible to have a list as the index of a list?
Ex:
pixelAddr=[50,50] # list
img[pixelAddr[0], pixelAddr[1]]=[255,255,255] # This is the way I know
# Is something like this possible? I get syntax errors when I try it...
img[*pixelAddr]=[255,255,255]
Btw, using python 3.7
when you do: img[pixelAddr[0], pixelAddr[1]] you are actually just re-packing the indices as a tuple so that is really all you need:
pixelAddr=(50,50) # NOTE THESE ARE ROUND PARENTHASIS
img[pixelAddr]=[255,255,255]
# or
pixelAddrList = [50,50]
img[tuple(pixelAddr)]=[255,255,255]

Removing Python characters from a list [duplicate]

This question already has answers here:
Slice every string in list in Python
(3 answers)
What is the purpose of the two colons in this Python string-slicing statement?
(1 answer)
Closed 4 years ago.
If i had a list as an example:
a = ['Hello_1.txt', 'Hello_2.txt']
In Python is it possible to somehow remove the first 5 characters ('Hello')
and the last 3 characters ('txt') from each of the items in the list ?
You could use a list-comprehension and string slicing:
[s[5:-3] for s in a]
which gives what you describe (not sure this is the neatest output though!)
['_1.', '_2.']

2 methods with the same name in python [duplicate]

This question already has answers here:
Python function overloading
(19 answers)
Closed 5 years ago.
In Python, could I write 2 methods having the same name but different number of parameters ?
em.on_create_experience(action.dest_id)
em.on_create_experience2(action.dest_id,0)
You can't write two separate methods with different parameter lists. But you can write one method with optional keyword parameters to do what you want.

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

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.

Categories

Resources