1 '''
2 print("Hello World!")3 print("Hello Again")4
5 print("I like typing this.")6 print("This is fun.")7 print("Yay!Printing")8 print("I'd much rather you 'not'")9 print('I "said" do not touch this')10 '''
11 '''
12 print("I will now count my chickens:")13 print("Hens",25+30/6)14 print("Roosters",100.0-25*3%4)15
16 print("Now I will count the eggs:")17 print("3+2+1-5+4%2-1/4+6")18 print("Is it true that 3+2<5-7?")19
20 print(3+2<5-7)21
22 print("What is 3+2?",3+2)23 print("What is 5-7?",5-7)24
25 print("Oh,that's why it's False.")26 print("How about some more.")27
28 print("Is it greater?",5>-2)29 print("Is it greater or equal?",5>=-2)30 print("Is it less or equal?",5<=-2)31 '''
32 '''
33 测试34 #coding=utf-835 '''
36 '''
37 cars=10038 space_in_a_car=439 drivers=3040 passengers=9041 cars_not_driven=cars-drivers42 cars_driven=drivers43 carpool_capacity=cars_driven*space_in_a_car44 average_passengers_per_car=passengers/cars_driven45
46 print("There are",cars,"cars available.")47 print("There are only",drivers,"drivers are available.")48 print("There will be",cars_not_driven,"empty cars today.")49 print("We can transport",carpool_capacity,"people today.")50 print("We have",passengers,"to carpool today.")51 print("We need to put about",average_passengers_per_car,"in each car.")52 '''
53 '''
54 my_name = 'Zed A. Shaw'55 my_age = 35 #not a lie [my_name]56 my_height = 74 #inches57 my_weight = 180 #lbs58 my_eyes = 'Blue'59 my_teeth = 'White'60 my_hair = 'Brown'61
62 print(f"Let's talk about {my_name}.")63 print(f"He's {my_height} inches tall.")64 print(f"He's {my_weight} pounds heavy.")65 print("Actually that's not too heavy.")66 print(f"He's got {my_eyes} eyes and {my_hair} hair.")67 print(f"Gis teeth are usually {my_teeth} depending on the coffee.")68
69 #This line is tricky,try to get it exact70 total = my_age+my_height+my_weight71 print(f"If I add {my_age},{my_height} and {my_weight} I get {total} .")72 '''
73 #如何将浮点数四舍五入 round(number)
74 '''
75 a = 1.9976 print(f"1.99 is {a},四舍五入为{round(a)}")77 '''
78 '''
79 types_of_people = 1080 x= f"There are {types_of_people} types of people."81
82 binary = "binary"83 do_not = "don't"84 y = f"Those who know {binary} and those who {do_not} ."85
86 print(x)87 print(y)88 print(f"I said {x}")89 print(f"I also said {y}")90
91 hilarious = True92 joke_evaluation = "Isn't that joke so funny?! {}"93
94 print(joke_evaluation.format(hilarious))95
96 w = "This is the left side of ..."97 e = "a string with a right side."98
99 print(w+e)100 '''
101 '''
102 print("Mary had a little lamb ")103 print("Its fleece was white as {}.".format('snow'))104 print("And everywhere that Mary went.")105 print("." * 10) #What'd that do?106
107 end1 = "C"108 end2 = "h"109 end3 = "e"110 end4 = "e"111 end5 = "s"112 end6 = "e"113 end7 = "B"114 end8 = "u"115 end9 = "r"116 end10 = "g"117 end11 = "e"118 end12 = "r"119
120 #Watch that comma at the end .try removing it to see what happens121 print(end1 + end2 + end3 + end4 + end5 + end6,end = ' ')122 print(end7 + end8 + end9 + end10 + end11 + end12)123 '''
124 '''
125 formatter = "{} {} {} {}"126 print(formatter.format(1,2,3,4))127 print(formatter.format("one","two","three","four"))128 print(formatter.format(True,False,False,True))129 print(formatter.format(formatter,formatter,formatter,formatter))130 print(formatter.format(131 "Try your",132 "Own text here",133 "Maybe a poem",134 "Or a song about fear"135 ))136 '''
137 '''
138 #Here's some new strange stuff,remember type it exactly.139
140 days = "Mon Tue Wed Thu Fri Sat Sun"141 months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"142
143 print("Here are the days: ",days)144 print("Here are the months: ",months)145
146 print("""147 There's something going on here.148 with the three double-quotes.149 We'll be able to type as much as we like.150 Even 4 lines if we want, or 5, or 6.151 """)152 '''
153 '''
154 tabby_cat = "\tI'm tabbed in."155 persian_cat = "I'm split\non a line."156 backslash_cat = "I'm \\ a \\ cat."157
158 fat_cat = """159 I'll do a list:160 \t* Cat food161 \t* Fishies162 \t* Catnip\n\t* Grass163 """164
165 print(tabby_cat)166 print(persian_cat)167 print(backslash_cat)168 print(fat_cat)169 '''
170 '''
171 #print("How old are you?",end=' ')172 age = input("How old are you?")173 #print("How tall are you?",end=' ')174 height = input("How tall are you?")175 #print("How much do you weigh?",end=' ')176 weight = input("How much do you weigh?")177
178 #age = age*6179 print(f"So you're {age} old,{height} tall,{weight} heavy")180 '''
181 '''
182 from sys import argv183 script,first,second,third = argv184
185 print("The script is called:",script)186 print("The first variable is:",first)187 print("Your second variable is:",second)188 print("Your third variable is:",third)189 '''
190 '''
191 from sys import argv192
193 script,user_name = argv194 prompt = '>'195
196 print(f"Hi {user_name},I'm the {script} script.")197 print("I'd like to ask you a few questions.")198 print(f"Do you like me {user_name}?")199 likes = input(prompt)200
201 print(f"Where do you live {user_name}?")202 lives = input(prompt)203
204 print("What kind of computer do you have?")205 computer = input(prompt)206
207 print(f"""208 Alright,so you said {likes} about liking me.209 You live in {lives}.Not sure where that is.210 And you have a {computer} computer.Nice.211 """)212 '''
213 '''
214 from sys import argv215
216 script,filename = argv217
218 txt = open(filename)219
220 print(f"Here's your file {filename}:")221 print(txt.read())222
223 print("Type the filename again:")224 file_again = input("> ")225
226 txt_again = open(file_again)227
228 print(txt_again.read())229 txt.close()230 txt_again.close()231 '''
232 '''
233 from sys import argv234
235 script,filename = argv236
237 print(f"We're going to erase {filename}.")238 print("If you don't want that,hit CTRL-C (^C).")239 print("If you do want that,hit RETURN.")240
241 input("?")242
243 print("Opening the file...")244 target = open(filename,'w')245
246 print("Truncating the file.Goodbye!")247 target.truncate()248
249 print("Now I'm going to ask you for three details")250 line1 = input("line 1: ")251 line2 = input("line 2: ")252 line3 = input("line 3: ")253
254 print("I'm going to write these to the file.")255
256 target.write(line1)257 target.write("\n")258 target.write(line2)259 target.write("\n")260 target.write(line3)261 target.write("\n")262
263 print("And finally,we close it.")264 target.close()265
266 target = open(filename,'r')267 print(target.read())268 print("That's all.")269 '''
270 '''
271 from sys import argv272 from os.path import exists273
274 script,from_file,to_file = argv275
276 print(f"Copying from {from_file} to {to_file}")277 in_file = open(from_file)278 indata = in_file.read()279
280 print(f"The input file is {len(indata)} bytes long")281 print(f"Does the output file exist?{exists(to_file)}")282 print("Ready,hit RETURN to continue,CTRL-C to abort.")283 input()284
285 out_file =open(to_file,'w')286 out_file.write(indata)287
288 print("Alright,all done.")289
290 out_file.close()291 in_file.close()292 '''
293 '''
294 #18--------------------------------295 def print_two(*args):296 arg1,arg2 = args297 print(f"arg1:{arg1},arg2:{arg2}")298
299 def print_two_again(arg1,arg2):300 print(f"arg1:{arg1},arg2:{arg2}")301
302 def print_one(arg1):303 print(f"arg1:{arg1}")304
305 def print_none():306 print("I got nothin'.")307
308 print_two("Zed","Shaw")309 print_two_again("Zed","Shaw")310 print_one("First!")311 print_none()312 #19--------------------------------313 '''
314 '''
315 def cheese_and_crackers(cheese_count,boxes_of_crackers):316 print(f"You have {cheese_count} cheese.")317 print(f"You have {boxes_of_crackers} boxes of crackers.")318 print("Man that's enough for a party.")319 print("Get a blanket.\n")320
321 print("We can just give the function numbers directly:")322 cheese_and_crackers(20,30)323
324 print("OR,we can use variables from our script:")325 amount_of_cheese = 10326 amount_of_crackers = 50327
328 cheese_and_crackers(amount_of_cheese,amount_of_crackers)329
330 print("We can even do math inside too:")331 cheese_and_crackers(10+20,5+6)332
333 print("And we can combine the two,variables and math:")334 cheese_and_crackers(amount_of_cheese+100,amount_of_crackers+1000)335 '''
336 '''
337 #20--------------------------------338 from sys import argv339
340 script,input_file = argv341
342 def print_all(f):343 print(f.read())344 def rewind(f):345 f.seek(0)346 def print_a_line(line_count,f):347 print(line_count,f.readline())348
349 current_file = open(input_file)350 print("First let's print the whole file :\n")351 print_all(current_file)352 print("Now let's rewind,kind of like a tape.")353 rewind(current_file)354
355 print("Let's print three lines:")356
357 current_line = 1358 print_a_line(current_line,current_file)359
360 current_line = current_line + 1361 print_a_line(current_line,current_file)362
363 current_line = current_line + 1364 print_a_line(current_line,current_file)365 '''
366 '''
367 #21--------------------------------368 def add(a,b):369 print(f"ADDING {a} + {b}")370 return a+b371
372 def subtract(a,b):373 print(f"SUBTRACTING {a} - {b}")374 return a-b375
376 def multiply(a,b):377 print(f"MULTIPLYING {a} * {b}")378 return a*b379
380 def divide(a,b):381 print(f"DIVIDING {a} / {b}")382 return a/b383
384 print("Let's do some math with just functions!")385
386 age = add(30,5)387 height = subtract(78,4)388 weight = multiply(90,2)389 iq = divide(100,2)390
391 print(f"Age:{age},Height:{height},Weight:{weight},IQ:{iq}")392
393 #A puzzle for the extra credit,type it in anyway394 print("Here is a puzzle.")395 what = add(age,subtract(height,multiply(weight,divide(iq,2))))396 print("That becomes:",what,"Can you do it by hand?")k397 '''
398 #23--------------------------------
399 '''
400 import sys401 script,encoding,error = sys.argv402
403 def main(language_file,encoding,errors):404 line = language_file.readline()405
406 if line:407 print_line(line,encoding,errors)408 return main(language_file,encoding,errors)409
410 def print_line(line,encoding,errors):411 next_lang = line.strip()412 raw_bytes = next_lang.encode(encoding,errors = errors)413 cooked_string = raw_bytes.decode(encoding,errors =errors)414
415 print(raw_bytes,"<===>",cooked_string)416
417 languages = open("languages.txt",encoding = "utf-8")418
419 main(languages,encoding,error)420 '''
421 '''
422 print("Let's practice everything.")423 print('You\'d need to know \'bout escapes with \\ that do:')424 print('\n new lines and \t tabs.')425
426 poem = """427 \t The lovely World428 with logic so firmly planted429 cannot discern \n the needs of lovely430 nor comprehend passion from intuition431 and requires an explanation432 \n\twhere there is none.433 """434 """435 print("---------------")436 print(poem)437 print("---------------")438
439 five = 10 - 2 + 3 - 6440 print(f"This should be five:{five}")441
442 def secret_formula(started):443 jelly_beans = started * 500444 jars = jelly_beans / 1000445 crates = jars /100446 return jelly_beans,jars,crates447
448 start_point = 10000449 beans,jars,crates = secret_formula(start_point)450
451 # remember that this is another way to format a cooked_string452 print("With starting point of :{}".format(start_point))453 #it's just like with an f""string454 print(f"We'd have {beans} beans,{jars} jars,and {crates} crates.")455
456 start_point = start_point /10457
458 print("We can also do that this way:")459 formula = secret_formula(start_point)460 #This is an easy way to apply a list to a format string461 print("We'd have {} beans,{} jars,and {} crates.".format(*formula))462 '''
463 '''
464 def break_words(stuff):465 """This function will break up words for us."""466 words = stuff.split(' ')467 return words468
469 def sort_words(words):470 """Sorts the words."""471 return sorted(words)472
473 def print_first_word(words):474 """Prints the first word after popping it off"""475 word = words.pop(0)476 print(word)477
478 def print_last_word(words):479 """Prints the last word after popping it off."""480 word = words.pop(-1)481 print(word)482
483 def sort_sentence(sentence):484 """Takes in a full sentence and returns the sorted words"""485 words = break_words(sentence)486 return sort_words(words)487
488 def print_first_and_last(sentence):489 """Prints the first and last words of the sentence"""490 words = break_words(sentence)491 print_first_word(words)492 print_last_word(words)493
494 def print_first_and_last_sorted(sentence):495 """Sorts the words then prints the first and last one."""496 words = sort_sentence(sentence)497 print_first_word(words)498 print_last_word(words)499 '''
500 '''
501 print("How old are you?", end=' ')502 age = input()503 print("How tall are you?", end=' ')504 height = input()505 print("How much do you weigh?", end=' ')506 weight = input()507
508 print(f"So, you're {age} old, {height} tall and {weight} heavy.")509 '''
510 '''
511 from sys import argv512 script, filename = argv513
514 txt = open(filename)515
516 print("Here's your file {filename}:")517 print(txt.read())518
519 print("Type the filename again:")520 file_again = input("> ")521 txt_again = open(file_again)522 print(txt_again.read())523 '''
524 '''
525 print("Let\'s practice everything.")526 print('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')527
528 poem = """529 \tThe lovely world530 with logic so firmly planted531 cannot discern \n the needs of love532 nor comprehend passion from intuition533 and requires an explanation534 \n\t\twhere there is none.535 """536
537 print("--------------")538 print(poem)539 print("--------------")540
541
542 five = 10 - 2 + 3 - 6543 print(f"This should be five: {five}")544
545 def secret_formula(started):546 jelly_beans = started * 500547 jars = jelly_beans / 1000548 crates = jars / 100549 return jelly_beans, jars, crates550
551
552 start_point = 10000553 beans, jars,crates = secret_formula(start_point)554
555 # remember that this is another way to format a string556 print("With a starting point of: {}".format(start_point))557 # it's just like with an f"" string558 print(f"We'd have {beans} beans, {jars} jars, and {crates} crates.")559
560 start_point = start_point / 10561
562 print("We can also do that this way:")563 formula = secret_formula(start_point)564 # this is an easy way to apply a list to a format string565 print("We'd have {} beans, {} jars, and {} crates.".format(*formula))566
567 people = 20568 cats = 30569 dogs = 15570
571 if people < cats:572 print ("Too many cats! The world is doomed!")573
574 if people < cats:575 print("Not many cats! The world is saved!")576
577 if people < dogs:578 print("The world is drooled on!")579
580 if people > dogs:581 print("The world is dry!")582
583
584 dogs += 5585
586 if people >= dogs:587 print("People are greater than or equal to dogs.")588
589 if people <= dogs:590 print("People are less than or equal to dogs.")591
592
593 if people == dogs:594 print("People are dogs.")595 '''
596 '''
597 people = 20598 cats = 30599 dogs = 15600
601 if people < cats:602 print("Too many cats!The world is doomed!")603 if people > cats:604 print("Not many cats!The world is saved!")605 if people < dogs:606 print("The world is drooled on!")607 if people > dogs:608 print("The world is dry!")609
610 dogs += 5611 if people >= dogs:612 print("People are greater than or equal to dogs.")613 if people <= dogs:614 print("People are less than or equal to dogs.")615
616 if people == dogs:617 print("People are dogs.")618 '''
619 '''
620 people = 15621 cars = 40622 trucks = 15623
624 if cars> people:625 print("We should take the cars.")626 elif cars
<
<