This question already has answers here:
Negative indexing in Python [duplicate]
(4 answers)
Closed 6 years ago.
str1 = "hello"
print(str1[-1])
The output of the program is o, but, shouldn't it give error as an output, as nothing exist at -1 index?
Negative indices in Python means they are relative to the end of the sequence. Which means -1 will give you the last, and -2 the second last, etc.
Or, if you prefer, you can think of the string as circular:
-3-2-1 0 1 2 3 4 5 6
...l l o h e l l o h e...
Related
This question already has answers here:
How does Python's comma operator work during assignment?
(3 answers)
Closed last month.
I am trying to understand how this comma separated solution works and how it is called, example:
b, c=0, 5
print(b,c)
>>0 5
Why is variable 'b' value 0?
Why is variable 'c' value 5?
Comma separated values are interpreted as tuple, which can be unpacked:
t = 0,5 # sames as t = (0,5)
print(type(t))
a,b = t
print(a,b)
Out:
<class 'tuple'>
0 5
This question already has answers here:
What do lambda function closures capture?
(7 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 3 years ago.
Let's say I want to create a list of functions programmatically, e.g. I want to create nine functions that, given a number in input, they add it respectively 1, 2, ... 9 and return it.
My take would be to write the following code:
>>> functions = []
>>> for i in range(1,10):
... functions.append(lambda x : x + i)
...
>>> for f in functions:
... print(f(0), end=' ')
However, the ouput is
9 9 9 9 9 9 9 9 9
Instead of the expected
1 2 3 4 5 6 7 8 9
I understand that this has to do with how Python deals with function arguments, i.e. when I create my lambda, I bound it to the reference of i, and not to its value.
How can I get around this? Is there a way to force Python to make a copy of i, instead of passing is reference?
This question already has answers here:
Find min, max, and average of a list
(5 answers)
Closed 5 years ago.
Let's say I have the below list:
o =[[-0.90405713, -0.86583093, -0.14048125]]
How do I find out how positive each element of o[0] is?
So,by looking at this I know that -0.14048125 is the most "positive" with respect to 0 on the number line. Is there a way to do this via a python code?
If you want the value closest to 0, you could use min with abs as key:
>>> o =[-0.90405713,-0.86583093,-0.14048125,3]
>>> min(o, key=abs)
-0.14048125
use max()
>>> o =[-0.90405713,-0.86583093,-0.14048125]
>>> max(o)
-0.14048125
This question already has answers here:
and / or operators return value [duplicate]
(4 answers)
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
It's just a little problem I have with logical condition in Python but :
What about a or b == m --> return true !
I'm going to explain with a piece of code
p = 58
m = 100
s = true
p or s == m #returns me 58
s or p == m #returns me 1
p or s # returns me 58
s or p # returns me 1
I can't explain this ! p or s is not equal to 100 .. And also the last 2 condition in the code ! might seem like a silly question but what why comparing a true and an int returns me an int ?
Sorry if I seem stupid ^^'
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python Ternary Operator
Does Python have an equivalent of the ternary operator?:
( x < 5 ? 1 : 0 )
Or must I express the same thing with an if-else pair?
You can use a conditional expression:
1 if x < 5 else 0
In code written for very old versions of Python, you may also see:
x < 5 and 1 or 0
However, the conditional expression form is preferred for Python 2.5 and later.
Python has:
1 if x < 5 else 0
or the old style:
x < 5 and 1 or 0