Python There are mainly 8 Type of data :number( Numbers )、string( character string )、list( list )、tuple( Tuples )、dict( Dictionaries )、set( aggregate )、Boolean( Boolean value )、None( Null value ).
among Python There are six standard data types :
1、 character stringThere are three ways to declare strings : Single quotation marks 、 Double and triple quotes ( Include three single quotes or three double quotes )
>>> str1 = 'hello world'>>> str2 = "hello world">>> str3 = '''hello world'''>>> str4 = """hello world""">>> print str1hello world>>> print str2hello world>>> print str3hello world>>> print str4hello world
2、 Numbers Python3 Three different numerical types are supported :
integer (int): It's usually called an integer or an integer , It's a positive or negative integer , Without decimal point .Python3 There is no limit to the size of an integer , Can be viewed as Long Type used , therefore Python3 No, Python2 Of Long type .
floating-point (float): Floating point type consists of integer part and decimal part , Floating point types can also be represented by scientific notation .
The plural ( (complex)): The plural consists of the real part and the imaginary part , It can be used a + bj, perhaps complex(a,b) Express , The real part of a complex number a Deficiency part of harmony b It's all floating point .
3、 listA list is a modifiable collection type , Its elements can be numbers 、string Isobasic type , It could be a list 、 Tuples 、 Collection objects such as dictionaries , It can even be a custom type . It is defined as follows :
>>> 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、 Tuples Tuple types are the same as lists , It's also a sequence , Unlike the list , Tuples are immutable . Tuples are declared as follows :
lst = (0,1,2,2,2)lst1=("hello",)lst2 = ("hello")print type(lst1) #<type 'tuple'> If there is only one element, add a comma after it Otherwise, it would be str type print type(lst2) #<type 'str'>
5、 Dictionaries Dictionary is another variable container model , And can store any type of object . Each key value of the dictionary key=>value Yes, with a colon : Division , Comma between each key value pair , Division , The whole dictionary is enclosed in curly brackets {} in , The format is as follows :
>>>dict = {'a': 1, 'b': 2, 'b': '3'}>>> dict['b']'3'>>> dict{'a': 1, 'b': '3'}
6、 aggregate aggregate (set) Is an unordered sequence of non-repeating elements . You can use braces { } perhaps set() Function to create a collection .
Be careful : To create an empty collection, you must use the set() instead of { }, 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 about Python This is the end of the article on data types . I hope it will be helpful for your study , I also hope you can support the software development network .