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

[learn Python from simple to deep] six data types

編輯:Python

brief introduction

Python Some standard types are defined , Used to store various types of data .
Python There are currently six standard data types :

  • Numbers( Numbers )
  • String( character string )
  • List( list )
  • Tuple( Tuples )
  • Dictionary( Dictionaries )
  • Set( aggregate )

Several classifications

Category one :
These six data types can be subdivided into immutable data types and variable data types

Immutable data (3 individual ):Number( Numbers )、String( character string )、Tuple( Tuples )
Variable data (3 individual ):List( list )、Dictionary( Dictionaries )、Set( aggregate )

Category two :
These six data types can be subdivided into 4 Number types and 5 Two sequence types , common 9 Number types .

4 The two types of numbers are : integer (int), floating-point (float), The plural (complex), Boolean value (bool)
5 The two sequence types are : character string (string), list (list), Tuples (tuple), Dictionaries (dict), aggregate (set)

【 notes 】 When we need to store a set of data , You need to use the sequence type , The sequence assigns an index to each element , The first is 0, The second is 1, Discuss and deduce in turn .

One 、 Numbers (Number)

Python3 Four different types of numbers are supported :

int( Long integer )
float( floating-point )
complex( The plural )
bool( Boolean type )

【 Be careful 】 stay Python 3 in , There is only one integer type int, Expressed as a long integer , No, python2 Medium Long. stay Python2 There is no Boolean in , It uses numbers 0 Express False, use 1 Express True. To Python3 in , hold True and False Defined as a keyword , But their value is still 1 and 0, They can be added to numbers .

give an example :

# Numerical type 
>>> n=100 # Define a variable 
#type: View data types of variables 
>>> print(type(n))

Output :<class ‘int’>

>>> pi=3.14
>>>print(type(pi))

Output :<class ‘float’>

Two 、 character string (String)

Data in double or single quotes , It's just a string .
The single character is in Python Is also used as a string .

# Character 
string1="Python"
print(type(string1))

Output :<class ‘str’>

# Multiline string 
string3='''Python baidu '''
print(string3)
print(type(string3))

Output :
Python
baidu

<class ‘str’>

3、 ... and 、 list (List)

Create a list , Just use different data items separated by commas square brackets [ ] Just round it up .
The data items of a list do not need to have the same type .

list1 = ["qing", "ping", 27]
list2 = [1, 2, 3, 4]
list3 = ["a", "b", "c"]

Four 、 Tuples (Tuple)

Tuples are similar to lists , But the element values of tuples cannot be modified or deleted , It can be sliced and connected .
Tuples are easy to create , Enclosed in parentheses , Separated by commas . Tuples use braces , The list uses brackets .

Tuple = () # Creating a tuple 
Tuple = ("a","b","c","d")
Tuple = ([1,2,3],"a",3,)

Be careful 1: Tuple of an element , Add a... At the back “,”

Tuple =(2022,)

Be careful 2: Arbitrary with “,” Separate sequences , Default is tuple .

>>> a = 1,2,3,4
>>> a
(1, 2, 3, 4)
>>> a =[1,2,3,4],5,"str"
>>> a
([1, 2, 3, 4], 5, 'str')

Be careful 3:Python Tuples tuple() Function to convert a list to a tuple .

5、 ... and 、 Dictionaries (Dictionary)

Dictionaries are stored in key value pairs , Elements can be key visit . Dictionaries are written by Curly braces { } Enclosed contains ,key : value Two parts .

 dict = {
'name':' Monitor of the class ', 'id':100, 'sex':'f', 'address':' Earth Asia China Beijing '}
print(dict["name"],dict["sex"])

6、 ... and 、 aggregate (Set)

A collection is an unordered and indexed collection . stay Python in , Sets are written in curly braces .
Be careful : have access to Curly braces { } perhaps set() Function to create a collection , But to create an empty collection, you must use set() instead of { }, because { } Is used to create an empty dictionary ,

s1= {
"apple", "banana", "cherry"}
print(s1)

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