There are eight main data types in Python: number, string, list, tuple, dict, set, Boolean, and None.
Among the six standard Python datatypes are.
1. String
Strings are declared in three ways: single quotes, double quotes, and triple quotes (including three single quotes or three double quotes)
>>> str1 = 'hello world' >>> str2 = "hello world" >>> str3 = '''hello world''' >>> str4 = """hello world""" >>> print str1 hello world >>> print str2 hello world >>> print str3 hello world >>> print str4 hello world
2. Numbers
Python3 supports three different numeric types:
Integer (int): often referred to as being an integer or whole number, a positive or negative integer without a decimal point.Python3 integers are unlimited in size and can be used as Long types, so Python3 does not have the Long type of Python2.
Floating point (float): Floating point consists of an integer part and a decimal part, floating point can also be expressed using scientific notation.
Complex numbers ((complex)): A complex number consists of a real part and an imaginary part, and can be represented as a + bj, or complex(a,b). The real part a and the imaginary part b of a complex number are floating-point.
3. List
List is a modifiable collection type whose elements can be basic types such as number, string, or collection objects such as lists, tuples, dictionaries, or even custom types. It is defined in the following way:
>>> nums = [1,2,3,4] >>> type(nums) <type 'list'> >>> print nums [1, 2, 3, 4] >>> strs = ["hello","world"] >>> print strs ['hello', 'world'] >>> lst = [1,"hello",False,nums,strs] >>> type(lst) <type 'list'> >>> print lst [1, 'hello', False, [1, 2, 3, 4], ['hello', 'world']]
4. Tuple
The tuple type, like a list, is a sequence, and unlike a list, a tuple is unmodifiable. A tuple is declared as follows:
lst = (0,1,2,2,2) lst1=("hello",) lst2 = ("hello") print type(lst1) #<type 'tuple'> followed by a comma if there is only one element Otherwise it's a str type print type(lst2) #<type 'str'>
5. Dictionary
Dictionaries are another model of mutable containers and can store objects of any type. Each key-value key=>value pair of a dictionary is separated by a colon :, each key-value pair is separated by a comma , and the entire dictionary is enclosed in curly braces {} , in the format shown below:
>>>dict = {'a': 1, 'b': 2, 'b': '3'} >>> dict['b'] '3' >>> dict {'a': 1, 'b': '3'}
6. Gathering
A set is an unordered sequence of non-repeating elements. Sets can be created using curly braces { } or the set() function.
Note: You must use set() instead of { } to create an empty collection, because { } is used to create an empty dictionary. Create Format:
a={'a','b','c','d'} b=set('abcdefabcd') c=set({'a':1,'b':2}) d=set(['a','b','c','a']) print(a,type(a)) print(b,type(b)) print(c,type(c)) print(d,type(d)) #Running results {'c', 'd', 'b', 'a'} <class 'set'> {'f', 'e', 'b', 'c', 'd', 'a'} <class 'set'> {'b', 'a'} <class 'set'> {'c', 'b', 'a'} <class 'set'>
This is the end of this article on Python data types. I hope it will be helpful for your learning and I hope you will support me more.