DEV Community

SCDan0624
SCDan0624

Posted on

1

Python Variables

What Are Variables in Python?

Similar to variables in Javascript and other programming languages variables in Python are the names of containers that store values.

Creating a Variable

Variables in Python do not have a command for declaration, instead a variable is declared once you assign a value to it:

x = 15
y = "Tom"
car = 'Honda'

print(x) // 15 # prints 15
print(y) // Tom # prints Tom
print(car) // Honda # prints Honda
Enter fullscreen mode Exit fullscreen mode

As you can see above if you want to assign a string to a variable you can use either single or double quotes.

Changing Variable Values

To change a variable in Python you assign a new value to the same name:

x = 15
print(x) # prints 15

x = "basketball"
print(x) # prints basketball
Enter fullscreen mode Exit fullscreen mode

Variables are Case Sensitive

Just a quick note variable names in Python are case sensitive:

r = 2
R = "Bob"
#R will not overwrite a
Enter fullscreen mode Exit fullscreen mode

Variable Names Rules in Python

Variable names in Python have the following rules:

  • A variable name must start with a letter or underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • A variable name must be case sensitive (See example above)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay