Python Bytes: Conditional Variable Assignment

Veröffentlicht am: 13 Mai 2021
auf dem Kanal: CyberScribe.org
802
7

Python Bytes
Intermediate Python Topics
Conditional Variable Assignment

Topics beyond the basics of Python.

Conditional statements in Python

Handled with 'if' keyword.

variable = value if the conditional is met otherwise set to this value

If you have two environments, development and production:

dev = "dev"
prod = "prod"

script_arg = "dev"

if script_arg == prod:
db_env = prod
else:
db_env = dev

db_env = prod if script_arg == prod else dev

Example with True/False:
script_arg = True
db_env = prod if script_arg == True else dev

Example with testing the existence of something:
script_arg = None
db_env = prod if script_arg else dev

Useful for config files and also different switches and options for a script.

Should be used for relatively simple expressions so the line is still readable.


Auf dieser Seite können Sie das Online-Video Python Bytes: Conditional Variable Assignment mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer CyberScribe.org 13 Mai 2021 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 802 Mal angesehen und es wurde von 7 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!