Basic data types - strings and numbers

Basic data types----string (2018/5/3 12:01:16)
Definition and creation of strings A

string is an ordered collection of characters used to store and represent basic text information, ' ' Or '' '' or ''' ''', the content contained in the middle is called a
string Characteristic and common operations of character string

Characteristic:

1. Define the character set in the order from left to right, and the subscript is accessed sequentially from 0 , orderly



![](6ecc95bf-9d9e-49c7-9bcb-af8dd53946eb_files/d2542a0bb1ba23e284f41b10d2cfa518.png)
Added:

1. The single and double quotation marks of the string cannot cancel the meaning of special characters, if you want to cancel all the characters in the quotation marks For special meaning, add r before the quotation marks, such as name=r'l\thf'

2. The unicode string must be used in conjunction with r before r, such as name=ur'l\thf'

Common operations:



#index
>>> s = 'hello'
>>> s[1]
'e'
>>> s[-1]
'o'
>>> s.index('e')
8 #Search
: if it exists, return a normal index, if it does not, return a negative number
> >> s.index('e' )
11
>>> s.find("e")
13
>>> s.find("i")
-1
#移除空白
>>> s = '  hello,world!  '
>>> s.strip()
'hello,world!'
>>> s.lstrip()
'hello,world!  '
>>> s.rstrip()
'  hello,world!'
>>> s2 = '***hello,world!***'
>>> s2.strip('*')
'hello,world!'

#长度
>>> s = 'hello world'
>>> len(s)
11



#替换
>>> s = 'hello world'
>>> s.replace('h','H')
'Hello world'


#切片 ::2代表步长
>>> s = 'hello world'
>>> s[0:2]
'he'
>>> s[::2]
'hlowrd' Boolean ( bool) basic data type---number (2018/5/3 11:45:11) 'ello world' >>> s[-10:] 'hello world'
>>> s[-11:]










The bool type has only two values: True and False.
The reason why bool values ​​are classified as numbers is because we are also used to using 1 for True and 0 for False.
Integer Integers
in Python belong to the int type, which is represented by decimal by default. In addition, binary, octal, and hexadecimal representations are also supported.
The number in the base conversion
python is still decimal by default. Some methods are also provided to help us do the conversion. For example, when converting from hexadecimal to binary, use the bin method, and add '0b' in front of the conversion result to indicate that it is a binary book.
! [] (6ecc95bf-9d9e- 49c7-9bcb-af8dd53946eb_files / 9f42f9cc-a2d2-4b46-8301-56ff62cffac7.png)
float

! [] (6ecc95bf-9d9e- 49c7-9bcb-af8dd53946eb_files / 0db759a533c971a25e3b5b773664a5f5.png)
Floating point numbers are A numerical representation of a number belonging to a particular subset of rational numbers, used in computers to approximate any real number. Specifically, this real number is obtained by multiplying an integer or fixed-point number (ie, the mantissa) by an integer power of a base (usually 2 in computers), which is similar to base 10 scientific notation.
Why is it called float floating point?
Floating-point numbers are also decimals. The reason why they are called floating-point numbers is that when expressed in scientific notation,
the position of the decimal point of a floating-point number is variable. For example,
1.23*109 and 12.3*108 are equal.
Floating point numbers can be written mathematically, such as 1.23, 3.14, -9.01, and so on. But for very large or small floating-point numbers, you must use scientific notation, replacing 10 with e:
1.23*109 is 1.23e9, or 12.3e8, 0.000012 can be written as 1.2e-5, etc.
The way integers and floating-point numbers are stored inside the computer is different. Integer operations are always exact while floating-point operations may have rounding errors.
Regarding the inaccuracy of decimals
, Python defaults to 17 digits of precision, that is, 16 digits after the decimal point. Although there are 16 digits, this accuracy becomes more and more inaccurate.
When our calculations need to use higher precision (more than 16 decimal places) what to do?
![](6ecc95bf-9d9e-49c7-9bcb-af8dd53946eb_files/52190b1a-9c2e-4259-afda-31586fcd47bc.png)

Complex numbers

From the above figure, we can see that complex numbers are composed of real numbers and imaginary numbers.
To understand complex numbers , In fact, about complex numbers, you need to understand imaginary numbers first. Imaginary numbers (that is, false and unreal numbers): A number whose square is a complex number is called an imaginary number.
A complex number is a number that can be written in the form a+bi, where a and b are real numbers and i is the imaginary unit (i.e. root of -1). In the complex number a+bi, a is called the real part of the complex number, b is called the imaginary part of the complex number (an imaginary number refers to a number whose square is negative), and i is called the imaginary number unit.
When the imaginary part is equal to zero, the complex number is a real number; when the imaginary part is not equal to zero, the complex number is called an imaginary number.
Note that the letter j in the imaginary part can be both uppercase and lowercase.






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325230039&siteId=291194637