Python Data Types
Everything is an object in Python programming, data types are actually
classes and variables are instance (object) of these classes. There are
various data types in Python. Some of the important types are listed
below.
1. Python Numbers
Number stores numeric values. The integer, float, and complex values
belong to a Python Numbers data-type. Python provides the type()
function to know the data-type of the variable. Similarly, the isinstance()
function is used to check an object belongs to a particular class.
Python supports three types of numeric data.
1. Int - Integer value can be any length such as integers 10, 2, 29, -20,
-150 etc. Python has no restriction on the length of an integer. Its
value belongs to int
a=5
print("The type of a", type(a))
Output : The type of a <class 'int'>
2. Float - Float is used to store floating-point numbers like 1.9, 9.902,
15.2, etc. It is accurate upto 15 decimal points.
b = 40.5
print("The type of b", type(b))
Output : The type of b <class 'float'>
3. Complex - A complex number contains an ordered pair, i.e., x + iy
where x and y denote the real and imaginary parts, respectively.
The complex numbers like 2.14j, 2.0 + 2.3j, etc.
c = 1+3j
print("The type of c", type(c))
Output : The type of c <class 'complex'>
2. Python List
List is an ordered sequence of items. It is one of the most used
datatype in Python and is very flexible. All the items in a list do
not need to be of the same type. Declaring a list is pretty straight
forward. Items separated by commas are enclosed within
brackets [ ].
list1 = [1, 'hi', 'Python', 2]
Output : [1, 'hi', 'Python', 2]
3. String
The string can be defined as the sequence of characters represented in
the quotation marks. In Python, we can use single, double, or triple quotes
to define a string. String handling in Python is a straightforward task since
Python provides built-in functions and operators to perform operations in
the string.
str = "string using double quotes"
print(str)
Output : string using double quotes
4. Tuple
A tuple is similar to the list in many ways. Like lists, tuples also contain
the collection of the items of different data types. The items of the tuple
are separated with a comma (,) and enclosed in parentheses (). A tuple is
a read-only data structure as we can't modify the size and value of the
items of a tuple.
tup = ("hi", "Python", 2)
print (tup)
Output : ('hi', 'Python', 2)
5. Dictionary
Dictionary is an unordered set of a key-value pair of items. It is like an
associative array or a hash table where each key stores a specific value.
Key can hold any primitive data type, whereas value is an arbitrary Python
object. The items in the dictionary are separated with the comma (,) and
enclosed in the curly braces {}.
d = {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
print (d)
Output : {1:'Jimmy', 2:'Alex', 3:'john', 4:'mike'}
6. Boolean
Boolean type provides two built-in values, True and False. These values
are used to determine the given statement true or false. It denotes by the
class bool. True can be represented by any non-zero value or 'T' whereas
false can be represented by the 0 or 'F'.
print(10 > 9)
Output : True
7. Set
Python Set is the unordered collection of the data type. It is iterable,
mutable(can modify after creation), and has unique elements. In set, the
order of the elements is undefined; it may return the changed sequence of
the element. The set is created by using a built-in function set(), or a
sequence of elements is passed in the curly braces and separated by the
comma.
a = {5,2,3,1,4}
# printing set variable
print("a = ", a)
Output : a = {1, 2, 3, 4, 5}
JSON : JavaScript Object Notation
It is a standard text-based format for representing structured data based
on JavaScript object syntax. It is commonly used for transmitting data in
web applications (e.g., sending some data from the server to the client, so
it can be displayed on a web page, or vice versa).
XML : Extensible Markup Language
It is used to encode the document that can be understandable by
humans and machines both. A data is stored in XML format is easy to
understand and easy to modify. XML is a structured format where we can
decide how to arrange the data within a file. We can struct as we want,
put any data at any place.
HTTP : HyperText Transfer Protocol
HyperText is the type of text which is specially coded with the help of
some standard coding language called as HyperText Markup Language
(HTML). The protocols that are used to transfer hypertext between two
computers is known as HyperText Transfer Protocol.
HTTP provides standard between a web browser and web server to
establish communication.
URL : Uniform Resource Locator
A URL is nothing more than the address of a given unique resource on
the Web. In theory, each valid URL points to a unique resource. Such
resources can be an HTML page, a CSS document, an image, etc. In
practice, there are some exceptions, the most common being a URL
pointing to a resource that no longer exists or that has moved.
SMTP : Simple Mail Transfer Protocol
The main purpose of SMTP is used to set up communication rules between
servers. The servers have a way of identifying themselves and announcing
what kind of communication they are trying to perform. They also have a
way of handling the errors such as incorrect email address. For example, if
the recipient address is wrong, then receiving server reply with an error
message of some kind.