Python Variables

Python variable is a place in the computer's memory where you can store a single value. If you want to use a value later in the program, you can save it inside a variable. Like other programming languages, Python has no special command for declaring a variable. A variable is created the first time a value is stored inside it.

Example:

  1. num = 7
  2. char = "Hello"
  3. print(num)
  4. print(char)

Output:

7
Hello

Variable Names

You can use a variable name anything as long as it obeys the following rules:

  • It can be only one word
  • It can only use letters, numbers and the underscore(_) character
  • It can not start with a number

Variable names are case-sensitive, meaning that Hello, hello, HELLO are three different variables. But Python convention is to start a variable name with a lowercase letter.


Multiple Assignment of Variables

Python allows you to multiple assignment of variable. You can assign values to multiple variables in a single line.

Example

  1. a, b, c = 15, 17, 19

You can also assign a single value to several variables simultaneously.

Example

  1. a = b = c = 7