Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does this python program print True

Tags:

python

x=True
def stupid():
    x=False
stupid()
print x
like image 996
Brian Harris Avatar asked Aug 07 '09 01:08

Brian Harris


People also ask

Why a '>' A is true in Python?

That is why, ('a' > 'b') is false and ('a' > 'A') is true. Save this answer. Show activity on this post. This is because on the ASCII (American Standard Code For Information Interchange) CHART the letter "a" equates to 97 (in decimal values) while the letter "b" equates to 98 (in the decimal values).

How do you change true to false in Python?

In Python, 1 denotes True , and 0 denotes False . So, the tilde operator converts True to False and vice-versa.

How do you return false in Python?

Python bool() function is used to return or convert a value to a Boolean value i.e., True or False, using the standard truth testing procedure.

How do you know if a Python is true or false?

You can check if a value is either truthy or falsy with the built-in bool() function. According to the Python Documentation, this function: Returns a Boolean value, i.e. one of True or False .


2 Answers

You don't need to declare a function-local variable in Python. The "x=False" is referring to an x local to stupid(). If you really want to modify the global x inside stupid:

def stupid():
    global x
    x=False
like image 123
Jonathan Graehl Avatar answered Oct 04 '22 21:10

Jonathan Graehl


To answer your next question, use global:

x=True
def stupid():
    global x
    x=False
stupid()
print x
like image 32
Greg Hewgill Avatar answered Oct 04 '22 20:10

Greg Hewgill