标签:res which mem 格式 指针 hex 对齐 read insert
目录
type()
可直接查看变量类型
补充:
>>>dict(name = 'aloha', food = 'apple pie', id = '0')
{'name': 'aloha', 'food': 'apple pie', 'id': '0'}
python from fractions import Fraction fractions.Fraction(a,b) # a为分子,b为分母
upper()
: 返回全部转换为大写的原字符串lower()
: 返回全部转换为小写的原字符串capitalize()
: 返回句首字母大写的原字符串title()
: 返回每个单词的首字母大写的原字符串is_alpha()
: 检查是否全为字母is_digit()
: 检查是否全为数字strip()
lstrip()
: 同strip()
不过只作用于字符串左部rstrip()
: 同strip()
不过只作用于字符串右部count()
: 计算子字符串出现次数find()
: 查找子字符串第一次出现的位置。如果没找到,则返回-1replace(str1, str2)
: 将所有子字符串str1用str2代替python中赋值是创建一个引用的过程(在Python中,从变量到对象的连接称作引用)。变量实际上是指向对象的一个指针。可以用变量指向(也许是引用)任何类型的数据。
根据右值可将赋值分为两种
注:引用可以通过del删除。
Python中的列表可以起到类似指针的效果。
a = [1, 2, 3]
b = a
a[0] = '1'
print(b)
['1', 2, 3]
在修改a[0]
后,b[0]
也被修改了。
a[0] = ‘1‘
修改的是内存中列表的第一个元素。a
和b
的值都没变,但内存中列表的值发生了改变。
标准格式:
%[(name)][flags][minimumwidth][.precision]typecode
‘d‘
| Signed integer decimal. | |‘i‘
| Signed integer decimal. | |‘o‘
| Signed octal value. | (1) |‘u‘
| Obsolete type – it is identical to ‘d‘
. | (6) |‘x‘
| Signed hexadecimal (lowercase). | (2) |‘X‘
| Signed hexadecimal (uppercase). | (2) |‘e‘
| Floating point exponential format (lowercase). | (3) |‘E‘
| Floating point exponential format (uppercase). | (3) |‘f‘
| Floating point decimal format. | (3) |‘F‘
| Floating point decimal format. | (3) |‘g‘
| Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |‘G‘
| Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |‘c‘
| Single character (accepts integer or single character string). | |‘r‘
| String (converts any Python object using repr()
). | (5) |‘s‘
| String (converts any Python object using str()
). | (5) |‘a‘
| String (converts any Python object using ascii()
). | (5) |‘%‘
| No argument is converted, results in a ‘%‘
character in the result. | |Notes:
- The alternate form causes a leading octal specifier (
‘0o‘
) to be inserted before the first digit.- The alternate form causes a leading
‘0x‘
or‘0X‘
(depending on whether the‘x‘
or‘X‘
format was used) to be inserted before the first digit.- The alternate form causes the result to always contain a decimal point, even if no digits follow it.
The precision determines the number of digits after the decimal point and defaults to 6.- The alternate form causes the result to always contain a decimal point, and trailing zeroes are not removed as they would otherwise be.
The precision determines the number of significant digits before and after the decimal point and defaults to 6.- If precision is
N
, the output is truncated toN
characters.- See PEP 237.
flags
python i = 4.53459 print(r‘%-10.5f: ‘, ‘...%-10.5f...‘%(i)) # - 左对齐 print(r‘%+10.5f: ‘, ‘...%+10.5f...‘%(i)) # + 显示符号 print(r‘%010.5f: ‘, ‘...%010.5f...‘%(i)) # 0 空位补零
%-10.5f: ...4.53459 ... %+10.5f: ... +4.53459... %010.5f: ...0004.53459...
*
表明两者的值从%
运算符后获取%f
,则默认保留6位小数点后数字i = 4.53459
print(r'%-5.2f : ', '...%-5.2f...'%(i))
print(r'%-f : ', '...%-f...'%(i))
print(r'%-.f : ', '...%-.f...'%(i))
print(r'%-0.0f : ', '...%-0.0f...'%(i))
width = 5
print(r'%-*.*f : ', '...%-*.*f...'%(width, width-2, i))
%-5.2f : ...4.53 ...
%-5.10f: ...4.5345900000...
%-f : ...4.534590...
%-.f : ...5...
%-0.0f : ...5...
%-*.*f : ...4.535...
用在基于字典的字符串格式化中,如:
>>>"%(name)s-%(id)d loves %(food)s"%{**dict(name = 'aloha', food = 'apple pie', id = 0)}
'aloha-0 loves apple pie'
常搭配vars()使用:
>>>name, food = "John", "apple pie"
>>>print("I'm %(name)s. My favorite food is %(food)s"%vars())
I'm John. My favorite food is apple pie
python template = "{0} {name}. {greeting}\n"; # {0}和{}等效 me = template.format("My name is", name = "aloha", greeting = "What‘s your name?") he = template.format("I‘m", name = "Stephen Prata", greeting ="How do you do?") print(me, he, sep = ‘‘)
My name is aloha. What‘s your name? I‘m Stephen. How do you do?
python template = "{} {name[0]}. {greeting}\n"; # {0}和{}等效 print(template.format("I‘m", name = "Stephen Prata".split(), greeting = "How do you do?"))
I‘m Stephen. How do you do?
{[fieldname][!conversionflag][:formatspec]}
.<mem_name>
或[<index>]
成分引用str(obj, /)
: 调用obj.__str__()
. 如果obj没有定义__str__()
函数, 则转为调用repr()
函数repr(obj, /)
: 尝试生成(yield)一个字符串, 使eval(repr(obj)) == obj
, 否则显示类似‘<function main at 0x000001B1CCD5FA68>‘
的obj的详细信息. 可以通过定义__repr__()
函数控制repr()
函数的输出ascii(obj, /)
: 类似repr()
, 但是会用转义格式表示非ASCII字符{[[fill]align][sign][#][0][minimumwidth][.precision][typecode]}
‘<‘
(默认)、‘^‘
、‘>‘
分别为左、中、右对齐, ‘=‘
(仅限于数字)强行用填充字符填在符号(如果有)和数字之间‘+‘
: 正负数都显示符号‘-‘
: 负数显示符号‘ ‘
: 正数前置空格, 负数显示符号‘#‘
: 给出‘#‘
表示会自动为输出的二进制(0b***
)、八进制(0o***
)、十六进制(0x***
)数字加上前缀‘0‘
: 等价于指定fill为‘0‘
、align为‘=‘
‘b‘
| Binary. Outputs the number in base 2. |‘c‘
| Character. Converts the integer to the corresponding Unicode character before printing. |‘d‘
| Decimal Integer. Outputs the number in base 10. |‘o‘
| Octal format. Outputs the number in base 8. |‘x‘
| Hex format. Outputs the number in base 16, using lower-case letters for the digits above 9. |‘X‘
| Hex format. Outputs the number in base 16, using upper-case letters for the digits above 9. |‘n‘
| Number. This is the same as ‘d‘, except that it uses the current locale setting to insert the appropriate number separator characters. |‘‘
(None) | the same as ‘d‘ |‘e‘
| Exponent notation. Prints the number in scientific notation using the letter ‘e‘ to indicate the exponent.|‘E‘
| Exponent notation. Same as ‘e‘ except it converts the number to uppercase.|‘f‘
| Fixed point. Displays the number as a fixed-point number.|‘F‘
| Fixed point. Same as ‘f‘ except it converts the number to uppercase.|‘g‘
| General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to ‘e‘ exponent notation.|‘G‘
| General format. Same as ‘g‘ except switches to ‘E‘ if the number gets to large.|‘n‘
| Number. This is the same as ‘g‘, except that it uses the current locale setting to insert the appropriate number separator characters.|‘%‘
| Percentage. Multiplies the number by 100 and displays in fixed (‘f‘) format, followed by a percent sign.|‘‘
(None) | similar to ‘g‘, except that it prints at least one digit after the decimal point.|python def func_name(para1, para2,...): code
python def func_name(para1, para2 = ‘‘,...): code
python def func_name(para1, para2,...): code return para1 + para2, para1 - para2
控制执行
```python
def main():
pass
示例代码:
```python
def main(food, name, *args, **kwargs):
print("%s love(s) %s." % (name, food))
print(args)
print(kwargs)
输出:
aloha love(s) apple pie. ("I‘m", ‘telling‘, ‘nothing.‘) {‘age‘: 100, ‘id‘: 0}
注意:在函数声明中,普通参数(regular parameter)必须在这两种参数前,*parameter必须在**parameter前。
示例代码:
```python
def getname(arr):
arr[0] = str(input("What‘s your name?\n"))
names = ["username", "aloha"]
getname(names)
print("Hello %s, I'm %s."%(names[0], names[1]))
```输出:
What‘s your name? Stephen Hello Stephen, I‘m aloha.
标签:res which mem 格式 指针 hex 对齐 read insert
原文地址:https://www.cnblogs.com/alohana/p/12238501.html