标签:lte product margin open 不用 err 相同 基础 下划线
a,b = b,a |
product = max(100,200)\ *30 |
help() |
if 1 < 2:print(‘smaller‘)
|
if __name__ == "__main__": main() |
"greater"[2:5] #Retruns "eat" |
<formtt string> % <datum> |
print( "%6s" % "four" ) #右对齐 print( "%-6s" % "four" ) #左对齐 |
for exponent in range(7,11): print( "%-3d%12d" % ( exponent, 10 ** exponent ) ) |
%<field width>.<precision>f |
salary = 100 print( "Your salary is $%0.2f." % salary ) |
dir(str) |
rgTuple = ( ( <r>, <g>, <b> ), <string> ) ( ( r, g, b ), hexString ) = rgTuple |
def ourSum( lower, upper, margin = 0 ): """Returns the sum of the numbers from lower to upper, and outputs a trace of the arguements and return values on each call."""
blanks = " " * margin print( blanks,str(lower)+ str(upper) ) if lower > upper: print( blanks, 0 ) return 0 else: result = lower + ourSum( lower + 1, upper, margin + 4 ) print( blanks,result ) return result
if __name__ == "__main__": ourSum(1,4) |
14 24 34 44 54 0 4 7 9 10 |
def factorial(n): """ Returns the factorial of n. """
def recurse(n, product): if n == 1: return product else: return recurse(n-1, n * product)
return recurse(n, 1) |
oldList = [1,2,3,4] newList = list( map( str, oldList ) ) #Returns [‘1‘, ‘2‘, ‘3‘, ‘4‘] |
def isPositive( number ): """判断是否为正数""" if number > 0: return True else: return False
oldList = [-1,2,3,5,-100,1000,0] newList = list( filter( isPositive, oldList ) ) #[2,3,5,1000] |
lambda <argument list> : <expression> |
newList = list( filter( lambda number : number > 0, oldList ) ) |
import functools product = functools.reduce( lambda x,y : x * y, range(1,6) ) #120 |
def safeIntegerInput( prompt ): """Pormpts the user for an integer nad returns the integer if it is well-formed. Otherwise, prints an error message and repeats this process."""
inputString = input( prompt ) try: number = int( inputString ) return number except ValueError: print( "Please Enter in the correct number format:", inputString ) return safeIntegerInput( prompt ) |
import pickle
#写入文件 lyst = [60, "A string object", [1444, 1644, 1911] ] fileObj = open( "items.dat", "wb" ) for item in lyst: pickle.dump( item, fileObj ) fileObj.close()
#读取文件 lyst = list() fileObj = open( "items.dat", "rb" ) while True: try: item = pickle.load( fileObj ) lyst.append( item ) except EOFError: fileObj.close() break
print( lyst ) |
class Counter(object): """Models a counter"""
# Class variable instances = 0
# Constructor def __init__(self): """Set up the counter.""" Counter.instances += 1 self.reset()
#Mutator methods def reset(self): """Sets the counter to 0. """ self._value = 0
def increment(self, amount = 1): """Adds amount to the counter.""" self._value += amount
def decrement(self, amount = 1): """Subtracts amount from the counter""" self._value -= amount
#Acessor methods def getValue(self): """Return the counter‘s value.""" return str( self._value )
def __str__(self): """Returns the string representation of the counter.""" return str(self._value)
def __eq__(self, other): """Returns True if self equals other or False otherwise.""" if self is other: return True if type(self) != type(other): return False return self._value == other._value |
标签:lte product margin open 不用 err 相同 基础 下划线
原文地址:https://www.cnblogs.com/lijunjie9502/p/9892570.html