As with other computer languages, Python has various simple, inbuilt types of data. These
the null object.
surprisingly, there are only two values, and in Python they are called True and False.
−1 (for 32 bit and 64 bit respectively). There is no limit on long integers
integer, the long integer. Unless you are doing something unusual, there is no point
situations in Python 2 the plain integers will suffice. Example usage:
decimal points or exponential notation, are not always represented exactly, since a
errors, and potential instability of numerical algorithms. However, such issues are
common to all computer languages. Example usage:
z = 123.45
There is also an inbuilt data type to represent complex numbers which you would
normally write in the form ‘a+bi’ (mathematical notation) or ‘a+bj’ (engineering notation).
Although complex numbers occur quite naturally in mathematics, science and
engineering, relatively few Python programs use them. The Python syntax follows the
engineering style and the real and imaginary parts can themselves be integer or floating
point:
x = 3+4j
y = 1.2-5.8j
Strings represent text, i.e. strings of characters. They can be delimited by single quotes
(’) or double quotes (”), but you have to use the same delimiter at both ends. Unlike some
programming languages, such as Perl, there is no practical difference between the two
types of quote, although using one type does allow the other type to appear inside the
string as a regular character. Example usage:
r1 = 'Ala'
r2 = "Arg"
text = "It's a line with an apostrophe"
Python also allows multi-line strings, which start and end either with triple single
quotes (”’) or triple double quotes (”””). Example usage:
text = """Python also allows multi-line strings, which
start and end with a triple single quote or a triple
double quote."""
Note that the indentation inside the string does not have to align with the start of the
statement. Any whitespace at the beginning or end of the internal lines, i.e. between the
opening and closing triple quotes, does make a difference though. Hence, if the second
line of text were indented, then those indentation spaces would be present in the string.
The last of the basic data types we cover here is a special built-in value called None,
which can be thought of as representing nothingness or that something is undefined. For
example, it can be used to indicate that a variable exists, but has not yet been set to
anything specific. Example usage:
z = None
Finally, if you have a variable and want to know what its data type is then you can use
the type() function. This actually generates a special object representing the type, though it
prints out in an informative way:
print( type(x) ) # 'complex'
print( type(z) ) # 'NoneType'
Do'stlaringiz bilan baham: