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

Python basic data type

編輯:Python

Python Basic data type

List of articles

  • Python Basic data type
            • Multiple variable assignments
      • Standard data type
        • 1. Numeric type (number):
            • Digital type conversion :
            • Hexadecimal conversion :
        • 2. String type (string):
        • 3. List the type (list):
        • 4. Tuples (tuple):

Python Variables of do not need to be declared , Each variable must be assigned a value before use , Variables can only be created after assignment . Equal sign (=) It is used to assign values to variables , The format is : Variable name = Values stored in variables .

Multiple variable assignments

Python It is allowed to assign values to multiple variables at the same time , for example :

a = b = c = 1

Create an integer object , The value is 1, From the back to the front , Multiple variables are given the same value

Standard data type

Python There are six standard data types in :

  • number:( Numbers )
  • string:( character string )
  • list:( list )
  • tuple:( Tuples )
  • set:( aggregate )
  • dictionary:( Dictionaries )

Python Of the six standard data types :

  • Immutable data types number( Numbers )、string( character string )、tuple( Tuples )
  • Variable data type list( list )、dictionary( Dictionaries )、set( aggregate )

1. Numeric type (number):

(1).int integer :Python Can handle integers of any size .
(2).flolat floating-point : It is composed of integral part and decimal part , To approximate or round without being able to calculate accurately .
(3)bool Boolean type : Used as a logical judgment , The value of a variable is only true (True) And fake (False),
(4)complex The plural : It is composed of a real number and an imaginary number , Expressed as :x+yj, A complex number is a pair of ordered floating-point numbers (x,y), among x Is the real part , Is the imaginary part .

Digital type conversion :

We can convert numeric types through built-in functions .

  • take int Convert to bool,0 convert to False, Not 0 convert to True.
print(bool(a))# a For integer numbers 
  • take bool Convert to int,False convert to 0,True convert to 1.
print(int(a))# a by False or True
  • take float Convert to int
print(int(a))# a Is a floating-point number 
  • take int Convert to float
print(float(a))# a For integer numbers 
  • take bool Convert to float
print(float(a))# a by False or True
  • take float Convert to bool
print(bool(a))# a Is a floating-point number 
Hexadecimal conversion :
  • hex(i) take 10 Hexadecimal numbers are converted to 16 Base number .
  • oct(i) take 10 Hexadecimal numbers are converted to 8 Base number .
  • bin(i) take 10 Hexadecimal numbers are converted to 2 Base number .
  • use int() Convert other base numbers to 10 Base number .

Be careful : Switching , If there are other useless characters, an error will be reported , Plus and minus signs make sense .

2. String type (string):

  • Python The self string in is used as ' And double quotes " Cover up , Also use a backslash \ Escapes special characters .
  • With 0 Start index for ,-1 Index the beginning of the flashback .
  • + Is the concatenator of a string ,* Means to copy the current string , The number that follows is the number of copies .
  • python String cannot be changed
    Be careful :python There is no separate character type in , A character is a string of length one .

3. List the type (list):

  • The list is written in [] Between , A comma separated list of elements .
  • List can complete the data structure implementation of most collection classes . The types of elements in the list can be different , It contains numbers 、 Strings and even lists ( nesting )
  • Just like a string , Lists can also be indexed and intercepted , After the list is intercepted, a new list is returned .
  • Index from 0 Start ,-1 Is the index value at the end .
  • + Is a list connector * Is a repeat operation , The elements in the list can be changed .
  • list yes python The most commonly used data type in .

4. Tuples (tuple):

Tuples are similar to lists , The difference is that elements of tuples cannot be modified , Tuples are written in parentheses () in , Use commas... Between elements , separate .
Tuples are similar to strings , Can be indexed and indexed from 0 Start ,-1 Is the beginning of the end . Although tuples are immutable , But it can contain mutable objects . Tuples also use + To splice .
Be careful string,list,tuple All belong to sequebce( Sequence ) Of .


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