变量命名赋值规则
1 # variables - placeholders for important values 2 # used to avoid recomputing values and to 3 # give values names that help reader understand code 4 5 6 # valid variable names - consists of letters, numbers, underscore (_) 7 # starts with letter or underscore 8 # case sensitive (capitalization matters) 9 10 # legal names - ninja, Ninja, n_i_n_j_a 11 # illegal names - 1337, 1337ninja 12 13 # Python convention - multiple words joined by _ 14 # legal names - elite_ninja, leet_ninja, ninja_1337 15 # illegal name 1337_ninja 16 17 18 19 # assign to variable name using single equal sign = 20 # (remember that double equals == is used to test equality) 21 22 # examples 23 24 my_name = "Joe Warren" 25 print my_name 26 27 my_age = 51 28 print my_age 29 30 # birthday - add one 31 32 #my_age += 1 33 print my_age 34 35 36 # the story of the magic pill 37 38 magic_pill = 30 39 print my_age - magic_pill 40 41 my_grand_dad = 74 42 43 print my_grand_dad - 2 * magic_pill 44 45 46 47 48 49 50 51 52 53 # Temperature examples 54 55 # convert from Fahrenheit to Celsuis 56 # c = 5 / 9 * (f - 32) 57 # use explanatory names 58 59 temp_Fahrenheit = 212 60 61 temp_Celsius = 5.0 / 9.0 * (temp_Fahrenheit - 32) 62 63 print temp_Celsius 64 65 # test it! 32 Fahrenheit is 0 Celsius, 212 Fahrenheit is 100 Celsius 66 67 68 # convert from Celsius to Fahrenheit 69 # f = 9 / 5 * c + 32 70 71 temp_Celsius = 100 72 73 temp_Fahrenheit = 9.0 / 5.0 * temp_Celsius + 32 74 75 print temp_Fahrenheit 76 77 78 # test it!