Explanation of new features you should know about python 3.9

  • 2021-11-02 01:42:32
  • OfStack

Directory 1. Number Type 2. String 3. List Type 4. for Loop

1. Number type

python not only supports the original int and float types, but also supports Decimal or Fraction. python also has built-in support for complex numbers, and the suffix j or J is used to identify imaginary numbers.

2. String

If a string has single quotation marks but no double quotation marks, double quotation marks are enclosed outside the string; Otherwise, single quotation marks are enclosed. The output of the print () function is simpler and more readable, omitting the quotation marks and outputting the escaped special characters.


>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> print('"Isn\'t," they said.')
"Isn't," they said.
>>> s = 'First line.\nSecond line.'  # \n means newline
>>> s  # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s)  # with print(), \n produces a new line
First line.
Second line.

If you don't want the preceding\ character to be translated into a special character, you can use the original string by adding r before the quotation marks.


>>> print('C:\some\name')  # h Here's \n Will be considered as escape characters 
C:\some
ame
>>> print(r'C:\some\name')  # Marked as before escaping r
C:\some\name

Strings can be merged into 1 using +, or they can be copied multiple times using *


>>>3 * 'mmm'  +  ' qqq'
>'mmmmmmmmmqqq'

Two or more adjacent strings are automatically merged


>>>'Py' 'thon'
> ' Python'

This feature is especially useful when splitting long strings:


>>>('Put several strings within parentheses '
...         'to have them joined together.')
>>>text
>'Put several strings within parentheses to have them joined together.'

This function can only be used for two literals, not variables or expressions:


>>> prefix = 'Py'
>>> prefix 'thon'  # can't concatenate a variable and a string literal
  File "<stdin>", line 1
    prefix 'thon'
                ^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
  File "<stdin>", line 1
    ('un' * 3) 'ium'
                   ^
SyntaxError: invalid syntax

To merge multiple variables or merge variables with literals, use "+":


>>> prefix + 'thon'
'Python'

Strings support indexing, both forward and reverse indexing.


str[0]  str[1]  str[-1]
# Why do reverse indexes derive from -1 Begin ?
#-0  And  0  Equality refers to the same 1 A value. 

python supports slicing operations. Indexes can extract individual characters, and slicing operations can extract strings.


>>> word[0] 
'P'
>>> word[2:5] 
'tho'

The output of the python slicing operation contains the slicing start, but does not contain the slicing end. So s [: i] + s [i:] is always equal to s


>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'

python can automatically handle out-of-bounds indexes:


>>> print('C:\some\name')  # h Here's \n Will be considered as escape characters 
C:\some
ame
>>> print(r'C:\some\name')  # Marked as before escaping r
C:\some\name
0

The python string is immutable, so copying directly to an index will report an error. If you want to change the value of a string, you must create a new string.

3. List type

The python list is a magical type, and the elements added to the list can be of different data types. Like python string 1, python list also supports indexing and slicing operations.

The slicing operation returns a new list containing the requested elements. A shallow copy of the list is returned.


>>> print('C:\some\name')  # h Here's \n Will be considered as escape characters 
C:\some
ame
>>> print(r'C:\some\name')  # Marked as before escaping r
C:\some\name
1

Deep copy:


>>> print('C:\some\name')  # h Here's \n Will be considered as escape characters 
C:\some
ame
>>> print(r'C:\some\name')  # Marked as before escaping r
C:\some\name
2

Summary:

Variable type Vs immutable type
Variable type (mutable): list, dictionary
Immutable types (unmutable): Numbers, strings, tuples

A deep copy will make a copy of the variable object, but a shallow copy will not.

The above example is a copy of the list (mutable objects). What about tuples, characters and other indispensable objects?

The answer is that for immutable objects, there is no problem of deep and shallow copies. No matter how you copy it, the effect is to create a new pointer to an immutable object.


import copy
a = (1,2)
b = copy.copy(a)
c =copy.deepcopy(a)
print(b == c)
print(id(b)==id(c))

lista = [5,6]
listb = copy.copy(lista)
listc = copy.copy(lista)
print(listb == listc)
print(id(lista) == id(listb))

Output:

True
True
True
False


>>> print('C:\some\name')  # h Here's \n Will be considered as escape characters 
C:\some
ame
>>> print(r'C:\some\name')  # Marked as before escaping r
C:\some\name
4

4. for Loop


>>> print('C:\some\name')  # h Here's \n Will be considered as escape characters 
C:\some
ame
>>> print(r'C:\some\name')  # Marked as before escaping r
C:\some\name
5

Output:

2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3


Related articles: