python and numeric string based 2--

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/EngineerHe/article/details/98851188

2 basis python

Here Insert Picture Description

type of data

python data types include a digital type, string type, a list (List), tuple (tuple), the dictionary (dict), set (SET), the other (Boolean), may be used in python type()function to see if the data Types of

>>> s = "hello world"
>>> num = 666
>>> type(s)
<class 'str'>
>>> type(num)
<class 'int'>
>>>

Integer type

Numeric types include integer, floating point, complex numbers, as well as binary, octal, hexadecimal;

integer in python, and other languages ​​different is that he can represent large numbers, inside the python will be processed automatically; if the number exceeds the number of bytes occupied will overflow in other languages, but not python

>>> num = 666666666666666666666666666666666666666666
>>> print(num)
666666666666666666666666666666666666666666

If an error occurs in the C / C ++ in

int i = 66666666666;
cout << i  << endl;
// 结果为-2052810070

By binary integer type (0b), octal (0o), hexadecimal notation (0x)

# 注意,这里0b,0o,0x前面的是数字零不是字母‘o’,然后八进制是数字‘0’和字母‘o’,没有大小写的区别。
>>> a, b, c = 0b11, 0o11, 0x11
>>> print(a, b, c)
3 9 17

Floating-point, where the scientific notation, with the 10 ein place, ecase insensitive, E,eand can be; rules to note epreceding figures have to be, ethe latter must beInteger

>>> a = 6.66
>>> b = 0.666e2
>>> print(a, b)
6.66 66.6

Complex, which is a real part and an imaginary part, general form x+yj, where x is a real part of the complex, y is the imaginary part of the complex, where x and y are real numbers, j where uppercase, lowercase can

>>> a = 1+2j
>>> b = 1+2J

String

Strings or string (String) is a string of characters consisting of numbers, letters, the underscore, single quote is == 'Or double quotes"Any text enclosed, or may be three marks (’’’,or"" "==) with three long marks for inputting contents can freely wrap; single-character type is not supported in python, according to the single-character string is used in python.

>>> a = 'this is a string\n'
>>> b = "this is a string\n"
>>> c = '''this is a long long
long string \n'''
>>> print(a, b, c)
this is a string
this is a string
this is a long long
long string 

python access strings taken by square brackets, can be entered directly index, a method may be used continuously acquired slices elements can also be accessed from the back, where a -1 is the last element represents the penultimate -2, syntax 变量[头下标:尾下标]is: ,

>>> s = "this is a string"
>>> print(s)
this is a string
>>> print(s[6])
s
>>> print(s[-1])
g
>>> print(s[3:9])
s is a

Two strings commonly used operators, +the string concatenation operator and *repeat

>>> s = 'hello'
>>> print(s)
hello
>>> s1 = s + ' world'
>>> print(s1)
hello world
>>> s2 = s * 3
>>> print(s2)
hellohellohello

String with quotation marks, then how the output of a single quotation mark it? Use the escape character\

>>> s = 'I\'m a string'
>>> print(s)
I'm a string

Common escape character, \nnewline, \ttab character, the character \itself escape \\
`

>>> s = 'I\'m a string'
>>> print(s)
I'm a string

Common escape character, \nnewline, \ttab character, the character \itself escapes\\

Guess you like

Origin blog.csdn.net/EngineerHe/article/details/98851188