程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Introduction to basic data types in Python

編輯:Python

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 string

There 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 world2、 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、 list

A 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 .



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved