Variables
Using just literal constants can soon become boring - we need some way of storing any information and manipulate them as well. This is where variables come into the picture. Variables are exactly what the name implies - their value can vary, i.e., you can store anything using a variable. Variables are just parts of your computer's memory where you store some information. Unlike literal constants, you need some method of accessing these variables and hence you give them names.
Which statement about variables is correct?
Create a variable storing your age:
Identifier Naming
Variables are examples of identifiers. Identifiers are names given to identify something. There are some rules you have to follow for naming identifiers:
- The first character of the identifier must be a letter of the alphabet (uppercase ASCII or lowercase ASCII or Unicode character) or an underscore ('_').
- The rest of the identifier name can consist of letters (uppercase ASCII or lowercase ASCII or Unicode character), underscores ('_') or digits (0-9).
- Identifier names are case-sensitive. For example, 'myname' and 'myName' are not the same. Note the lowercase 'n' in the former and the uppercase 'N' in the latter.
- Examples of valid identifier names are 'i', 'name_2_3'. Examples of invalid identifier names are '2things', 'this is spaced out', 'my-name' and '>a1b2_c3'.
Which of these is a valid Python identifier?
Let's categorize these identifiers:
Data Types
Variables can hold values of different types called data types. The basic types are numbers and strings, which we have already discussed. In later chapters, we will see how to create our own types using classes.
What's the type of this value? "42"
Object
Remember, Python refers to anything used in a program as an object. This is meant in the generic sense. Instead of saying "the something"', we say "the object".
Note for Object Oriented Programming users:
Python is strongly object-oriented in the sense that everything is an object including numbers, strings and functions.
In Python, which of these is NOT an object?