The data type

  1. String, String, immutable
  2. The Number is immutable
  3. The List, the List
  4. Tuple, immutable
  5. The Set,
  6. The Dictionary, the Dictionary

Q: List, the Tuple, the Set, what is the Dictionary?

A: A List is similar to an Array in JS. The types of elements in A List can be different. Represents [‘him’, 25, 100, ‘her’]

Tuple is similar to List in that elements of a Tuple cannot be modified. Expressed as (1991, 2014, ‘physics’, ‘math’)

A Set, like ES6, is an unordered Set of non-repeating elements. {‘Tom’, ‘Jim’, ‘Mary’, ‘Tom’, ‘Jack’, ‘Rose’}

Similar to objects in JS, Dictionary has unordered key-value pairs. Represented as {‘Jack’:1557, ‘Tom’:1320, ‘Rose’:1886}

Q: How is immutable defined?

A: If the value of A variable changes, the address of the variable must change

Q: How do I print the memory address?

A: Built-in function id()

Q: How do I print data types?

A: Built-in function type()

Q: == how do I find no Boolean types?

A: Python2 does not have Boolean types, which use the number 0 for False and 1 for True. In Python3, True and False are defined as keywords, but their values are still 1 and 0, and they can be added to numbers.

Elaborate in detail

1. String

s = "ddd"   # or s = STR (" DDD ")
sid = id(s)
s = 'x1x222'
sid2 = id(s)
print(sid == sid2)  #False

print(s[0:2]) # truncate string -->1
print(s[-2: -1]) # intercept string -->2
print(s[1:)# 1 x222 slice

Copy the code

2. Number

# Python number types have the following subset
a,b,c,d = 20.5.5.True.2+3j # Multiple variable assignments
print(type(a),type(b),type(c),type(d)) # a,b,c,d = int,float,bool
You can also use the built-in isinstance() function for type determination
isinstance(a,int)
# isinstance() The second argument can be a tuple
isinstance(a,(str.dict.list)) Check if a is a member of a tuple (STR,dict,list,tuple)
Copy the code

Q: What is the difference between type() and isinstance()?

A: The old rules are analogous to typeof and Instaceof in JS. In JS typeof can only accurately determine the underlying data type (null = object),

The application type is object(function). Instaceof is an instance that determines whether a variable belongs to an object.

Type () does not consider a subclass to be a superclass type, regardless of inheritance; isinstance() does the opposite.

3. List

The way to define a list
list1 = ["Chorme"."Firfox"."Safari"]
list2 = list([1.2.3])


arr = [1.2.3]
copy_arr = arr[:]
print(id(arr) == id(copy_arr))  # False learns that slicing is actually copying the list
Copy the code

4. Tuple

t1 = (1.)# create tuple with only one element
t2 = tup([1.2.3.4])

t3 = t1 + t2 # merge tuples
print(t2[1]) Read the tuple element
del t2       # delete tuple


The built-in functions include
# len(tuple) counts the number of tuple elements.
# Max (tuple) returns the maximum number of elements in a tuple.
# min(tuple) returns the minimum value of an element in a tuple.
# tuple(seq) converts a list to a tuple.
Copy the code

5. Set

s1 = set([1.1.2.3.4])

print(list(s1)[0]) Set does not have an index

The built-in functions include

# add() adds elements to the collection
# remove() removes elements from the collection
# update() updates the collection
# len() returns the length of the collection
# copy() returns a shallow copy of the collection
# clear() removes all elements in the collection

Copy the code

6. Dictionary

d = {'name':'2U'.'age':24}
d2 = dict(name="a", age=12)
d3 = dict(zip(['name'.'age'], ['han'.'16']))
d4 = dict([('name'.'dd'), ('age'.18)])

# Built-in functions

# clear() removes all elements in the dictionary
# copy() returns a shallow copy of the dictionary
# get() returns the value of the specified key
# item() returns a traversable array of (key, value) tuples as a list
# keys() returns a list of all the keys of a dictionary
# update() updates the new dictionary's key/value pairs to the dict
# values() returns all the values in the dictionary as a list

Copy the code