To Documents
Constants, Datatypes, and Variables
Constants
Python uses four types of constants: integer, floating point, string,
and boolean.
- Integer
- Python integers are positive, negative, or zero numbers
that do not have a fractional part:
34 643938 -21 -38472874 0
- Python integers have an unlimited range.
- Floating Point
- String
- Boolean Python datatype bool
- A boolean constant can take on either of the values
True or False.
Datatypes
- A datatype is a Python representation of data.
- Python uses these datatypes to represent constants:
Constant Type | Python Datatype |
Integer | int |
Floating Point | float |
String | str |
Boolean | bool |
Variables
- A variable is a location in a computer that contains data, which
might be changed during the execution of a Python script.
There are three aspects to a variable: (1) name, (2) value,
(3) address.
- The name of a Python variable must start with a
letter (upper or lowercase) or underscore and consist entirely of
letters, digits, or underscores.
- There is no limit to the length of a Python variable name. However, names
longer than about 12 characters become unwieldy. Here are some
examples of Python variable names:
x x123 customer3 number_of_customers
- Python is case sensitive, which means that capitalization matters. The names
number_Of_Customers
and
number_of_customers
represent two different variables.
- By convention, Python spells local variable names in lower case and uses underscores to separate words
(underscore notation).
An example of a local variable name is number_of_letters.
- Although a single underscore is a legal variable name, it is not
recommended.
- The address of a variable is the location
in the computer's memory where the data is stored. The Python interpreter
keeps a symbol table that maintains the correspondence between the
variable's name and its address, so the programmer need not be
explicity concerned about the address.
- The value of a variable is the data currently stored at its address.
Every variable contains the data of an object. Objects can be
from the int, float,
str, or bool classes. They can also
be from any other class defined by the user or from a Python module.
A variable can only contain one value at a time.
- The datatype of a variable is inferred by Python by a process called
duck typing. (If it looks like a duck and quacks like a duck, it's a duck!)
For example, consider these Python statements
n = 34
x = 3.14
s = "dog"
f = true
Python knows that n is int, x is
float,
s is str, and f is bool because of the datatypes of
the constants on the right hand side of the equation.
- Explicit variable declaration is not used in Python.
- Each variable has a specific type. Although each of the following are
allowed:
# Following result is 12 because addition is performed
s = 7 + 5
# Following result is "dogcat" because concatenation is performed
t = "dog" + "cat"
However, the mixing variable types with the + operator is not allowed:
s = "328" + 5
Use either of the following instead:
s = int("328") + 5
t = "dog" + str(5)
The Assignment Operator
- The assignment operator is used to assign a value on the right side of an
expression to a variable on the left side.
For example,
x = 5
assigns the value 5 to the variable x.
- Question: why is the following not a legal assignment?
5 = x