Python data type, python
Data Type unit conversion
-
The smallest unit is bit, indicating a binary value of 0 or 1. Generally, it is represented by lowercase B.
-
The minimum unit of storage is Byte, 1B = 8b
-
1024B = 1KB
-
1024KB = 1 MB
-
1024 MB = 1 GB
-
1024 GB = 1 TB
-
1024 TB = 1 Pb
-
2 ^ 32 = 2 ^ 10*2 ^ 10*2 ^ 10*2 ^ 2 = 4 GB
Hexadecimal conversion
Data Type
-
Data Types exist to process different operations,
-
Data Types in python include integer, floating point, String, list, tuples, Dictionary, set, and null.
-
INTEGER (int): an integer in mathematics.
-
Float: refers to the decimal point in mathematics.
# Decimal point (floating point number)
B = 3.14
Print (type (B ))
# Scientific notation
C = 3.14e-3
Print (c)
-
Boolean: corresponds to the true and false logic in life
# Bool type, only two values, True and False
D = False
Print (type (d ))
-
NULL (NoneType): a special type with only one value (None)
-
String (str): a string of characters enclosed in quotation marks.
-
Either single or double quotation marks are allowed, or three single or double quotation marks are allowed.
-
Double quotation marks in single quotation marks and single quotation marks in double quotation marks must be escaped.
-
Escape characters: newline (\ n), tabulation (\ t), etc.
-
Sometimes '\' also needs to be escaped
-
Line feed processing when a line cannot be displayed
g = 'hello' \
' world'
print(g)
h = 'hello \
world'
print(h)
i = '''
abc
def
123
'''
print(i)
# No matter what character, it is a length
S = 'Hello Day'
Print (len (s ))
# Directly splice
S2 = 'hello' + 'World'
Print (s2)
# Repeat the specified string several times and splice it together
S3 = 'abc' * 3
Print (s3)
S4 = '000000'
# Extract from the beginning, and the following table starts from 0
Print (s4 [0])
# Extract from the end. The following table starts from-1.
Print (s4 [-2])
S5 = 'abcdefg'
# S [start table: end table], including the start table elements, excluding the end table elements
Print (s5 [0: 3])
# Extract from the beginning to the specified position
Print (s5 [: 4])
# Extract from the specified table to the end
Print (s5 [3:])
# The following table starts from 1 and is extracted to the second to the last, excluding the second to the last.
Print (s5 [1:-2])
# Extract the last 4 characters
Print (s5 [-4:])
# Extract all
Print (s5 [:])
# Step value. The default value is 1.
Print (s5 [: 2])
# Reverse string order
Print (s5 [:-1])
Name = 'gouda'
Like = 'wangdahua'
Age = 18
Print ('My name is % s, crush % s, she % d years old '% (name, like, age ))
# Character => ASCII
Print (ord ('A '))
# ASCII => character
Print (chr (65 ))
Print ('My name is {}, I like {}, {} years old this year '. format (name, like, age) print ('My name is {2}, I like {1}, {0} years old this year '. format (name, like, age) print ('My name is {nm}, I like {lk}, {a} years old this year '. format (nm = name, lk = like, a = age ))
> % S: placeholder for the string
>
> % D: placeholder for integer data
>
> % F: placeholder for Floating Point Numbers
>
> % C: placeholder for characters
>
> • ASCII:
>
> • 0 ~ Am ~ 57
>
> • ~ Z: 65 ~ 90
>
> • ~ Z: 97 ~ 122
-List: defined by [], which can store any type of data
-Values: lt [0] And lt [-1]
-The list can be modified. lt [1] = 'min min'
-Out-Of-Boundary Problems
-You can also perform '+' and '*' operations.
-You can also perform slicing.
-Lt [:] = [] indicates clearing the list
-Lt [: 2] = []: Delete the first two elements.
-Tuple: defined by (). Other operations are the same as those in the list.
-The tuples cannot be modified.
-When there is only one element, add a comma after the element.
-Set: defined,
-Empty sets cannot be defined using braces (left for dictionary) and set ().
-The set cannot be modified.
-Unordered element in the Set
-The elements in the set cannot be repeated.
-Easy to use
'''Python
S = {'Li Bai ', 'du Fu', 'bai juyi', 'su Shi ', 'Tao Yuanming', 'Li Bai '}
S2 = {'Li Bai ', 'shang Yin', 'du Mu ', 'su Shi '}
Print (type (s ))
Print (s)
# Intersection
Print (s & s2)
# Union
Print (s | s2)
# Difference set
Print (s-s2)
Print (s2-s)
# Empty set defined in this way, cannot be used {}
S3 = set ()
Print (type (s3 ))
-
Dictionary (dict): defined {}
-
Elements are composed of key-value pairs.
-
Use ':' To connect keys and values.
-
The keys in the dictionary are unique and the values can be arbitrary.
-
The dictionary is variable.
-
Easy to use:
D = {'name': 'xiaoming', 'age': 18}
Print (d)
Print (type (d ))
# Extract the corresponding value based on the key
Print (d ['name'])
# If the key does not exist, an error is returned.
# Print (d ['abc'])
# If a key exists, the corresponding value is returned. If the key does not exist, None is returned. You can also customize the default value.
Print (d. get ('abc', 'default '))
# Add a key-value pair if no key-Value Pair exists
# D ['hello'] = 'World'
# Modify an existing one
D ['name'] = 'xiaofang'
# Deleting an element
# Del d ['name']
# Print (d)
# Len returns the number of key-value pairs
Print (len (d ))
-
Forced type conversion
Int () # convert to integer
Float () # convert to floating point
Str () # convert a string
List () # convert to list
Tuple () # convert to tuples
Set () # convert to a set
Dict () # convert to Dictionary
L = [('name', 'xiaoming'), ('age', 18)]
# Convert a list to a dictionary
D1 = dict (l)
Print (d1)
List deduplication, list => set => List
-
Get variable address
a = 10
print(id(a))
Do not go into the address of the Variable