a basic data type python

A basic data type

Integer: In using the keyword int to represent python3

Int 32-bit machine in the range: -2**31 2**31-1,即-2147483648~2147483647

Int 64-bit machine in the range: -2**63~2**63-1,即-9223372036854775808~9223372036854775807

In python3 are all integer int. However, if a large amount of data. Python2 will be used in the long type.

Absence of long type integers can be carried out in the operation python3 + - * /% // **

Binary to decimal: left to right to go with each binary number multiplied by the corresponding power of 2, after the decimal point is from left to right

General formula is:

abcd.efg(2)=d*2^0+c*2^1+b*2^2+a*2^3+e*2^-1+f*2^-2+g*2^-3(10)

例1101.01转换为十进制
1101.01(2)=1*2^0+0*2^1+1*2^2+1*2^3 +0*2^-1+1*2^-2=1+0+4+8+0+0.25=13.25(10)

Decimal to Binary: decimal integer into a binary integer a "modulo 2 addition, reverse arrangement" method. Specifically: with 2 divisible decimal integer, you can get a quotient and a remainder; then 2 removal providers, will obtain a quotient and a remainder, so until the supplier is smaller than up to 1, then the remainder of the first obtained as binary the least significant bit of the remainder obtained as the higher significant bits of the binary number, lined up sequentially.

18/2=9 余 0
9/2=4 余 1
4/2=2 余0
2/2=1 余0
1/2=0 余1
18用二进制表示为 10010

Seek time occupied decimal conversion into binary digits are available: Print (num.bit_length ())

num = 18
print(num.bit_length())

Boolean value

Conditions for Use True (true) False (false)

Digital conversion to a Boolean value:

print(bool(-10))   #结果 True   规律:非0即True

Convert a string to a Boolean value:

print(bool(''))    #结果为 False 规律:非空即True(字符串里全空,空格也没有)

Boolean value to a string:

print(str(True))    #True type 为字符串

Boolean values ​​are converted to numbers:

print(int(True))    #结果为1    False转换结果为0

String

For storing data, but smaller than

' '

" "

''' '''

""" """

Inside are strings

String indexing and slicing

name = "张三,Zain01,imm_ky"
#索引   从左往右数 01234...  相当于从0开始从左往右给元素编号,也可从右往左:-1-2-3-4.. 查找时超出范围会错. 
#字符串,列表,元组  --  都是有索引(下标)
print(name[1])
print(name[1:10:3])     # 切片:[起始位置:终止位置:步长]  顾头不顾尾 查找的时候超出范围没事
#步长:正负决定查找的方向 大小决定查找的间隔 默认时1 默认方向为从左到右 如-1是从左至右
#先定义一个范围  再确定方向
print(name[::-1]   #倒序输出
print(name[:4])    #开始到3号索引即第四个元素
 #[a:b:c] 前两位是去一个范围 a b  可正可负  c的符号决定了输出方向   

Common character string processing method

1. ALL CAPS upper

name = "absjdfg"
print(name.upper())

All lowercase lower

name = "SADFWSSS"
print(name.lower())

2. Check the beginning startswith

name = "alex"
print(name.startswith('a'))  #结果为 True

View the end endswith

name = "alex"
print(name.endswith('a'))   #结果为False

3.count statistical occurrences of an element or content

name = "sdsdsdsdaadwfqs"
print(name.count('sd'))   

4. Alternatively replace

name = "sdsdsdsdaadwfqs"
print(name.replace('d','A',5))  #括号内前面为要被替换的内容,中间为新内容,数字为替换次数,不填默认全换

5. The spaces strip head and tail removed

name = '  imm_ky/   '
print(name.strip())         #默认去掉空格和换行符,可以写自己想换的内容.

6. After the split split split get a list

name = "a l e x"
print(name.split())         #默认以空格分隔,也可自定义分割

7. The format string formatting

name = 'alex{}wusir{}'
name1 = name.format('去玩了','在上班')        #按顺序对应填充
name = 'alex{0}wusir{1}'
name1 = name.format('去玩了','在上班')        #按索引位置填充
name = 'alex{a}wusir{b}'
name1 = name.format(a='去玩了',b='在上班')      #指名填充

8.is Series judgment

name = '2343②'
print(name.isdigit())   # isdigit   判断是不是阿拉伯数字
name = '385'
print(name.isdecimal())   # isdicimal 判断是不是十进制 一般用它判断是不是数字
name = 'inmm汉字'
print(name.isalpha())   # isalpha 判断是不是由字母和汉字组成
name = 'inmm汉字985'
print(name.isalnum())   #isalnum 判断是不是右字母、数字、汉字组成.

Usage join (string obtained)

str.join () str symbols to be inserted, as the operation target list, tuples, dictionaries can operate brackets to insert the result is the string str spaced

li = ['a','b','c','d']
s = ':'.join(li)   # 用冒号分开各元素
>>> 'a:b:c:d'   #得到结果为一个字符串

len () function

It may be used for counting the number of string elements

s = '今晚打老虎'
print(len(s))    #  结果为5

for loop

for i in s:
    print(i)       

for keywords, i variable, s iterables (all except int.bool)

s = 'alex'
for i in s:
    pass # 过  占位符
    ``` #同pass
print(i)             
#注意和上边的区别,最终结果为'x',因为对于for循环会根据索引给i赋值为s内的元素,直到最后一个变量(正常完成循环情况下),因此最终print(i)会输出最后一个元素.
                    

Take a range of the number range

python3 print it themselves

python2 is a list of print

for i in  range(1,10,2): # (起始位置,终止位置,步长)(顾头不顾尾)
    #range(10) 指定了终止位置,起始位置默认是0
    print(i)

Guess you like

Origin www.cnblogs.com/banshannongchang/p/11006368.html