What are the data types in Python?
Built-in Data Types
The data type is the main concept of programming.
And variables can store the data of different types
and these different data-types can do different things.
In these categories, python has
the following data types built-in by default.
Text Type:
|
Str
|
Numeric Types:
|
int, float, complex
|
Sequence Types:
|
list, tuple, range
|
Mapping Type:
|
Dict
|
Set Types:
set, frozenset
|
set, frozenset
|
Boolean Type:
Bool
|
Bool
|
Binary Types:
|
bytes, bytearray, memoryview
|
Getting the DataType
If You can get data-type of
any object through using the type() function:
Example
Print the data type of variable x:
x = 5
print(type(x))
In python, BasicData Types
The Table of Contents
- Integers
- Floating-Point Numbers
- Complex Numbers
- Strings
- Built-in Function
- Math
- Input/output
- Variables, References, and Scopes
- Miscellaneous
- Conclusion
Integers
There is effectually no limits to how long an integer
operations value can be.
And this is constrained through the quantity of
memory your system has, as are all things, but outside that, an integer can
also be as long as you necessity it to be, in python 3:
>>>
>>>
print(123123123123123123123123123123123123123123123123 + 1)
123123123123123123123123123123123123123123123124
The decimal digit without any prefix to be a
decimal number by python interprets a system:
>>>
>>>
print(10)
10
Some following strings can be prepended to an integer
operations value to show the base other than 10:
Prefix
|
Interpretation
|
Base
|
0b (zero + lowercase letter 'b')
0B (zero + uppercase letter 'B')
|
Binary
|
2
|
0o (zero + lowercase letter 'o')
0O (zero + uppercase letter 'O')
|
Octal
|
8
|
0x (zero + lowercase letter 'x')
0X (zero + uppercase letter 'X')
|
Hexadecimal
|
16
|
For example:
>>>
>>>
print(0o10)
8
>>>
print(0x10)
16
>>>
print(0b10)
2
For more information on integer
operations values with non-decimal bases, if you want to see the
following Wikipedia sites then go on it: Binary, Octal, and Hexadecimal.
And which is the underlying type of python integer,
these are irrespective of base used to require it, it’s called int:
>>>
>>>
type(10)
<class
'int'>
>>>
type(0o10)
<class
'int'>
>>>
type(0x10)
<class
'int'>
Note: This
is a respectable time to indication that if you want to display a value while
in a REPL session, you don’t want to use the print() function.
Just only typing the importance at the >>>
prompt and hitting Enter will display it:
>>>
>>>
10
10
>>>
0x10
16
>>>
0b10
2
Several of the examples in this lesson series will
use this feature.
Note that this does not work inside a script file.
A value acting on a line by itself in a script
folder will not do anything.
Floating-Point Numbers
A floating-point number is a floating-point type
in python designates. The float value is indicated with a decimal point.
Optionally, some characters like e or E followed by
a positive or negative integer may be added to specify scientific notation.
>>>
>>>
4.2
4.2
>>>
type(4.2)
<class
'float'>
>>>
4.
4.0
>>>
.2
0.2
>>>
.4e7
4000000.0
>>>
type(.4e7)
<class
'float'>
>>>
4.2e-4
0.00042
Complex Numbers
A Complex numbers are
identified as <real part>+<imaginary part>j. For example:
>>>
>>>
2+3j
(2+3j)
>>>
type(2+3j)
<class
'complex'>
Strings
A String operator type is
the structure of character data. The string type is called str, in python.
A python defines string literals
may be using either single or double-quotes.
All these characters among the opening delimiter
and matching closing delimiter these are part of the string:
>>>
>>>
print("I am a string.")
I am
a string.
>>>
type("I am a string.")
<class
'str'>
>>>
print('I am too.')
I am
too.
>>>
type('I am too.')
<class
'str'>
In
python, a string operator can
contain as several characters as you wish. Your machine’s memory resources are
only limits. But a string can also be empty:
>>>
>>>
''
''
Whatever if you want to contain a character quote
as the part of the string itself! And your main impulse might be to try
something like this:
>>>
>>>
print('This string contains a single quote (') character.')
SyntaxError:
invalid syntax
A string in this example opens by a single quote,
therefore the python assumes the following single quote, and one in parentheses
this was intentional to be the part of the string, is a closing delimiter.
Now final single quote is then a stray and causes
the syntax error shown. As you can see, that doesn’t work so well.
Now if you want to contain either type of character
quote within the string, then the simplest way is to delimit the string through
another type.
But if the string is to include a single quote,
delimit it with double quotes and vice versa:
>>>
>>>
print like ("The string has contained a single quotation of (')
character.")
This
string involves a single quote (') character.
>>>
print like (“The string has to contain a double quotation of (")
character.')
This
string contains a double quote (") character.
Built-In Functions
As of python 3.6, now the python interpreter
supports several built-in functions: sixty-eight.
And you will also cover many of these in the
following discussions, as they come up in context.
Now, a brief overview follows, what is available
it’s just to give a feel. If you want to see more detail of python
documentation on built-in functions.
Then many of the following descriptions are
referring to the topic and concepts that will be discoursed in upcoming
tutorials.
Math
Function
|
Description
|
abs()
|
Returns absolute value of a number
|
divmod()
|
Returns quotient and remainder of integer
division
|
max()
|
Returns the biggest of the given arguments or
items in an iterable
|
min()
|
Returns the lowest of the given arguments or
items in an iterable
|
pow()
|
Raises a number to a power
|
round()
|
Rounds a floating-point value
|
sum()
|
Sums the items of an iterable
|
Input/Output
Function
|
Description
|
format()
|
Converts a value to a formatted representation
|
input()
open()
|
Reads input from the console
Opens a file and returns a file object
|
print()
|
Prints to a text stream or the console
|
Variables, References, and Scope
Function
|
Description
|
dir()
|
Returns a list of names in existing
local scope or a slope of object attributes
|
globals()
|
Returns a dictionary representative the existing
global symbol table
|
id()
|
Returns the personality of an object
|
locals()
|
Updates and returns a dictionary
representative current local symbol table
|
Vars()
|
Returns __dict__ characteristic for
a part, class, or object
|
Miscellaneous
Function
|
Description
|
callable()
|
Returns True if object appears callable
|
compile()
|
Compiles cause into a code or AST object
|
eval()
|
Evaluates a Python expression
|
exec()
|
Implements dynamic execution of Python code
|
hash()
|
Returns the hash value of an object
|
help()
|
Invokes the built-in help system
|
memoryview()
staticmethod()
|
Returns a memory view object
Returns a static method for a function
|
__import__()
|
Invoked by the import statement
|
Conclusion
You learned in this tutorials, about the built-in
data type and function which python provides.
This example assumed so far have all manipulated
and displayed only constant values.
In most of the programs, that value is change as
the program executes when you are usually going to want to create objects.
0 Comments