Go to: Na-Rae Han's home page  

Python 2.7 Tutorial

With Videos by mybringback.com

#4: Variables Assignment

                          << Previous Tutorial           Next Tutorial >>
On this page: variables, variable assignment statement using =, incrementing an integer variable with +=, valid Python variable names.

Get Started

Video Summary

  • Interactive shell output for this tutorial is found here.
  • To set the value of a variable, type the character(s) you want followed by "=" and then the desired value. Ex: x = 5. The variable "x" will now have the value of "5" until specified otherwise.
  • Non mathematical values, such as a string of characters, are also allowed in Python. Ex: x = 'red'. if this line is entered, x will have a value of 'red' until specified otherwise. In addition, longer variable names are allowed in Python, so assignments such as red = 13 and _typ = 'red' are also valid.
  • To increment the value of a variable, type the variable followed by "+=" and then the amount you wish to add, Ex: x += 1. If the value of "x" was originally at 5, the new value will now be 6.

Learn More

  • Python variable names can contain any alphabetic characters (A-Z and a-z), digits (0-9), and the underscore character "_". There are some caveats and restrictions:
    1. Uppercase and lowercase letters are distinct. Therefore, Number and number are two different variables.
    2. A digit cannot start a variable name. Therefore, student1 is a valid variable, while 1student is not.
    3. Built-in keywords and function names shouldn't be used as a variable name. For example, print is obviously a Python keyword, therefore you shouldn't be using it as your variable name.

Explore