Difficulties focus python basis ---

Ternary operator

  Variables and results Conditional Expression = 1 or 2 Results

  If the condition is a Boolean expression, then the result of the establishment and implementation of the latter; does not hold back or if execution results

1  # keyboard to type a character, if the lowercase letters, obtaining the corresponding capital letters; if uppercase deposited directly into 
2  
. 3  # method. ord: turn decimal; chr: forwarding character 
. 4 CH = INPUT ( " Please enter a character: " )
 . 5 NUM = the ord (CH)
 . 6 RES = (97 <= NUM <= 122 )
 . 7 value = RES and CHR (NUM - 32) or CH
 . 8  Print (value)
 . 9  
10  # method II 
. 11 CH = iNPUT ( " Please enter a character: " )
 12 is NUM = the ord (CH)
 13 is RES = ( ' a '<= CH <= ' Z ' )
 14 value = RES and CHR (NUM-32) or CH
 15  Print (value)
 16  
. 17  # Method III. Shorthand 
18 is CH = INPUT ( " Please enter a character: " )
 . 19 NUM = the ord (CH)
 20 is value = ( ' A ' <= CH <= ' Z ' ) and CHR (NUM-32) or CH
 21 is  Print (value )

 Exercise function

1  # determines whether a number is a leap year 
2  DEF is_year (year):
 . 3      IF (year == 0. 4% and ! = 100% year 0) or (400% year == 0):
 . 4          return True
 . 5      the else :
 . 6          return false
 . 7 RES = is_year (1996 )
 . 8  Print (RES)

 

Guess you like

Origin www.cnblogs.com/TMMM/p/11444563.html