What are the 4 data types in Python?
Data Type?
Data types are the bracket or categorization of data particulars. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are case( object) of these classes.
Following are the standard or erected- in data type of Python
- Numeric
- Sequence Type
- Boolean
- Set
- Dictionary
Numeric
In Python, numeric data type represent the data which has numeric value. Numeric value can be integer, floating number or even complex numbers. These values are defined as int, float and complex class in Python.
Integers – This fee is represented with the aid of using int class. It includes nice or poor complete numbers (with out fraction or decimal). In Python there may be no restrict to how lengthy an integer fee can be.
Float – This fee is represented with the aid of using flow class. It is a actual quantity with floating factor representation. It is targeted with the aid of using a decimal factor. Optionally, the person e or E observed with the aid of using a nice or poor integer can be appended to specify clinical notation.
Complex Numbers – Complex quantity is represented with the aid of using complicated class. It is targeted as (actual part) + (imaginary part)j. For example – 2+3j
# Examples all these like, int , float, complex.
a = 15
print("Type of a: ", type(a))
b = 6.0
print("\nType of b: ", type(b))
c = 4 + 8j
print("\nType of c: ", type(c))
Output
Type of a: <class 'int'>
Type of b: <class 'float'>
Type of c: <class 'complex'>
String
In Python, Strings are arrays of bytes representing Unicode characters. A string is a group of 1 or greater characters installed a unmarried quote, double-quote or triple quote. In python there's no person information type, a person is a string of period one. It is represented via way of means of str class.
Example Show:
Creating String
Strings in Python can be created using single quotes or double quotes or even triple quotes.
# Python Program for
# Creation of String
# Creating a String
# with single Quotes
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
print(String1)
# Creating a String
# with double Quotes
String1 = "I'm a Geek"
print("\nString with the use of Double Quotes: ")
print(String1)
print(type(String1))
# Creating a String
# with triple Quotes
String1 = '''I'm a Geek and I live in a world of "Geeks"'''
print("\nString with the use of Triple Quotes: ")
print(String1)
print(type(String1))
# Creating String with triple
# Quotes allows multiple lines
String1 = '''Geeks
For
Life'''
print("\nCreating a multiline String: ")
print(String1)
List
Lists are similar to the arrays, declared in different languages that may be a ordered assortment of data. it's terribly versatile because the things in an exceedingly list don't got to be of constant type.
Making List
Lists in Python will be created by simply putting the sequence within the sq. brackets[].
Example :
# Python program to demonstrate
# Creation of List
# Creating a List
List = []
print("Initial blank List: ")
print(List)
# Creating a List with
# the use of a String
List = ['GeeksForGeeks']
print("\nList with the use of String: ")
print(List)
# Creating a List with
# the use of multiple values
List = ["Geeks", "For", "Geeks"]
print("\nList containing multiple values: ")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Geeks', 'For'], ['Geeks']]
print("\nMulti-Dimensional List: ")
print(List)
Tuple
Just like listing, tuple is likewise an ordered series of Python objects. The handiest distinction among tuple and listing is that tuples are immutable i.e. tuples can not be changed after it's miles created. It is represented through tuple class.
Creating Tuple
In Python, tuples are created through setting a chain of values separated through ‘comma’ without or with the usage of parentheses for grouping of the statistics sequence. Tuples can incorporate any variety of factors and of any datatype (like strings, integers, listing, etc.).
Example :
# Python program to demonstrate
# creation of Set
# Creating an empty tuple
Tuple1 = ()
print("Initial empty Tuple: ")
print (Tuple1)
# Creating a Tuple with
# the use of Strings
Tuple1 = ('Geeks', 'For')
print("\nTuple with the use of String: ")
print(Tuple1)
# Creating a Tuple with
# the use of list
list1 = [1, 2, 4, 5, 6]
print("\nTuple using List: ")
print(tuple(list1))
# Creating a Tuple with the
# use of built-in function
Tuple1 = tuple('Geeks')
print("\nTuple with the use of function: ")
print(Tuple1)
# Creating a Tuple
# with nested tuples
Tuple1 = (0, 1, 2, 3)
Tuple2 = ('python', 'geek')
Tuple3 = (Tuple1, Tuple2)
print("\nTuple with nested tuples: ")
print(Tuple3)
Boolean
Data kind with one of the integrated values, True or False. Boolean gadgets which can be identical to True are truthy (real), and people identical to False are falsy (false). But non-Boolean gadgets may be evaluated in Boolean context as properly and decided to be real or false. It is denoted with the aid of using the magnificence bool.
Example:
# Python program to
# demonstrate boolean type
print(type(True))
print(type(False))
print(type(true))
Set
Sets are used to shop more than one gadgets in a unmarried variable.
In Python, Set is an unordered series of facts kind this is iterable, mutable and has no reproduction factors. The order of factors in a hard and fast is undefined aleven though it is able to include diverse factors.
Creating Sets
Sets may be created via way of means of the usage of the integrated set() feature with an iterable item or a chain via way of means of setting the series interior curly braces, separated via way of means of ‘comma’. Type of factors in a hard and fast want now no longer be the same, diverse mixed-up facts kind values also can be surpassed to the set.
Example :
# Python program to demonstrate
# Creation of Set in Python
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
# Creating a Set with
# the use of a String
set1 = set("GeeksForGeeks")
print("\nSet with the use of String: ")
print(set1)
# Creating a Set with
# the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("\nSet with the use of List: ")
print(set1)
# Creating a Set with
# a mixed type of values
# (Having numbers and strings)
set1 = set([1, 2, 'Geeks', 4, 'For', 6, 'Geeks'])
print("\nSet with the use of Mixed Values")
print(set1)
Dictionary
Dictionaries are used to store data values in key: value pairs.
A dictionary is a collection which is ordered*, changeable and do not allow duplicates.
Example:
# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Geeks', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Dict = dict({1: 'Geeks', 2: 'For', 3:'Geeks'})
print("\nDictionary with the use of dict():")
print(Dict)
# Creating a Dictionary
# with each item as a Pair
Dict = dict([(1, 'Geeks'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(Dict)
Next Post : if else in python
Comments