Python Forum

Full Version: define a variable using an if statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to define a variable using an if statement, for example:

a=0
b=1

if a = 0:
   c = b
 else:
   c = a
I know the above does not work, but is there a way of defining 'c' with an if statement or something similar?

Thank you
#this
x = 2
#is no different than this
if 4 > 3:
    x = 2
#or this
a = 3
if a % 2:
    x = 2
You can define variables anywhere you want in Python. Your example didn't work because you don't know the difference between set equal to "=" and is equal to "=="
a=0
b=1

if a == 0:
    c = b
else:
    c = a
Your code would work if you change the = to == in your if statements