Are there two conditions?

Suck a sandbox, but help
1. What is the condition if start: # What isin the code below?
string = "Let's show the application of sections in practice"
start = None
while start !="":
 start = input(f Type a number between 0 and {len(string) - 1}')
 if start: # What is this
 start = int(start) 
 end = int(input(f now enter a number from 1 to {len(string)},'
 f' well, or indefinitely, whatever...'))
 print(string[start:end])

If you give any number to the variable start, then the program works as it should.
And only when you type Enter, the program terminates without error.
2. Accordingly, what is transmitted when you enter a blank Enter?
3. What is an empty string, because it is neither False nor True nor None
y = ""
print(y == False) #False
print(y == None) #False
print(y == True) #False
April 3rd 20 at 17:44
2 answers
April 3rd 20 at 17:46
Solution
1. What is the condition if start: # What is it in the code below?

1. In General, the expression of the if variable: by default replaces approximately the following structure:
if (
 variable is not None # or anything
 and variable is not False # no lies
 and variable != 0 # no zero
 and variable != "# no empty string
 and variable != [] # or an empty list
 and variable != {} # or an empty dictionary
 and variable != tuple() # or an empty tuple
 and variable != set() # or an empty set
):

In lectures and literature, this is sometimes referred to as falsy -- a logical set of values for built-in types that in the expression if variable: equivalent to False. As an example, if my_list: to test for an empty list [] to write much better than if len(my_list) != 0:.

In your case, the condition is if start excludes from the operation of the nested code, as in an empty string "and None.

2. Accordingly, what is transmitted when you enter a blank Enter?

2. An empty string ", then "assigned" to the variable start. And as a condition of the execution of the loop while is start != "then this code input is empty string is the way out of the cycle.

3. What is an empty string, because it is neither False nor True nor None

3. The empty string " is of type str. However, see above about falsy values.

In General, the term if start cover and check if start != ":and a number of other tests described in paragraph 1.
Easy to understand, thanks) - Al commented on April 3rd 20 at 17:49
Excellent answer! - estefania_Magg commented on April 3rd 20 at 17:52
@orval_Brakus22,
In General, the expression of the if variable: by default replaces approximately the following structure:

This is an incorrect explanation, see documentation:
5df3020648c3b020422785.png
And therefore it is possible to do so:
5df3033d6912d802967704.png - Hunter94 commented on April 3rd 20 at 17:55
@Hunter94, wow! Thank you - Al commented on April 3rd 20 at 17:58
This is an incorrect explanation, see the documentation:

@Hunter94rather incomplete and it is possible to add.

The phrase "in General", "default" and "a logical value set for built-in types" clearly indicate that this is not an exhaustive explanation, and that it was about built-in types.

Custom classes with an overridden method __bool__() I 4 years of programming in Python is generally not useful. It only makes sense to test objects with complex state, which can be simplified to True or False. But I prefer to write separate methods.

Override the method __bool__() contrary to two things:

First, explicit is better implicit (see PEP 20). The developers of frameworks and libraries stubbornly avoid overriding this method to the public API, preferring to write methods like validate() and offering to call them explicitly (see validating HTML forms in Django).

Second, the principle of least surprise. A novice developer can get used to the default behavior when objects of custom classes is equivalent to True. Now imagine that a novice developer is required to derive your class from a class from a third-party library. But the developers of third-party libraries prefer to override __bool__() instead of write my class method validate_all_fields(). As a result, a novice developer from ignorance can curve had more bugs and kill more of their time on debugging.

In General, a novice developer it is useful to know how to write short terms. But changing behavior by default -- a very controversial practice. - orval_Brakus22 commented on April 3rd 20 at 18:01
April 3rd 20 at 17:48
1) if start equivalento if start is not None

2) an empty string, do print - XS is there a character transfer or no, I guess not

3) do print type(y) will see what type she is not a boolean (this is where TRU FALS) and non - is
1) wrong, it is equivalent to if bool(start) because for example "the" is not None and 0 is not None, however if 0 is not met. - estefania_Magg commented on April 3rd 20 at 17:51
@Al it is necessary to check before marking a decision. - Manuela_McClu commented on April 3rd 20 at 17:54
Thank you.
Still found what I was looking for. In the 3rd paragraph I was referring to the value of the object. Forgot that it checks the bool() - Al commented on April 3rd 20 at 17:57
@estefania_Magg, thanks, helped! - Al commented on April 3rd 20 at 18:00
@Al, you're welcome! - estefania_Magg commented on April 3rd 20 at 18:03

Find more questions by tags Python