My code is not printing the executed code [duplicate] - python

This question already has answers here:
Python3 exec, why returns None?
(3 answers)
Closed 2 years ago.
print(exec("5 + 5"))
its not printing 10 but prints None instead. I'm sure exec basically executes a code.

exec returns None. You should probably use the eval() in this case

Related

Why are variables defined inside loop are accessible outside? [duplicate]

This question already has answers here:
What's the scope of a variable initialized in an if statement?
(7 answers)
Scoping in Python 'for' loops
(8 answers)
Closed 1 year ago.
for i in range(1, 10):
a =10
print(a)
It is giving me the output: 10
Expected output: Name is not defined
Can anyone please explain?

print function that prints variable names and their values [duplicate]

This question already has answers here:
Python: Print a variable's name and value?
(13 answers)
Closed 2 years ago.
For debugging I often want to print a variable out. I do this with for example:
print("cat_litter", cat_litter)
That is I print the variable name and its value. Is it possible to define a function to do this for me so I could call something like "printwithname(cat_litter)" for example.
No function needed! Python 3.8+ f-strings support this kind of printing directly:
foo = 1
bar = 2
baz = "Something else"
print(f'{foo=}, {bar=}, {baz=}')
https://docs.python.org/3/whatsnew/3.8.html#f-strings-support-for-self-documenting-expressions-and-debugging
Python 3.6 -> 3.7 Workaround:
Python: Print a variable's name and value?

Why does it show none at the end? Once i call the main function? How does the code need to be improved to not have it show none at the end? [duplicate]

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 3 years ago.
Once i call the main function I get none at the end. How does the code need to be improved to not have it show none in the end of the function call.
def count_spaces, then call main function to count number of spaces
You're printing the result of the count_spaces function. That function does not return anything (well, actually, a function that doesn't return anything returns None).
So when you do print(count_spaces()), you are printing None.

What is the Python class magic method for using is keyword? [duplicate]

This question already has answers here:
python: class override "is" behavior
(4 answers)
Is it possible to get a list of keywords in Python?
(7 answers)
Closed 4 years ago.
I know that in python we can use __eq__ method to check equality and __iter__ when using in keyword. Is there any method for is keyword?

Why is assert a statement in Python? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
design of python: why is assert a statement and not a function?
In Python 3, print was made into a function. What are the benefits of having assert be a statement?
For optimization. If you run your Python script with the -O option no code will be generated for assert statements. This would not be possible if assert were a function.
See the documentation on assert, which references this behavior.

Categories

Resources