Skip to content

Commit

Permalink
学习过程中写的代码
Browse files Browse the repository at this point in the history
参考书目为本办法学python
  • Loading branch information
KuzeKuze authored Jun 2, 2019
1 parent 648873d commit 172d992
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
print("Hello world!")
print("Hello Again")
print("I like typing this.")
print("This is fun.")
print("Yay! Printing.")
print("I'd much rather you 'not'.")
print('I "said" do not touch this.')
7 changes: 7 additions & 0 deletions ex13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from sys import argv
script, first, second, third = argv

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)
21 changes: 21 additions & 0 deletions ex14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sys import argv

script, user_name = argv
prompt = "-->>"

print(f"Hi {user_name}, i'm the {script} script.")
print(f"I'd like to ask you a few questions.")
print(f"Do you like me {user_name}")
likes = input(prompt)

print(f"Where do you live {user_name}?")
lives = input(prompt)

print("What kind of computer do you have?")
computer = input(prompt)

print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice.
""")
13 changes: 13 additions & 0 deletions ex15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from sys import argv
script, filename = argv

txt = open( filename )
print(f"Here's your file {filename}:")
print(txt.read())

print("Type the filename again:")
file_again = input("> ")

txt_again = open(file_again)

print(txt_again.read())
3 changes: 3 additions & 0 deletions ex15_sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is stuff I typed into a file.
It is really cool stuff.
Lots and Lots of fun to have in here.
32 changes: 32 additions & 0 deletions ex16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from sys import argv

script, filename = argv

print(f"We're going to erase {filename}.")
print(f"If you don't want that, hit CTRL-C (^C).")
print("If you do want that,hit RETURN.")

input("?")

print("Opening the file...")
target = open(filename,"w")

print("Truncating the file. Goodbye!")
target.truncate()

print("Now I'm going to ask you for three lines.")

line1 = input("line1: ")
line2 = input("line2: ")
line3 = input("line3: ")

print("I am going to write these to the file.")
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print("And finally, we close it.")
target.close()
24 changes: 24 additions & 0 deletions ex17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from sys import argv
from os.path import exists
script, from_file, to_file = argv

print(f"Coping from {from_file} to {to_file}")

in_file = open( from_file )
indata = in_file.read()

print(f"The input file is {len(indata)} bytes long.")

print(f"Does the output file exist? {exists(to_file)}")

print("Ready, hit RETURN to continue,CTRL-C to abort.")

input()

out_file = open(to_file, 'w')
out_file.write(indata)

print("Alright, all done.")

out_file.close()
in_file.close()
18 changes: 18 additions & 0 deletions ex18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#这里定义几个函数和解包
def print_two( *args ):
arg1, arg2 = args #解包操作
print( f"arg1: {arg1}, arg2: {arg2}")

def print_two_again(arg1, arg2):
print(f"arg1: {arg1}, arg2: {arg2}")

def print_one(arg1):
print(f"arg1: {arg1}")

def print_none():
print("I got nothin'.")

print_two("Kuze","Du")
print_two_again("Kuze","Du")
print_one("First")
print_none()
5 changes: 5 additions & 0 deletions ex2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#这是一个注释
print("I could code like this.")#这也是一个注释
#我可以用注释来使得语句无效
#print("This won't run.")
print("This will run.")
33 changes: 33 additions & 0 deletions ex20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from sys import argv
script, input_file = argv

def print_all(f):
print( f.read())

def rewind(f):
f.seek(0)

def print_a_line(line_count, f):
print( line_count, f.readline())

current_file = open( input_file )

print("让我们先来全部打印一下。")

print_all(current_file)

print("像磁带那样回溯到一个位置。")

rewind(current_file)

print("打印3行")

current_line = 1
print_a_line(current_line,current_file)

current_line += 1
print_a_line(current_line,current_file)

current_line += 1
print_a_line(current_line,current_file)

20 changes: 20 additions & 0 deletions ex3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
print("I will now count my chickens:")

print("Hens",25+30/6)
print("Roosters",100 - 25 * 3 % 4)

print("Now I will count the aggs:")
print(3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6)

print("Is it true that 3 + 2 < 5 - 7?")

print( 3 + 2 < 5 - 7)

print("What is 3 + 2?",3 + 2)
print("What is 5 - 7?",5 - 7)

print("Oh, that's why it's False.")

print("Is is greater?",5 > -2)
print("Is is greater or equal?", 5 >= -2)
print("Is it less or equal?", 5 <= -2)
17 changes: 17 additions & 0 deletions ex3_study_drills_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
print("现在我要用浮点数来数鸡有多少只。")
print("母鸡有:",25 + 30 / 6 )
print("公鸡有:",100 - 25 * 3 % 4 + 0.0)

print("现在我要数数蛋有多少。")
print(3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6)

print("3 + 2 < 5 - 7 对吗?")
print(3 + 2 < 5 - 7)
print("3 + 2 等于多少?",3 + 2)
print("5 - 7 等于多少?",5 - 7)
print("怪不得结果是False。")

print("大于成立?",5 > -2)
print("大于等于成立吗?",5 >= -2)
print("小于等于成立吗?",5 <= -2)

15 changes: 15 additions & 0 deletions ex4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cars = 100#一共100辆车
space_in_a_car = 4#车上的空位
drivers = 30#司机的数量
passengers = 90#乘客的数量
cars_not_driven = cars - drivers #空车的数量
cars_driven = drivers#有司机的车的数量
carpool_capacity = cars_driven * space_in_a_car#车的总容量
average_passengers_per_car = passengers / cars_driven#平均每辆车的乘客数,似乎/这种除法的结果怎么都是浮点数

print("There are", cars, "cars available.")
print("There are only", drivers, "drivers available.")
print("There will be", cars_not_driven, "empty cars today.")
print("We can transport",carpool_capacity,"people today.")
print("We have",passengers,"to carpool today.")
print("We need to put about", average_passengers_per_car,"in each car.")
2 changes: 2 additions & 0 deletions ex4_study_drills.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print(round(1.7333))#没有第二个参数是返回整数
#若第二个ndigits参数是0是返回浮点数!!!!
17 changes: 17 additions & 0 deletions ex5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name = 'Kuze'
age = 20
height = 183*0.3937
weight = 61*2.2
eyes = 'Black'
teeth = 'White'
hair = 'Black'

print(f"Let's talk about {name}.")
print(f"He's {height} cm.")
print(f"He's {weight} pounds heavy.")
print("Actually that's not too heavy.")
print(f"He's got {eyes} eyes and {hair} hair")
print(f"His teeth are usually {teeth} depending on the")

total = age + height + weight
print(f"If I add {age}, {height}, and {weight} I get {total}")
21 changes: 21 additions & 0 deletions ex6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
types_of_people = 10
x = f"There are {types_of_people} types_of_people." #把types_of_people替换进字符串
binary = "binary"
do_not = "don't"
y = f"Those who know {binary} and those who {do_not}."#把binary和do_not变量替换进去

print(x)
print(y)

print(f"I said: {x}") #把x变量替换进字符串然后输出
print(f"I also said: '{y}'") #把y变量替换进字符串然后输出

hilarious = False
joke_evaluation = "Isn't that joke so funny?!{}"

print(joke_evaluation.format(hilarious)) #用format函数把hilarious替换到字符串的{}中

w = "This is the left side of..."
e = "a string with a right side."

print( w + e )
20 changes: 20 additions & 0 deletions ex7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
print("Mary had a little lamb.")
print("Its fleece was white as {}.".format('snow'))
print("And everywhere that Mary went.")
print("." * 10) # what'd that do?

end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"

print(end1 + end2 + end3 + end4 + end5 + end6, end=" ")
print(end7 + end8 + end9 + end10 + end11 + end12)
12 changes: 12 additions & 0 deletions ex8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
formatter = "{} {} {} {}"

print(formatter.format(1, 2, 3, 4))
print(formatter.format("one", "two", "three", "four"))
print(formatter.format(True, False, False, True))
print(formatter.format(formatter, formatter, formatter, formatter))
print(formatter.format(
"Try your",
"Own text here",
"Maybe a poem",
"Or a song about fear"
))
11 changes: 11 additions & 0 deletions ex9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"

print("Here are the days: ",days)
print("Here are the months: ",months)

print("""
There's somthing going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5,or 6.""")
3 changes: 3 additions & 0 deletions new_file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
laozi\nchihuoguo
ni\tok dj
devil may cry
3 changes: 3 additions & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
laozi\nchihuoguo
ni\tok dj
devil may cry

0 comments on commit 172d992

Please sign in to comment.