笨办法学习Python3练习代码1-10

ex1.py

1 print("hello world!",end = " ")#不换行
2 print("hello again")
3 print("I like typing this.")
4 print("This is fun.")
5 print("Yay!Printing.")
6 print("I'd much rather you 'not'.")
7 print('I "said" do not touch this.')
8 print("")

ex2.py

 1 # A comment, this is so you can read your program later.
 2 # Anything after the # is ignored by python.
 3 print("I could have code like this.")# and the comment after is ignored .
 4 
 5 # you can also use a comment to "disable" or comment out code:
 6 #print("This won't run.")
 7 print("This will run.")
 8 print("hi # there.")
 9 
10 #  (#)的英文是octothorpe或者 pound character

ex3.py

 1 #数字和数学运算
 2 print("I will now count my chickens:")
 3 print("hens", 25 + 30 / 6)
 4 print("roosters", 100 - 25 * 3 % 4)
 5 print("Now I will count the eggs:")
 6 
 7 print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
 8 
 9 print("Is it true that 3 + 2 < 5 - 7?")
10 print(3 + 2 < 5 - 7)
11 print("what is 3 + 2 ?", 3 + 2)
12 print("what is 5 - 7?", 5 -  7)
13 print("Oh,that's why it's false.")
14 print("How about some more.")
15 print("Is it greater ?", 5 > -2)
16 print("Is it greater or eequal ?", 5 >= -2)
17 print("Is it less or equal ?", 5 <= -2)

ex4.py

 1 #变量和命名
 2 
 3 #汽车数量
 4 cars = 100
 5 
 6 #每辆汽车的运载人数
 7 space_in_a_car = 4
 8 
 9 #司机人数
10 drivers = 30
11 
12 #乘客人数
13 passengers = 90
14 
15 #空车数量
16 cars_not_driven = cars - drivers
17 
18 #占用的汽车数量
19 cars_driven = drivers
20 
21 #最大可运输的人数
22 carpool_capacity = cars_driven * space_in_a_car  #capacity=容积,carpool=拼车
23 
24 #平均每台汽车上的人数
25 average_passengers_per_car = passengers / cars_driven
26 
27 print("there are ",cars,"cars available.")
28 print("There are only",drivers,"drivers availlable.")
29 print("There will be ",cars_not_driven,"empty cars today.")
30 print("We can transport ",carpool_capacity,"people today.")
31 print("We have ",passengers,"to carpool today.")
32 print("We need to put about",average_passengers_per_car,"in each car.")

ex5.py

 1 name = 'Zed A.Shaw'
 2 age = 35     #not a lie
 3 height = 74  #inches
 4 weight = 180 #lbs
 5 eyes = 'Blue'
 6 teeth = "White"
 7 hair = "Brown"
 8 
 9 print(f"Let's talk about {name}.")    #f代表格式化的意思format
10 print(f"He's {height} inches tall.")
11 print(f"He's {weight} pounds heavy.")
12 print("Actually that's not too heavy.")
13 print(f"He's got {eyes} eyes and {hair} hair.")
14 print(f"His teeth are usually {teeth} depending on the coffee.")
15 
16 #this line is tricky, try to get it exactly right
17 total = age + height + weight
18 print(f"If I add {age}, {height}, and {weight}. I get {total}.") 

ex6.py

 1 tpyes_of_people = 10
 2 
 3 x = f"There are {tpyes_of_people} types of people."
 4 binary = "binary"
 5 do_not = "don't"
 6 y = f"Those who know {binary} and those who {do_not}."
 7 
 8 print(x)
 9 print(y)
10 print(f"I said: {x} .")
11 #还是格式化输出字符串,在输出的字符串外面带上''
12 print(f"I also said: '{y}'.")
13 
14 hilarious = "False"
15 joke_evaluation = "Isn't that joke so funny ?!{}"
16 
17 #输出Isn't that joke so funny ?!False
18 #.format()括号里面的内容都是格式化输出
19 print(joke_evaluation.format(hilarious))
20 
21 w = "This is the left side of ..."
22 e = "a string with a right side."
23 print(w + e)

ex7.py

 1 print("Mary had a little lamb.")
 2 
 3 print("Its fleece was white as {}.".format('snow'))
 4 print("and everywhere that Mary went.")
 5 print("." * 10)
 6 
 7 end1 = "c"
 8 end2 = 'h'
 9 end3 = 'e'
10 end4 = 'e'
11 end5 = 's'
12 end6 = 'e'
13 end7 = 'B'
14 end8 = 'u'
15 end9 = 'r'
16 end10 = 'g'
17 end11 = 'e'
18 end12 = 'r'
19 
20 print(end1 + end2 + end3 + end4 + end5 + end6, end = " ")
21 print(end7 + end8 + end9 +end10 + end11 + end12)

ex8.py

 1 formatter = "{} {} {} {}"
 2 
 3 print(formatter.format(1,2,3,4))
 4 print(formatter.format('one','two','three','four'))
 5 print(formatter.format(True,False,False,True))
 6 print(formatter.format(formatter,formatter,formatter,formatter))
 7 
 8 print(formatter.format(
 9       "Try your",
10       "Own text here",
11       "Maybe a poem",
12       "Or a song about fear"))
13       
14 #这个习题使用了format函数,让它返回formatter变量到其他字符串中
15 #当看到formatter.format(....),相当于
16 #1.取第一行定义的formatter字符串
17 #2.调用它的format函数
18 #3.给format传递四个参数,这些参数和formatter中的{}匹配
19 #4.在formatter上调用format的结果是一个新字符串,其中的{}被四个变量所替换

ex9.py

 1 # Here's some new strange stuf(材料,原料),remember type it exactly.
 2 days = "mon tue wed thu fri sat sun"
 3 months = " jan\nfeb\nmar\napr\nmay\njun\njul\naug"
 4 print("here are the days:",days)
 5 print("Here are the months: ",months)
 6 
 7 print('''
 8 There's something going on here.
 9 with the three double-quotes.
10 we'll be able to tpye as much as we like.
11 even 4 lines if we want,or 5, or 6.''')

 

ex10.py

 1 #\\转义字符\
 2 #\t转义字符tab键缩进,水平制表符,一个制表符8个位置
 3 #\'输出'
 4 #\"输出“
 5 
 6 
 7 
 8 tabby_cat = "\tI'm tabbed in."
 9 persian_cat = "I'm split\non a line."
10 backslash_cat = "I'm \\ a \\cat."
11 
12 fat_cat = """
13 I'll do a list:
14 \t* Cat food
15 \t*Fishies
16 \t*Catnip\n\t*Grass
17 """
18 
19 print(tabby_cat)
20 print(persian_cat)
21 print(backslash_cat)
22 print(fat_cat)

 

 

  

 

posted @ 2020-07-14 17:49  lscv26  阅读(353)  评论(0)    收藏  举报