Variable and Data Types

Variable is a name used to refer memory location. Variable is also known as an identifier and used to hold value. It is recommended to use lowercase letters for the variable names.

Identifier Naming:

Variables are examples of identifiers. An Identifier is used to identify the literals used in the program. The rules for naming an identifier are given below.

- The first character of the variable must be an alphabet or underscore ( _ ).

- All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore or digit (0-9).

- Identifier name must not contain any white space, or special character (!, @, #, %, ^, &, *).

- The identifier name must not be similar to any keyword defined in the language.

- Identifier names are case sensitive for example my name, and my name is not the same.

- Examples of valid identifiers: a123, _n, n_9; and invalid identifiers: 1a, n%4, n 9.

Assigning a value to a Variable:

We can use the assignment operator = to assign a value to a variable.

testing = "QA"

Python is a type inferred language; it automatically knows QA is a string.

Data Types:

Variables store data of different types and every value in Python has a datatype. Data types are classes and variables are instance (object) of these classes.

These are the standard data types:

- Numbers

- String

- List

- Tuple

- Dictionary

Numbers:
  1. int, are signed integers like 10, 2 or -449.
  2. long, are integers used for a higher range of values like 908090800L.
  3. float, are used to store floating-point numbers (with decimal cases) like 4.5 or 9.283.

 

Related Tutorials