Created
October 2, 2021 13:06
-
-
Save brianspiering/ca2370fd812855e6c8e4996bba56bbad to your computer and use it in GitHub Desktop.
Explore variable naming in Python solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solutions | |
sumTotal = 42 # Valid | |
# sum-total = 42 # Invalid. Dashes are a no-no. | |
sum_total = 42 | |
# sum total = 42 # Invalid. Spaces are a no-no. White space is often meaningful in Python. | |
sum_total = 42 | |
sum_total = 42 # This is valid. The best example because it follows pep8 style guidelines. | |
# sum = 42 # Valid but a terrible idea. Never name a variable or function with the same name as a built-in. | |
total = 42 # Better alternative. This is my preference for naming - clarity is primary and beverity is secondary. | |
# 2x = 42 # Invalid | |
x2 = 42 | |
# -4 = 42 # Invalid | |
neg_4 = 42 | |
# if = 42 # Invalid. Reserved keyword. | |
if_variable = 42 | |
# while = 42 # Invalid. Reserved keyword. | |
while_variable = 42 | |
Private = 42 # Valid | |
public = 42 # Valid | |
_static = 42 # Valid. Creates a "hidden" variable. | |
_4 = 42 # Valid. Creates a "hidden" variable. | |
_ = 42 # Valid. Creates a "hidden" variable. This is common for throw-a-away variables - for i, _ in enumerate(["one", "two", "three"]): print(i) | |
___ = 42 # Valid. More of the same "hidden" variable hack in Python. | |
# $16 = 42 # Invalid | |
dollars_16 = 42 | |
U$O = 42 # Invalid | |
USO = 42 | |
# 10% = 42 # Invalid | |
ten_percent = 42 | |
maçã_da_beira_alta = "🍎" # Valid. Python tries to be inclusive and support as many human languages as possible. | |
ಠ_ಠ = "hmmm…" # Valid. Source - https://twitter.com/jakevdp/status/1239979963920445441 | |
λ = 42 # Valid. It is a letter in an alphabet. In this case, the Greek alphabet. | |
# 🐶 = 42 # Invalid. Emojis are not letters in an alphabet. | |
puppy = 42 | |
# The criterion and a complete list of valid characters to start a variable with | |
# https://stackoverflow.com/questions/17043894/what-unicode-symbols-are-accepted-in-python3-variable-names |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment