Naming Conventions
Officially, variable names in Python can be any length and can consist of uppercase and lowercase letters (A-Z, a-z), digits (0-9), and the underscore character (_). An additional restriction is that, although a variable name can contain digits, the first character of a variable name can’t be a digit.
Martin Breuss RP Team on April 7, 2020
Hello @jasonwilliams78, I am not quite understanding what you’re asking. I think you are referring to the previous video about the Python Pub Quiz?
a, b = 250, 250
for _ in range(250, 260):
if a is not b:
break
a += 1
b += 1
print(a)
Both a and b are being incremented in this for loop, as long as they have the same id(). As soon as they refer to two different objects, the if statement catches and the loop breaks, which means they don’t get incremented anymore.
However, a should not be 250 at the output. Maybe you have a bug somewhere in your code?
jasonwilliams78 on April 8, 2020
thanks Martin!, I understand now. But yes I have 250 for the output. Using Jupyter notebook. Will check over it again
Martin Breuss RP Team on April 9, 2020
I tested the code in a Jupyter notebook, since who knows, maybe notebooks handle caching differently, but my output is as expected.
Take a look at your indentation and make sure that it matches the example above.
If it still shows 250 as output, maybe you can publish your notebook on GitHub so I can give your code a tryout.
jasonwilliams78 on April 10, 2020
I figured out why the output wasn’t accurate. I had the a,b incrementors set inside the local variable namespace. Once I moved them up one level to the for loop scope I they were both able to increment correctly.
a, b = 250, 250
for i in range(250,260):
if a != b:
break
a += 1
b += 1
print(a,b)
260 260
jasonwilliams78 on April 10, 2020
Good evening, Martin. Can you tell me if I have a valid solution to this. My code is below the instructions.
1. Create a tuple data with two values. The first value should be the tuple (1, 2) and the second value should be the tuple (3, 4). 2. Write a for loop that loops over data and prints the sum of each nested tuple. The output should look like this: Row 1 sum: 3 Row 2 sum: 7data = ((1,2),(3,4)) for i in data: i = sum(data[0]) i = sum(data[1]) print(f”row 1 sum: {row1}”) print(f”row 2 sum: {row2}”)
jasonwilliams78 on April 10, 2020
data = ((1,2),(3,4))
for i in data:
i = sum(data[0])
i = sum(data[1])
print(f"row 1 sum: {row1}")
print(f"row 2 sum: {row2}")
Ganesh Kadam on May 1, 2020
a , b = 250, 250
for i in range(250,260): if a is not b: break a += 1 b += 1
print(a)
My output is : 257
Could someone please help here? Am I missing something?
Ganesh Kadam on May 1, 2020
a , b = 250, 250 print(id(a)) print(id(b))
print(a is b)
for i in range(250,260): if a is not b: break a += 1 b += 1
print(a)
Output:./test.py 139706676028128 139706676028128 True 257
Am I doing this right?
Martin Breuss RP Team on May 2, 2020
You’re doing it right @Ganesh. :)
kamjag on May 17, 2020
Hello @Martin Breuss I am really new in Python, When I run the below steps : a, b = 250, 250 print(id(a)) print(id(b)) for _ in range(250,260): if a is not b: break a += 1 b += 1 print(a)
OUTPUT is : 251
BUT when I run below steps: a , b = 250, 250 print(id(a)) print(id(b))
for _ in range(250,260): if a is not b: break a += 1 b += 1
print(a)
OUTPUT is : 257
Why am I getting diffrent results when my both steps are almost same except indentation in second steps in a += 1 b += 1
Kindly guide. Thanks in advance.
Martin Breuss RP Team on May 17, 2020
Hello @kamjag. It’s difficult to tell since your code formatting isn’t clear. You can use triple-backticks for proper code formatting, like so:
def hello():
print("hello")
If you post it again with proper formatting, I can take a closer look. But one thing to note is that indentation has meaning in Python. So whether a line of code is indented or not will make a difference in your code. Hope that helps!
Ajay on May 25, 2020
Got answer as 257 and know the exact reason behind this. Thanks martin for wonderful explaination.
a, b = 250, 250
for _ in range(250, 258):
print (f"Value of a {a}")
print (f"ID of a {id(a)}")
print (f"Value of a {b}")
print (f"ID of a {id(b)}")
if a is not b:
print (f"Value of a {a}")
print (f"Value of a {b}")
break
a += 1
b += 1
print (a)
**# Output
Value of a 250 ID of a 1385560464 Value of a 250 ID of a 1385560464 Value of a 251 ID of a 1385560480 Value of a 251 ID of a 1385560480 Value of a 252 ID of a 1385560496 Value of a 252 ID of a 1385560496 Value of a 253 ID of a 1385560512 Value of a 253 ID of a 1385560512 Value of a 254 ID of a 1385560528 Value of a 254 ID of a 1385560528 Value of a 255 ID of a 1385560544 Value of a 255 ID of a 1385560544 Value of a 256 ID of a 1385560560 Value of a 256 ID of a 1385560560 Value of a 257 ID of a 56217840 Value of a 257 ID of a 64785344 Value of a 257 Value of a 257 257**Become a Member to join the conversation.

jasonwilliams78 on April 7, 2020
Hello Martin, can you please explain why we have incrementors for a and b in the for loop that they are not being incremented why ‘a’ remains at 250 for the output ?