data types in python

Data types are the classification or categorization of data items. They are used to tell the type of data that a variable ca store. Python consider everything as an object, data types are actually classes and variables are instance (object) of these classes.

The basic data types of python are listed below :

  • Numbers 
  • Strings
  • List
  • Tuple
  • Dictionary
Numbers

These types are usually used to store numeric values. Numeric value can be integer, floating number or even a complex number. These values are defined as int, float and complex class in python.
  • Integers - This value is represented by int class. It contains positive or negative whole numbers (without fraction or decimal). In Python there is no limit to how long an integer value can be.
  • Float - This value is represented by float class. It is a real number with floating point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.
  • complex Numbers - Complex number is represented by complex class. It is specified as (real part) + (imaginary part)j. For example – 2+3j.


Strings

Strings are the sequence of characters enclosed with single, double or triple quotes.

Example 

str1="hello world 1"
print(str1)
str2='hello world 2'
print(str2)

Output

hello world 1
hello world 2

Note :
  • The operator + is used to concatenate two strings as the operation "hello"+" python" returns "hello python". 
  •  The operator * is known as repetition operator as the operation "Python " *2 returns "Python Python".


List

Lists are just like the arrays, declared in other languages. Lists need not be homogeneous always which makes it the most powerful tool in Python. A single list may contain Data Types like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation. List in Python are ordered and have a definite count. The elements in a list are indexed according to a definite sequence and the indexing of a list is done with 0 being the first index. Each element in the list has its definite place in the list, which allows duplicating of elements in the list, with each element having its own distinct place and credibility. It is represented by list class.

Example 

list=["hello","my","dear","friend",2020]
print(list)
print(list[2])

Output

['hello','my','dear',2020]
dear


Tuple


Tuple is an ordered collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. The important difference between a list and a tuple is that tuples are immutable. Also, Tuples are hashable whereas lists are not. It is represented by tuple class.A tuple is a read-only data structure as we can't modify the size and value of the items of a tuple.


Example 

t = ("hi", "python", 2)
print (t[1:])
print (t[0:1])
print (t)
print (t + t)
print (t * 3)
print (type(t))
t[2] = "hi"

Output

('python', 2)
('hi',)
('hi', 'python', 2)
('hi', 'python', 2, 'hi', 'python', 2)
('hi', 'python', 2, 'hi', 'python', 2, 'hi', 'python', 2)

Traceback (most recent call last):
  File "main.py", line 8, in 
    t[2] = "hi";
TypeError: 'tuple' object does not support item assignment


Set

In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set.

Example 

thisset = {"twitter", "application", "facebook"}
print(thisset)

Output

{'twitter','application','facebook'}

Dictionary

Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’.

Example 

thisdict = {
  "num": 1,
  "name": "Jinoy",
  "course": "cs"
}
print(thisdict)
x=thisdict["name"]
print(x)

Output

{'num': 1, 'name': 'Jinoy', 'course': 'cs'}
Jinoy


Conclusion




Prev
Next

Comments

Popular posts from this blog

Node.js Cheat Sheet

Enhancing Supply Chain Security and Cloud-Native Development with Chainguard

Codeigniter ! Simple But Powerful