This article is the second basic knowledge of Python, mainly for the introduction of built-in object types, the type of the following table.
Number Type
In Python, a number is not a true object type, but a grouping of similar types, such as integers and floating-point numbers, complex numbers, and so on. Python also provides several tools for dealing with digital objects, such as built-in math functions POW, ABS, standard library math, random, etc.
Look at the simple use of numbers.
In [15]: 123 + 234
OUT[15]: 357
In [16]: 1.5 * 4
OUT[16]: 6.0
in [+]: POW (2, 10)
OUT[32]: 1024
In [approx]: Import math
In [PNS]: Math.PI
OUT[37]: 3.141592653589793
In []: Math.sqrt (44)
OUT[38]: 6.6332495807108
In [All]: Import random
In [MAX]: Random.random ()
OUT[40]: 0.7181549555607203
in [+]: random.randint (1, 44)
OUT[41]: 36
String type
It is a collection of ordered characters and cannot be modified in situ (immutable sequences).
To create a string constant
Single quotation marks
in []: ' Spa ' m '
OUT[42]: ' Spa ' m '
Double quotes
in [+]: "Spa ' m"
OUT[43]: "Spa ' m"
Three quotation marks
in [+]: "... spam ..."
OUT[44]: ' ... spam ... '
in []: "" "... spam" "" "
OUT[45]: ' ... spam ... '
Basic operation of strings-get length, index and Shard
in [+]: S = ' spam '
In []: Len (S)
OUT[76]: 4
In [a]: S[0]
OUT[47]: ' s '
In []: S[-1]
OUT[48]: ' m '
in [+]: S[1:3]
OUT[49]: ' PA '
in [[]: s[:]
OUT[50]: ' Spam '
In [Wuyi]: s[:-1]
OUT[51]: ' Spa
The method of a string-method is a function associated with a particular object, used such as S.upper (), which is an object's property from the object's perspective, and that property is a callable function. How do you see what properties the object has, and how it works, through the built-in Dir, help function.
In [the]: Dir (S)
OUT[68]:
[' __add__ ',
' __class__ ',
' Rstrip ',
' Split ',
In [All]: Help (S.split)
Help on built-in function split:
Split (...)
S.split ([Sep [, Maxsplit]]), List of strings
Return A list of the words in the string S, using Sep as the
Delimiter string. If Maxsplit is given, at the most maxsplit
Splits is done. If Sep is no specified or is None and any
Whitespace string is a separator and empty strings be removed
From the result.
(END)
List type
It is an ordered set of objects of any type, variable in size, and can be nested (variable sequences).
Create a list
In []: L = []
In [the]: L = [1, 2, 3, 4]
in [n]: L = [' abc ', [' def ', ' Ghi ']]
List of basic operations-get length, index, shard and original modification
In [all]: L = [' spam ', ' spam ', ' spam! ']
in [+]: Len (L)
OUT[78]: 3
In []: l[1]
OUT[79]: ' Spam '
In []: L[0:2]
OUT[80]: [' spam ', ' spam ']
In [Bayi]: l[0:2] = [' eat ', ' more ']
in [[+]: L
OUT[82]: [' eat ', ' more ', ' spam! ']
List of methods, see the idea ibid., here slightly.
Dictionary type
It is a series of key-value pairs read by keys, also known as associative arrays or hash lists, unordered collections of arbitrary types of objects, variable size, and can be nested (variable mappings).
Create a dictionary
in [+]: D = {}
in [[]: D = {' spam ': 2, ' Eggs ': 3}
in [+]: D = dict (name= ' Bob ', age=42)
The basic operation of the dictionary-get length, access to an item and modify it in situ
In []: D = {' spam ': 2, "Ham": 1, "Eggs": 3}
In [the]: Len (D)
OUT[93]: 3
In [94]: d[' spam ']
OUT[94]: 2
In [up]: d[' spam '] = 4
In []: del d[' eggs ']
In []: D
OUT[97]: {' Ham ': 1, ' spam ': 4}
Dictionary of methods, slightly.
Tuple type
It is an ordered set of objects of any type and is immutable in size (immutable sequences).
Creating tuples
In [98]: T = ()
In [©]: T = (4,)
in [+]: T = (' A ', ' B ', ' C ')
In [102]: T = ' A ', ' B ', ' C '
Basic operation of tuples-get length, index and Shard
In [104]: T = (' A ', ' B ', ' C ', ' d ')
in [106]: Len (T)
OUT[106]: 4
In [107]: t[2]
OUT[107]: ' C '
In [108]: T[1:3]
OUT[108]: (' B ', ' C ')
Tuple's method, slightly.
Collection type
It is a unique, unordered set of immutable objects; An item can appear only once in the collection, regardless of how many times it is added, which supports operations that correspond to the mathematical set theory.
Create a Collection
in [+]: set (' spam ')
OUT[110]: {' A ', ' m ', ' P ', ' s '}
In [123]: {' A ', ' m ', ' P ', ' s ', ' a '}
Out[123]: {' A ', ' m ', ' P ', ' s '}
Basic operations for collections-get length, intersection, set, and difference sets
In [S1]: = {1, 2, 3, 4}
In [117]: S2 = {3, 4, 5}
In [118]: Len (S1)
OUT[118]: 4
In [119]: S1 & S2
OUT[119]: {3, 4}
In [122]: S1 | S2
OUT[122]: {1, 2, 3, 4, 5}
In [121]: S1-S2
OUT[121]: {1, 2}
Collection of methods, slightly.
For other types of objects such as files, this is not explained, as the study of the depth, slowly will come into contact.
With this summary, you can see that a built-in function like Dir () or Len () is available in Python, as well as a method call like S.upper () or D.keys (), so what's the difference between the two?
For many types of general-purpose operations, they are provided in the form of built-in functions or expressions (such as Len (S), s[0]); For a particular type of operation, it is provided in the form of a method call (for example, S.upper ()).
Type Object classification
Categorized by Access method
1. Numbers-support addition and multiplication
2. Sequence (string, list, tuple)-Supports indexes and shards
3. Mapping-access by key value
According to the variability classification
1. Immutable (numbers, strings, tuples)-not supported in situ modification
2. Variable (list, dictionary, collection)-Supports original modification
If interested, you can follow the subscription number "database Best practices" (Dbbestpractice).
Python Basics Grooming-part No. 02