Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get started with Python programming to quantify AI full Stack

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how to get started with Python programming to quantify the AI stack. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Python uses backslashes to escape quotation marks and other special characters for accurate representation.

If the string contains single quotes but no double quotes, the string is enclosed in double quotes, otherwise it is enclosed in single quotes. For such an input string, the print () function produces a more readable output.

Literal strings across lines can be represented in the following ways. Use a continuation character, that is, a backslash after the last character of each line to indicate that the next line is a logical continuation of the previous line: introduction to quantitative AI full-stack Zero Foundation for NetEase Cloud classroom Python programming

The following uses\ nto add a new line:

>'"Isn\ 't," she said.' '"Isn\ 't," she said.' > print (' "Isn\ 't," she said.') "Isn't," she said. > s = 'First line.\ nSecond line.' #\ n means a new line > s # does not use print (),\ ninclude in the output' First line.\ nSecond line.' > print (s) # use print (),\ noutput a new line First line. Second line.

The following uses a backslash (\) to continue the line:

Hello = "This is a rather long string containing\ n\ several lines of text just as you would do in C.\ n\ Note that whitespace at the beginning of the line is\ significant." Print (hello)

Notice that the newline character is still represented by\ nThe newline character after the backslash is discarded. The output of the above example is as follows:

This is a rather long string containing several lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant.

Alternatively, the string can be enclosed by "" (three double quotes) or "'" (three single quotes). When using three quotes, line breaks do not need to be escaped, they are included in the string. The following example uses an escape character to avoid creating an unwanted blank line in the first place.

Print (""\ Usage: thingy [OPTIONS]-h Display this usage message-H hostname Hostname to connect to ")

The output is as follows:

Usage: thingy [OPTIONS]-h Display this usage message-H hostname Hostname to connect to

If we use the "original" string,\ nit will not be converted to a newline, and the backslash at the end of the line, as well as the newline character in the source code, will be included in the string as data. For example:

Hello = r "This is a rather long string containing\ n\ several lines of text much as you would do in C." Print (hello)

Will output:

This is a rather long string containing\ n\ several lines of text much as you would do in C.

Strings can be concatenated with a + operator string or repeated with the * operator:

> word = 'Help' +' A'> word 'HelpA' >'

Two adjacent literal strings will be concatenated automatically; the first line of the above example can also be written as word = 'Help'' A'; this operation is only valid between two literal values and cannot be used arbitrarily in string expressions:

> 'str'' ing' # > > 'str'.strip () +' ing' # > 'str'.strip ()' ing' # > > word [4]'A'> word [0:2] 'He' > word [2:4]' lp'

The default slicing index is useful: the default first index is zero and the second index defaults to the length of the string that can be sliced.

> word [: 2] # the first two characters' He' > word [2:] # all characters after 'lpA' except the first two characters

Unlike C strings, Python strings cannot be changed. Assigning a value to an index location results in an error:

> word [0] ='x 'Traceback (most recent call last): File "", line 1, in? TypeError: 'str' object does not support item assignment > > word [: 1] =' Splat' Traceback (most recent call last): File ", line 1, in? TypeError: 'str' object does not support slice assignment

However, it is simple and efficient to create new strings by combining content:

>'x' + word [1:] 'xelpA' >' Splat' + word [4] 'SplatA' has a very useful rule: s [: I] + s [I:] equals s. > > word [: 2] + word [2:]' HelpA' > > word [: 3] + word [3:] 'HelpA'

The treatment of skewed sliced indexes is also elegant: an oversized index will be replaced by the size of the string, and an upper limit less than the lower limit will return an empty string.

> word [1word 100] 'elpA' > word [10:] > word [2:1]

Negative numbers can be used in the index, which will be counted from right to left. For example:

> word [- 1] # the last character'A'> word [- 2] # the penultimate character'p' > > word [- 2:] # the last two characters' pA' > word [:-2] # except for the last two characters, all the characters in front of them 'Hel', but note that-0 and 0 are exactly the same, so-0 will not be counted from the right! > word [- 0] # (since-0 equals 0)'H'

Negative indexes that are out of range will be truncated, but do not try to use them in a single-element index (non-sliced index):

> word [- 100:] 'HelpA' > word [- 10] # error Traceback (most recent call last): File ", line 1, in? IndexError: string index out of range

There is a way to remember how the sharding index works. Imagine that the index points between characters, and the number to the left of the first character is 0. Next, to the right of the last character of a string with n characters is index n, for example:

+-- + | H | e | l | p | A | +-- + 0 1 2 3 4 5-5-4-3-2-1

The number 0.5 in the first line gives the position of the index in the string; the second line gives the corresponding negative index. The slitting section consists of all the characters marked I and j at the edge from I to j, respectively.

For the non-negative slicing part, if the index is all within the valid range, the length of the slicing part is the difference of the index. For example, the length of word [1:3] is 2.

The built-in function len () returns the length of a string:

> s = 'supercalifragilisticexpialidocious' > len (s) 34 the above is how to get started with Python programming to quantify the AI stack. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report