Python Data Type
Introduction
Type represents the kind of value and determines how the value can be used. All data values in Python are encapsulated in relevant object classes. Everything in Python is an object and every object has an identity, a type, and a value. Like another object-oriented language such as Java or C++, there are several data types which are built into Python. Extension modules which are written in C, Java, or other languages can define additional types.
To determine a variable's type in Python you can use the type() function. The value of some objects can be changed. Objects whose value can be changed are called mutable and objects whose value is unchangeable (once they are created) are called immutable. Here are the details of Python data types
Numbers
Python has three distinct numeric types:
- Integers: Represent whole numbers, both positive and negative, without fractional parts.
- Floating-point numbers: Represent numbers with decimal points.
- Complex numbers: Used in engineering and science, containing a real and imaginary part (e.g., A + Bi).
All numeric objects in Python are immutable.
Examples: Integers and Floats
Floats and Integers
>>> x = 8
>>> y = 7
>>> x+y
>>> x-y
>>> x/y
>>> x*y
Output:
15 1 1.1428571428571428 56
Exponentiation
>>> 4**3
>>> 3**4
Output:
64 81
Integer Division
>>> 12/3
>>> 64//4
>>> 15//3
Output:
4.0 16 5
Remainder
>>> 15 % 4
Output:
3
Mathematically, a complex number (generally used in engineering) is a number of the form A+Bi where i is the imaginary number. Complex numbers have a real and imaginary part. Python supports complex numbers either by specifying the number in (real + imagJ) or (real + imagj) form or using a built-in method complex(x, y). See the following statements in Python Shell.
Self-assignment Operators
You can use compound assignment operators like +=, -=, *=, **=, and //=, which combine an operation with assignment.
>>> count = 0
>>> count +=2
>>> count
Output:
2
>>> count -=2
>>> count
Output:
0
>>> count +=2
>>> count *=4
>>> count
Output:
8
>>> count **=4
>>> count
Output:
4096
>>> count /=4
>>> count
Output:
1024.0
>>> count//=4
>>> count
Output:
256.0
>>> 15.0 //2
Output:
7.0
Order of operations
>>> (3+12) / 3
>>> 15 / (3 + 2)
Output:
5.0 3.0
Boolean (bool)
The simplest build-in type in Python is the bool type, it represents the truth values False and True. See the following statements in Python shell.
Strings
In Python, a string type object is a sequence (left-to- right order) of characters. Strings start and end with single or double quotes Python strings are immutable. Single and double quoted strings are same and you can use a single quote within a string when it is surrounded by double quote and vice versa. Declaring a string is simple, see the following statements.
Special characters in strings
The backslash (\) character is used to introduce a special character. See the following table.
| Escape sequence | Meaning |
| \n | Newline |
| \t | Horizontal Tab |
| \\ | Backslash |
| \' | Single Quote |
| \" | Double Quote |
See the following statements on special characters.
String indices and accessing string elements
Strings are arrays of characters and elements of an array can be accessed using indexing. Indices start with 0 from left side and -1 when starting from right side.
string1 ="PYTHON TUTORIAL"
| Character | P | Y | T | H | O | N | T |
