Post

Python Coding
Python Coding@clcoding·
What is the output of following Python Code?
Python Coding tweet media
English
16
16
152
18.9K
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding Answer: 60 + 35 = 95 (each character on a separate line) Solution: This question becomes easy once you understand `in` operator. So let's start there. The `in` operator is used to check membership. Given 2 values x and y, x in y is either True or False +
English
1
0
6
856
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding x in y is True if `x` is *contained in* `y`. Else, False. The exact meaning of *contained* depends on the types of operands. In the case that `x` and `y` are str values, x in y returns True only if x is a substring of y. What does that mean? It means that +
English
1
0
0
137
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding that the string x occurs at least once in the string y. Examples: 'na' in 'banana' : True 'zzz' in 'banana' : False 'bnn' in 'banana' : False That's all we need to understand about `in` usage with strings Now, `not in` simply inverts the condition. +
English
1
0
2
114
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding If the `in` result is True, `not in` result is False and vice versa Now we are ready to solve the quiz equation = '160 + 135 = 295' creates a string named `equation`. Note that these contents of string are treated as characters, not numbers. Then the for-loop +
English
1
0
0
86
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding for symbol in equation: ... loops over the characters in `equation`, taking each character `symbol` in the iterations. What happens in this loop? if symbol not in '12': ... Let's think for which values of symbol can this condition be False +
English
1
0
0
69
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding Any value that is a substring of "12" will lead to False condition. So, those possibilities are: "" (empty string) '1' '2' "12" But recall that `symbol` is always a single character from `equation`. So, only the possiblities of '1' and '2' are relevant. Thus, +
English
1
0
0
58
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding the condition is False for `symbol` value '1' and '2', True for everything else. Now let's see what happens if the condition is True. print(symbol) Putting together our analysis so far, if `symbol` is not contained in "12" it is printed In other words, if `symbol` is either +
English
1
0
0
59
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding '1' or '2', it won't be printed. Else, printed. So, the code checks each character in `equation`, and prints it if it is neither '1' nor '2'. That's a good simplification. Let's now quickly look at `equation`, and crack it. Characters other than '1' and '2' will be printed +
English
1
0
0
63
Coding Computing Coach
Coding Computing Coach@CodingComputing·
@clcoding Equation being "160 + 135 = 295" if we mentally remove '1's and '2's, what remains is 60 + 35 = 95 So these are the characters that get printed. But note that each call to `print` prints on a separate line. So it's NOT going to look like an equation 60 + 35 = 95 But +
English
1
0
1
112
แชร์