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

Diagram Python | basic data types

編輯:Python

author : Han Xinzi @ShowMeAI

Tutorial address :http://www.showmeai.tech/tutorials/56

This paper addresses :http://www.showmeai.tech/article-detail/67

Statement : copyright , For reprint, please contact the platform and the author and indicate the source


1.Python Variable type

Python Basic data types are generally divided into 6 Kind of : The number (Numbers)、 character string (String)、 list (List)、 Tuples (Tuple)、 Dictionaries (Dictionary)、 aggregate (Set). This article explains in detail Python Variable assignment in 、 Data type and conversion of data type .

The value of a variable stored in memory , This means that when you create variables, you open up a space in memory . Data types based on variables , The interpreter allocates the specified memory , And decide what data can be stored in memory . therefore , Variables can specify different data types , These variables can store integers , Decimal or character .

2. Variable assignment

  • Python The variable assignment in does not require a type declaration .

  • Each variable is created in memory , All include the identification of variables , Name and data these information .

  • Each variable must be assigned a value before use , The variable will not be created until it is assigned a value .

  • Equal sign = Used to assign values to variables .

Equal sign = To the left of the operator is a variable name , Equal sign = To the right of the operator is the value stored in the variable . for example ( The following code can be found in On-line python3 Environmental Science Run in ):

num = 100 # Assign an integer variable 
weight = 100.0 # floating-point
name = "ShowMeAI" # character string print(num)
print(weight)
print(name)

In the example above ,100,100.0 and "ShowMeAI" Assign to respectively num,weight,name Variable .

Executing the above program will output the following results :

100
100.0
ShowMeAI

3. Multivariable assignment

Python Allows you to assign values to multiple variables at the same time . for example :

a = b = c = 1

The above instance , Create an integer object , The value is 1, Three variables are allocated to the same memory space .

You can also specify multiple variables for multiple objects . for example :

a, b, c = 1, 2, "ShowMeAI"

The above instance , Two integer objects 1 and 2 Assign to variables separately a and b, String object "ShowMeAI" Assign to a variable c.

4. Standard data type

There can be many types of data stored in memory .

for example , A person's age can be stored in Numbers , His name can be stored in characters .

Python Some standard types are defined , Used to store various types of data .

Python There are the most commonly used 5 Standard data types :

  • Numbers( The number )
  • String( character string )
  • List( list )
  • Tuple( Tuples )
  • Dictionary( Dictionaries )

5.Python The number

Numeric data types are used to store numeric values .

They are immutable data types , This means that changing the digital data type assigns a new object .

When you specify a value ,Number The object will be created :

num1 = 1
num2 = 10

You can also use the del Statement to delete references to some objects .

del The syntax of the sentence is :

del num1[,num2[,num3[....,numN]]]

You can use the del Statement to delete references to single or multiple objects . for example :

del num
del num_a, num_b

Python Four different types of numbers are supported :

  • int( signed int )
  • float( floating-point )
  • complex( The plural )

Some examples of numerical types :

intfloatcomplex100.03.14j10015.2045.j-786-21.99.322e-36j08032.3e+18.876j-0490-90.-.6545+0J-0x260-32.54e1003e+26J0x6970.2E-124.53e-7j

6.Python character string

String or string (String) It's numbers 、 Letter 、 A string of characters made up of underscores .

It is generally recorded as :

s = "a1a2···an" # n>=0

It's the data type that represents text in programming languages .

python The list of strings for has 2 The order of values is :

  • Left to right index default 0 At the beginning , The maximum range is less string length 1
  • Right to left index default -1 At the beginning , The maximum range is at the beginning of the string

If you want to get a substring from a string , have access to [ Header subscript : Tail subscript ] To intercept the corresponding string , The subscript is from 0 From the beginning , It can be positive or negative , The subscript can be empty to indicate that the head or tail is taken .

[ Header subscript : Tail subscript ] The substring obtained contains the character of the header and subscript , But characters that don't contain trailing subscripts .

such as :

>>> s = 'ShowMeAI'
>>> s[6:8]
'AI'

When using colon delimited strings ,python Return a new object , The result contains consecutive content identified by this pair of offsets , The start on the left contains the lower boundary .

The above results include s[1] Value b, But not the maximum range Tail subscript , Namely s[5] Value f.

have access to __ plus (+)__ Connect strings , Use asterisk (*) Repeat the operation on the string . as follows ( The following code can be found in On-line python3 Environmental Science Run in ):

str = 'Hello ShowMeAI!'
print(str) # Output full string 
print(str[0]) # The first character in the output string
print(str[2:5]) # A string between the third and sixth in the output string
print(str[2:]) # Output a string starting with the third character
print(str * 2) # Output string twice
print(str + " Awesome") # Output the string of the connection

The output of the above example :

Hello ShowMeAI!
H
llo
llo ShowMeAI!
Hello ShowMeAI!Hello ShowMeAI!
Hello ShowMeAI! Awesome

Python List interception can receive the third parameter , The parameter function is the step size of interception , The following examples are in the index 1 To the index 4 And set the step size to 2( One place apart ) To intercept a string :

more python For the detailed explanation of string, please refer to python String and operation

7.Python list

List( list ) yes Python The most frequently used data type in .

List can complete the data structure implementation of most collection classes . It supports characters , Numbers , Strings can even contain lists ( That is, nesting ).

List with [ ] identification , yes python The most common composite data type .

The cutting of the values in the list can also use variables [ Header subscript : Tail subscript ] , You can intercept the corresponding list , Left to right index default 0 Start , Right to left index default -1 Start , The subscript can be empty to indicate that the head or tail is taken .

plus + Is the list join operator , asterisk ***** Is a repeat operation . as follows ( The following code can be found in On-line python3 Environmental Science Run in ):

list = [ 'ShowMeAI', 786 , 2.23, 'show', 70.2 ]
tinylist = [123, 'show'] print(list) # Output complete list
print(list[0]) # The first element of the output list
print(list[1:3]) # Output the second to third elements
print(list[2:]) # Output all elements from the third to the end of the list
print(tinylist * 2) # Output the list twice
print(list + tinylist) # Print a list of combinations

The output of the above example :

['ShowMeAI', 786, 2.23, 'show', 70.2]
ShowMeAI
[786, 2.23]
[2.23, 'show', 70.2]
[123, 'show', 123, 'show']
['ShowMeAI', 786, 2.23, 'show', 70.2, 123, 'show']

more python For the detailed explanation of the list, please refer to python list

8.Python Tuples

Tuples are another data type , Be similar to List( list ).

Tuple use () identification . The inner elements are separated by commas . But tuples cannot be assigned twice , Equivalent to read-only list .( The following code can be found in On-line python3 Environmental Science Run in )

tuple = ( 'ShowMeAI', 786 , 2.23, 'show', 70.2 )
tinytuple = (123, 'show') print(tuple) # Output full tuples
print(tuple[0]) # The first element of the output tuple
print(tuple[1:3]) # Output the second to fourth ( It doesn't contain ) The elements of
print(tuple[2:]) # Output all elements from the third to the end of the list
print(tinytuple * 2) # Output tuples twice
print(tuple + tinytuple) # Print combined tuples

The output of the above example :

('ShowMeAI', 786, 2.23, 'show', 70.2)
ShowMeAI
(786, 2.23)
(2.23, 'show', 70.2)
(123, 'show', 123, 'show')
('ShowMeAI', 786, 2.23, 'show', 70.2, 123, 'show')

The following is a tuple invalid , Because tuples are not allowed to be updated . The list is allowed to be updated :

tuple = ( 'ShowMeAI', 345 , 2.23, 'show', 456.2 )
list = [ 'ShowMeAI', 345 , 2.23, 'show', 456.2 ]
tuple[2] = 100 # Illegal application in tuple
list[2] = 100 # In the list are legitimate applications

more python For detailed explanation of tuples, please refer to python Tuples

9.Python Dictionaries

Dictionaries (dictionary) Except for the list python The most flexible type of built-in data structure . A list is an ordered collection of objects , A dictionary is an unordered collection of objects .

The difference between the two is : The elements in the dictionary are accessed by keys , Instead of accessing by offset .

Dictionary use "{ }" identification . The dictionary is indexed by (key) The value corresponding to it value form .( The following code can be found in On-line python3 Environmental Science Run in )

dict = {}
dict['one'] = "This is one"
dict[2] = "This is two" tinydict = {'name': 'ShowMeAI','code':3456, 'dept': 'AI'} print(dict['one']) # The output key is 'one' Value
print(dict[2]) # The output key is 2 Value
print(tinydict) # Output complete dictionary
print(tinydict.keys()) # Output all keys
print(tinydict.values()) # Output all values

The output is :

This is one
This is two
{'name': 'ShowMeAI', 'code': 3456, 'dept': 'AI'}
dict_keys(['name', 'code', 'dept'])
dict_values(['ShowMeAI', 3456, 'AI'])

more python For the detailed explanation of the dictionary, you can refer to python Dictionaries

10.Python Data type conversion

occasionally , We need to transform the built-in types of data , Conversion of data types , You just need to use the data type as the function name .

The following built-in functions can perform conversion between data types . These functions return a new object , Represents the value of the transformation .

function describe int(x [,base]) take x Convert to an integer long(x [,base] ) take x Convert to a long integer float(x) take x Converts to a floating point number complex(real [,imag]) Create a complex number str(x) Put the object x Convert to string repr(x) Put the object x Converts to an expression string eval(str) Used to calculate the validity in a string Python expression , And returns an object tuple(s) The sequence of s Converts to a tuple list(s) The sequence of s Convert to a list set(s) Convert to variable set dict(d) Create a dictionary .d It has to be a sequence (key,value) Tuples .frozenset(s) Convert to immutable set chr(x) Converts an integer to a character unichr(x) Convert an integer to Unicode character ord(x) Converts a character to its integer value hex(x) Convert an integer to a hexadecimal string oct(x) Converts an integer to an octal string

11. Video tutorial

Please click to B I'm looking at it from the website 【 Bilingual subtitles 】 edition


Data and code download

The code for this tutorial series can be found in ShowMeAI Corresponding github Download , Can be local python Environment is running , You can visit google Your baby can also use google colab One click operation and interactive operation learning Oh !

This tutorial series covers Python The quick look-up table can be downloaded and obtained at the following address :

  • Python Quick reference table

Expand references

  • Python course —Python3 file
  • Python course - Liao Xuefeng's official website

ShowMeAI Recommended articles

  • python Introduce
  • python Installation and environment configuration
  • python Basic grammar
  • python Basic data type
  • python Operator
  • python Condition control and if sentence
  • python Loop statement
  • python while loop
  • python for loop
  • python break sentence
  • python continue sentence
  • python pass sentence
  • python String and operation
  • python list
  • python Tuples
  • python Dictionaries
  • python aggregate
  • python function
  • python Iterators and generators
  • python data structure
  • python modular
  • python File read and write
  • python File and directory operations
  • python Error and exception handling
  • python object-oriented programming
  • python Namespace and scope
  • python Time and date

ShowMeAI A series of tutorials are recommended

  • The illustration Python Programming : From introduction to mastery
  • Graphical data analysis : From introduction to mastery
  • The illustration AI Mathematical basis : From introduction to mastery
  • Illustrate big data technology : From introduction to mastery

The illustration python | More articles on basic data types

  1. Python Basic data type - list (list) And a tuple (tuple) And collection (set)

    Python Basic data type - list (list) And a tuple (tuple) And collection (set) author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . This blog uses Python3.6 edition , And what we'll share later ...

  2. Python Basic data type - character string (string)

    Python Basic data type - character string (string) author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . This blog uses Python3.6 edition , And every article I share in the future is Python3.x edition ...

  3. python Basic data type test questions

    Python Basic data type test questions Examination time : Two and a half hours                       Full marks 100 branch (80 Points above include 80 Pass score ) One , Basic questions . 1, Brief introduction to variable naming specification (3 branch ) 2, The relationship between bytes and bits ...

  4. 1--Python introduction --Python Basic data type

    One .Python Basic grammar First use Python, First of all, we should clarify three points : Python Identifier ( For example, variable names . Function name, etc ), Available letters . Numbers and underscores make up , Cannot start with a number , And case sensitive . Python Sensitive to indentation . stay ...

  5. python One of the basic data types list

    python One of the basic data types list: 1. List creation list1 = ['hello', 'world', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ...

  6. Python Basic data type - Dictionaries (dict)

    Python Basic data type - Dictionaries (dict) author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . This blog uses Python3.6 edition , And every article I share in the future is Python3.x Version of yo ...

  7. Python Basic data type questions

    Python Basic data type Exam time : three hours Full marks 100 branch (80 Points above include 80 Pass score )1, Brief introduction to variable naming specification (3 branch ) 1. It must be a letter , Numbers , Any combination of underscores . 2. Can 't start with a number 3. It can't be pytho ...

  8. Python String of basic data type

    Python String of basic data type One .Python How to create a string stay python A string is formed by wrapping some text in quotation marks ( Quotation marks can be single quotation marks . Double quotes . Single three quotation marks , Double triple quotes , They are exactly the same ) >> ...

  9. Python A collection of basic data types

    Python A collection of basic data types aggregate (set) yes Python One of the basic data types , It has a natural ability to remove weight , That is, the elements in the set cannot be repeated . Sets are also disordered , And the elements in the collection must be immutable types . One . How to create a collection #1 ...

  10. The old boy Python== Basic data type test questions

    Reprint # Python Basic data type test questions # Examination time : Two and a half hours Full marks 100 branch (80 Points above include 80 Pass score ) # One , Basic questions . # 1, Brief introduction to variable naming specification (3 branch ) # 1. Variables are made up of letters . Numbers . Underline any ...

Random recommendation

  1. 【 primary 】 Fratricidal ——input Of blur Incident and button Of click event

    Let's start with an introduction , I've been writing about cell phones recently h5 page , It's mainly about login and registration , The most difficult part is the form element . What I want to achieve is : There is a delete icon behind the input box , Events are triggered when something is entered , Show delete icon , Clicking on this icon will delete the previously entered content ...

  2. 37-wc Brief notes

    Display row number . Number of words and bytes wc [options] [file-list] Parameters file-list yes wc List of pathnames of one or more files analyzed . If omitted file-list,wc Just read the input from the standard input Options ...

  3. HPQC HP Quality Center windows service

    HPQC HP Quality Center windows If the service has been started , You don't have to run run.bat Both have the same effect .

  4. hadoop Series three :mapreduce Use ( One )

    Please indicate the author and source at the top of the page http://www.cnblogs.com/zhuxiaojie/p/7224772.html One : explain Here are some blog posts of big data series , If you have time, it will be updated one after another , Including big data ...

  5. day08 File operations

    1. Three strings : (1)u'' Normal string ---> u'abc' ---> Default text mode , The output of text in characters (2)b'' Binary string ---> b'ASCII code ' -- ...

  6. CentOS 7 Add disk partition mount (lvm)

    1. View the existing disk of the host # fdisk -l Now there's a piece in the mainframe 8G Of disks sdb, It has not been mounted in a partition , So you need to mount the disk by partition . 2. Partition the disk # fdisk /dev/sdb   ( Select the partition you want to operate on ...

  7. [Postman] agent (16)

    The proxy server acts as the internal network and Internet Security barriers between , send Internet Other people on the Internet can't access information on the intranet . What is agency ? In the basic network , The client makes a request to the server , The server sent back a response . The proxy server acts as a computer ...

  8. Oracle11g RAC install

    Two knots RAC Environmental Science , database racdb example 1:racdb1      example 2:racdb2 1.IP planning name              oracle-db1    oracle-db2PUBLIC I ...

  9. Scala Advanced road - Use of collections of advanced data types

    Scala Advanced road - Use of collections of advanced data types author : Yin Zhengjie Copyright notice : Original works , Declined reprint ! Otherwise, the legal liability will be investigated . Scala There are three types of sets of : Sequence Seq. Set Set. mapping Map, All sets extend from ...

  10. JVM tuning (2)

    Heap size settings JVM There are three limits to the maximum heap size in : Data model of related operating system (32-bt still 64-bit) Limit : The available virtual memory limit of the system : The available physical memory limit of the system .32 A system. , The general limit is 1.5G~2G:64 For operation ...


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