Variables data shaped boxes - zamaniamin/python GitHub Wiki

The PEP 8 -- Style Guide for Python Code recommends the following naming convention for variables and functions in Python:

  • variable names should be lowercase, with words separated by underscores to improve readability (e.g., var, my_variable)
  • function names follow the same convention as variable names (e.g., fun, my_function)
  • it's also possible to use mixed case (e.g., myVariable), but only in contexts where that's already the prevailing style, to retain backwards compatibility with the adopted convention.

Take a look at the snippet

var = 1
print(var)

The first of them creates a variable named var, and assigns a literal with an integer value equal to 1.

You're not allowed to use a variable which doesn't exist (in other words, a variable that was not assigned a value).

Key takeaways

  1. A variable is a named location reserved to store values in the memory. A variable is created or initialized automatically when you assign a value to it for the first time. (2.1.4.1)

  2. Each variable must have a unique name - an identifier. A legal identifier name must be a non-empty sequence of characters, must begin with the underscore(_), or a letter, and it cannot be a Python keyword. The first character may be followed by underscores, letters, and digits. Identifiers in Python are case-sensitive. (2.1.4.1)

  3. Python is a dynamically-typed language, which means you don't need to declare variables in it. (2.1.4.3) To assign values to variables, you can use a simple assignment operator in the form of the equal (=) sign, i.e., var = 1.

  4. You can also use compound assignment operators (shortcut operators) to modify values assigned to variables, e.g., var += 1, or var /= 5 * 2.

  5. You can assign new values to already existing variables using the assignment operator or one of the compound operators, e.g.:

var = 2
print(var)

var = 3
print(var)

var += 1
print(var)

  1. You can combine text and variables using the + operator, and use the print() function to output strings and variables, e.g.:
var = "007"
print("Agent " + var)

2aad51ace252457ea829b30c120ef32222b07443