This question already has answers here:
Why does adding a trailing comma after an expression create a tuple?
(6 answers)
Closed 1 year ago.
I am learning python where i come across below scenario
what actually happening internally in python can someone brief
My code :
a=10,
print(a)
Output coming as :
(10,)
a=10, is a tuple:
You can easily determine the type of the a using the type function as follows:
type(a)
Use a[0] to access the value 10:
a=10,
print(a[0])
Related
This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 1 year ago.
a = '/dvi/abcbbb'
if ('/dev/' in a) or ('/dv/' in a) or ('/dvi/' in a):
print(a)
/dvi/abcbbb
Can we do it without the OR statements in Python ?
You can reverse the check:
if os.path.dirname(a) in ["/dev", "/dv", "/dvi"]:
print(a)
This question already has answers here:
Call methods of a number literal [duplicate]
(1 answer)
Accessing attributes on literals work on all types, but not `int`; why? [duplicate]
(4 answers)
Closed 1 year ago.
If I am doing this
>>> 5.bit_length()
I am getting SyntaxError: invalid syntax
but runs fine when using space
>>> 5 .bit_length()
Result -> 3
How does it reads the space ?
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.
This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.
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))