Skip to content

Commit 172d992

Browse files
authored
学习过程中写的代码
参考书目为本办法学python
1 parent 648873d commit 172d992

21 files changed

+304
-0
lines changed

ex1.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
print("Hello world!")
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.')

ex13.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from sys import argv
2+
script, first, second, third = argv
3+
4+
print("The script is called:", script)
5+
print("Your first variable is:", first)
6+
print("Your second variable is:", second)
7+
print("Your third variable is:", third)

ex14.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from sys import argv
2+
3+
script, user_name = argv
4+
prompt = "-->>"
5+
6+
print(f"Hi {user_name}, i'm the {script} script.")
7+
print(f"I'd like to ask you a few questions.")
8+
print(f"Do you like me {user_name}")
9+
likes = input(prompt)
10+
11+
print(f"Where do you live {user_name}?")
12+
lives = input(prompt)
13+
14+
print("What kind of computer do you have?")
15+
computer = input(prompt)
16+
17+
print(f"""
18+
Alright, so you said {likes} about liking me.
19+
You live in {lives}. Not sure where that is.
20+
And you have a {computer} computer. Nice.
21+
""")

ex15.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from sys import argv
2+
script, filename = argv
3+
4+
txt = open( filename )
5+
print(f"Here's your file {filename}:")
6+
print(txt.read())
7+
8+
print("Type the filename again:")
9+
file_again = input("> ")
10+
11+
txt_again = open(file_again)
12+
13+
print(txt_again.read())

ex15_sample.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is stuff I typed into a file.
2+
It is really cool stuff.
3+
Lots and Lots of fun to have in here.

ex16.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from sys import argv
2+
3+
script, filename = argv
4+
5+
print(f"We're going to erase {filename}.")
6+
print(f"If you don't want that, hit CTRL-C (^C).")
7+
print("If you do want that,hit RETURN.")
8+
9+
input("?")
10+
11+
print("Opening the file...")
12+
target = open(filename,"w")
13+
14+
print("Truncating the file. Goodbye!")
15+
target.truncate()
16+
17+
print("Now I'm going to ask you for three lines.")
18+
19+
line1 = input("line1: ")
20+
line2 = input("line2: ")
21+
line3 = input("line3: ")
22+
23+
print("I am going to write these to the file.")
24+
target.write(line1)
25+
target.write("\n")
26+
target.write(line2)
27+
target.write("\n")
28+
target.write(line3)
29+
target.write("\n")
30+
31+
print("And finally, we close it.")
32+
target.close()

ex17.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from sys import argv
2+
from os.path import exists
3+
script, from_file, to_file = argv
4+
5+
print(f"Coping from {from_file} to {to_file}")
6+
7+
in_file = open( from_file )
8+
indata = in_file.read()
9+
10+
print(f"The input file is {len(indata)} bytes long.")
11+
12+
print(f"Does the output file exist? {exists(to_file)}")
13+
14+
print("Ready, hit RETURN to continue,CTRL-C to abort.")
15+
16+
input()
17+
18+
out_file = open(to_file, 'w')
19+
out_file.write(indata)
20+
21+
print("Alright, all done.")
22+
23+
out_file.close()
24+
in_file.close()

ex18.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#这里定义几个函数和解包
2+
def print_two( *args ):
3+
arg1, arg2 = args #解包操作
4+
print( f"arg1: {arg1}, arg2: {arg2}")
5+
6+
def print_two_again(arg1, arg2):
7+
print(f"arg1: {arg1}, arg2: {arg2}")
8+
9+
def print_one(arg1):
10+
print(f"arg1: {arg1}")
11+
12+
def print_none():
13+
print("I got nothin'.")
14+
15+
print_two("Kuze","Du")
16+
print_two_again("Kuze","Du")
17+
print_one("First")
18+
print_none()

ex2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#这是一个注释
2+
print("I could code like this.")#这也是一个注释
3+
#我可以用注释来使得语句无效
4+
#print("This won't run.")
5+
print("This will run.")

ex20.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from sys import argv
2+
script, input_file = argv
3+
4+
def print_all(f):
5+
print( f.read())
6+
7+
def rewind(f):
8+
f.seek(0)
9+
10+
def print_a_line(line_count, f):
11+
print( line_count, f.readline())
12+
13+
current_file = open( input_file )
14+
15+
print("让我们先来全部打印一下。")
16+
17+
print_all(current_file)
18+
19+
print("像磁带那样回溯到一个位置。")
20+
21+
rewind(current_file)
22+
23+
print("打印3行")
24+
25+
current_line = 1
26+
print_a_line(current_line,current_file)
27+
28+
current_line += 1
29+
print_a_line(current_line,current_file)
30+
31+
current_line += 1
32+
print_a_line(current_line,current_file)
33+

ex3.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
print("I will now count my chickens:")
2+
3+
print("Hens",25+30/6)
4+
print("Roosters",100 - 25 * 3 % 4)
5+
6+
print("Now I will count the aggs:")
7+
print(3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6)
8+
9+
print("Is it true that 3 + 2 < 5 - 7?")
10+
11+
print( 3 + 2 < 5 - 7)
12+
13+
print("What is 3 + 2?",3 + 2)
14+
print("What is 5 - 7?",5 - 7)
15+
16+
print("Oh, that's why it's False.")
17+
18+
print("Is is greater?",5 > -2)
19+
print("Is is greater or equal?", 5 >= -2)
20+
print("Is it less or equal?", 5 <= -2)

ex3_study_drills_4.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
print("现在我要用浮点数来数鸡有多少只。")
2+
print("母鸡有:",25 + 30 / 6 )
3+
print("公鸡有:",100 - 25 * 3 % 4 + 0.0)
4+
5+
print("现在我要数数蛋有多少。")
6+
print(3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6)
7+
8+
print("3 + 2 < 5 - 7 对吗?")
9+
print(3 + 2 < 5 - 7)
10+
print("3 + 2 等于多少?",3 + 2)
11+
print("5 - 7 等于多少?",5 - 7)
12+
print("怪不得结果是False。")
13+
14+
print("大于成立?",5 > -2)
15+
print("大于等于成立吗?",5 >= -2)
16+
print("小于等于成立吗?",5 <= -2)
17+

ex4.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cars = 100#一共100辆车
2+
space_in_a_car = 4#车上的空位
3+
drivers = 30#司机的数量
4+
passengers = 90#乘客的数量
5+
cars_not_driven = cars - drivers #空车的数量
6+
cars_driven = drivers#有司机的车的数量
7+
carpool_capacity = cars_driven * space_in_a_car#车的总容量
8+
average_passengers_per_car = passengers / cars_driven#平均每辆车的乘客数,似乎/这种除法的结果怎么都是浮点数
9+
10+
print("There are", cars, "cars available.")
11+
print("There are only", drivers, "drivers available.")
12+
print("There will be", cars_not_driven, "empty cars today.")
13+
print("We can transport",carpool_capacity,"people today.")
14+
print("We have",passengers,"to carpool today.")
15+
print("We need to put about", average_passengers_per_car,"in each car.")

ex4_study_drills.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print(round(1.7333))#没有第二个参数是返回整数
2+
#若第二个ndigits参数是0是返回浮点数!!!!

ex5.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name = 'Kuze'
2+
age = 20
3+
height = 183*0.3937
4+
weight = 61*2.2
5+
eyes = 'Black'
6+
teeth = 'White'
7+
hair = 'Black'
8+
9+
print(f"Let's talk about {name}.")
10+
print(f"He's {height} cm.")
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")
15+
16+
total = age + height + weight
17+
print(f"If I add {age}, {height}, and {weight} I get {total}")

ex6.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
types_of_people = 10
2+
x = f"There are {types_of_people} types_of_people." #把types_of_people替换进字符串
3+
binary = "binary"
4+
do_not = "don't"
5+
y = f"Those who know {binary} and those who {do_not}."#把binary和do_not变量替换进去
6+
7+
print(x)
8+
print(y)
9+
10+
print(f"I said: {x}") #把x变量替换进字符串然后输出
11+
print(f"I also said: '{y}'") #把y变量替换进字符串然后输出
12+
13+
hilarious = False
14+
joke_evaluation = "Isn't that joke so funny?!{}"
15+
16+
print(joke_evaluation.format(hilarious)) #用format函数把hilarious替换到字符串的{}中
17+
18+
w = "This is the left side of..."
19+
e = "a string with a right side."
20+
21+
print( w + e )

ex7.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
print("Mary had a little lamb.")
2+
print("Its fleece was white as {}.".format('snow'))
3+
print("And everywhere that Mary went.")
4+
print("." * 10) # what'd that do?
5+
6+
end1 = "C"
7+
end2 = "h"
8+
end3 = "e"
9+
end4 = "e"
10+
end5 = "s"
11+
end6 = "e"
12+
end7 = "B"
13+
end8 = "u"
14+
end9 = "r"
15+
end10 = "g"
16+
end11 = "e"
17+
end12 = "r"
18+
19+
print(end1 + end2 + end3 + end4 + end5 + end6, end=" ")
20+
print(end7 + end8 + end9 + end10 + end11 + end12)

ex8.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
print(formatter.format(
8+
"Try your",
9+
"Own text here",
10+
"Maybe a poem",
11+
"Or a song about fear"
12+
))

ex9.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
days = "Mon Tue Wed Thu Fri Sat Sun"
2+
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
3+
4+
print("Here are the days: ",days)
5+
print("Here are the months: ",months)
6+
7+
print("""
8+
There's somthing going on here.
9+
With the three double-quotes.
10+
We'll be able to type as much as we like.
11+
Even 4 lines if we want, or 5,or 6.""")

new_file.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
laozi\nchihuoguo
2+
ni\tok dj
3+
devil may cry

test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
laozi\nchihuoguo
2+
ni\tok dj
3+
devil may cry

0 commit comments

Comments
 (0)