DEV Community

loading...

Python Variables

scdan0624 profile image SCDan0624 ・1 min read

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)

Discussion (0)

pic
Editor guide