What does "if 2 < a < b: do someting" mean in Python? [duplicate] - python

This question already has answers here:
Simplify Chained Comparison
(2 answers)
Closed 2 years ago.
I am learning Python and trying to understand the following if statement. May I know what does it mean?
if 2 < a < b:
do something.
Thank you!

An if statement is a check of a condition and if this condition is true it will execute the code after the : .
In this case the Code prior to your line should have two variables a and b which both contain numbers.
If a is greater value than 2 and b is an even greater value than a, for example a is 3 and b is 4, or a is 10 and b is 200, the following Code will be executed. „Do something“ is just a place holder for the code that should be executed.
The best for you would be to search and read about if and if else statements for python to find some real life examples.

Related

Clarifying "if all(...)" line: Project Euler #7 - Python [duplicate]

This question already has answers here:
How do Python's any and all functions work?
(10 answers)
Closed 7 months ago.
Thanks to this video I managed to further optimize the time for my code for Problem 7 on Project Euler. However, as someone who's getting more familiar with Python, I want to understand line 105 in the image here. What is this line of code trying to do? Also, any suggestions on how to optimize this solution even more are welcome.
if all(x%i for i in primeList): ...
image of Problem 7 solution
Hi this line of code is checking if the new x value is divisible by each value on primeLisl. for example:
x=4
primes = [2,3]
list = [x%i for i in primes]
in this case list returns: [0, 1] (4 is divisible by 2 and not by 3)
when applying the all function checks if any value in the list is equal to 0 and in this case returns fales. If all the values of the list are diferent to 0 returns true.

Who can explain how this code is not an error? [duplicate]

This question already has answers here:
Recursive function in simple english [duplicate]
(6 answers)
Closed 11 months ago.
I was making some exercise to train myself and the exercise asked me to do a program that calculates fractals, very simple, i've done in about 1-2 minutes and it work, but looking at his solution it return x multiplicated by the function itself? how does this run? I know maybe it's a stupid question but i think it might be useful.
def fract(x):
if x == 0:
return 1
return x * fract(x - 1)
print(fract(int(input())))
Here's a walk through of whats going on.
First, you call fract(int(input())). The input method gets a response from the user and parses that to an int. and then calls fract on that int.
Say we enter 3. So our print statement evaluates to fract(3).
fract(3) returns 3 * fract(2)
fract(2) is called and that returns 2 * fract(1)
fract(1) is called and that returns 1
So putting it altogether and substituting function calls for what they return we get fract(3) returns 3 * (2 * (1)).

Why the result of “bag” > “apple” is True in Python? [duplicate]

This question already has answers here:
How are strings compared?
(7 answers)
Closed 2 years ago.
Why the result of “bag” > “apple” is True in Python?
I tried this code below i don't know why it show this result and how? Please some one explain it.
print("bag" > "apple")
True
I believe this is True because Python compares the first letter of each word. And b is greater than a in Python.

Basic OR difference [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
I was doing some tests, when I tried:
len(pin) == (4 or 6)
Half of the tests failed.
However with :
(len(pin) == 4 or len(pin) == 6)
All the tests were passed.
I am unable to understand the difference that is between these two.
pin is usually a number like 1234 or 12345.
That is because according to precedence rules, the right hand side expression is evaluated first in your first condition i.e.
len(pin) == (4 or 6)
Here, first (4 or 6) is evaluated and returns 4 (or true in some languages). Now, only those cases return true where length actually is 4.
Your second condition works as expected, because it compares the length to 4 and 6 both separately and then applies an or on both the boolean values.

Python min function tuples with arbitrary tuples [duplicate]

This question already has answers here:
How does tuple comparison work in Python?
(4 answers)
Closed 5 years ago.
What is this python code doing?
min((2,3),(6,'f',1))
Output: (2, 3)
I am not able to follow the documentation.
Can someone explain why the output in (2,3) and not an error?
Because (2,3) < (6,'f',1)
Meaning tuples are compared itemwise, therefore 2 < 6 yields that the first tuple is less than the second one
While this code works on Python 2 and Python 3, it should fail on Python 3 if both items in 1st place were the same. Because it would compare 3 to the string 'f' and such comparison is now invalid.
The min function will call the comparator methods of the objects you pass. In this case, all tuples. It is returning the minimum tuple with respect to lexicographic order.

Categories