[python]learning python -5e

string-Formatting Expression Syntax

Table 7-4. String formatting type codes

Code Meaning
  • s String (or any object’s str(X) string)
  • r Same as s, but uses repr, not str
  • c Character (int or str)
  • d Decimal (base-10 integer)
  • i Integer
  • u Same as d (obsolete: no longer unsigned)
  • o Octal integer (base 8)
  • x Hex integer (base 16)
  • X Same as x, but with uppercase letters
  • e Floating point with exponent, lowercase
  • E Same as e, but uses uppercase letters
  • f Floating-point decimal
  • F Same as f, but uses uppercase letters
  • g Floating-point e or f
  • G Floating-point E or F
  • % Literal % (coded as %%)
%[(keyname)][flags][width][.precision]typecode

When sizes are not known until runtime, you can use a computed width and precision
by specifying them with a * in the format string to force their values to be taken from
the next item in the inputs to the right of the % operator—the 4 in the tuple here gives
precision:
 '%f, %.2f, %.*f' % (1/3.0, 1/3.0, 4, 1/3.0)
'0.333333, 0.33, 0.3333'

Dictionary-Based Formatting Expressions
'%(qty)d more %(food)s' % {'qty': 1, 'food': 'spam'}

'1 more spam'

string-Raw Strings Suppress Escapes

This is just the sort of thing that raw strings are useful for. Ifthe letter r (uppercase or
lowercase) appears just before the opening quote of a string, itturns off the escape
mechanism.
The result is that Python retains your backslashesliterally, exactly as you
type them. Therefore, to fix the filename problem, just remember toadd the letter r on
Windows:
myfile = open(r'C:\new\text.dat', 'w')

Extended Sequence Unpacking in Python 3.X

When a starred name is used, the number of items in the target on the left need not
match the length of the subject sequence. In fact, the starred name can appear anywhere
in the target. For instance, in the next interaction b matches the last item in the sequence,
and a matches everything before the last:

>>> a, *b, c = seq
>>> a
1
>>> b
[2, 3]
>>> c
4

Truth Values and Boolean Tests

• All objects have an inherent Boolean true or false value.
• Any nonzero number or nonempty object is true.
• Zero numbers, empty objects, and the special object None are considered false.
• Comparisons and equality tests are applied recursively to data structures.
• Comparisons and equality tests return True or False (custom versions of 1 and 0).
• Boolean and and or operators return a true or false operand object.
• Boolean operators stop evaluating (“short circuit”) as soon as a result is known.

if X:
A = Y
else:
A = Z

the same thing in one expression:

A = Y if X else Z

while and for Loops

the while statement, provides a way to code general loops. The
second, the for statement, is designed for stepping through the items in a sequence or
other iterable object and running a block of code for each.

break
Jumps out of the closest enclosing loop (past the entire loop statement)
continue
Jumps to the top of the closest enclosing loop (to the loop’s header line)
pass
Does nothing at all: it’s an empty statement placeholder
Loop else block
Runs if and only if the loop is exited normally (i.e., without hitting a break)

while x: # Exit when x empty
if match(x[0]):
print('Ni')
break # Exit, go around else
x = x[1:]
else:
print('Not found') # Only here if exhausted x


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值