Skip to content

Commit aad2443

Browse files
committed
Initial commit
0 parents  commit aad2443

File tree

225 files changed

+104247
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+104247
-0
lines changed

.idea/Python_Crash_Course_all.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

part_10/alice.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: cp936 -*-
2+
3+
# 统计一个文档有多少个单词
4+
5+
filename = 'alice.txt'
6+
7+
try:
8+
with open(filename) as f_obj:
9+
contents = f_obj.read()
10+
except FileNotFoundError:
11+
msg = "Sorry, the file " + filename + " does not exist."
12+
print(msg)
13+
else:
14+
words = contents.split()
15+
num_words = len(words)
16+
print("The file " + filename + " has about " + str(num_words) + " words.")

part_10/alice.txt

+3,735
Large diffs are not rendered by default.

part_10/division.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
print("Give me teo numbers, and I will divide them.")
2+
print("Enter 'q' to quit.")
3+
4+
while True:
5+
first_number = input("\nThe first number: ")
6+
if first_number == 'q':
7+
break
8+
second_number = input("The second number: ")
9+
if second_number == 'q':
10+
break
11+
try:
12+
answer = int(first_number) / int(second_number)
13+
except ZeroDivisionError:
14+
print("You can't divide by 0")
15+
else:
16+
print(answer)

part_10/file_reader.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: cp936 -*-
2+
3+
# 打开同一个文件夹下的文档
4+
with open('pi_digits.txt') as file_object:
5+
contents = file_object.read()
6+
print(contents)
7+
8+
# 采用绝对路径打开文档,正常是斜杠,如果打开错误换成反斜杠
9+
file_path = 'C:/Users/HP/Desktop/python_work/part_10/pi_digits.txt'
10+
with open(file_path) as file_object:
11+
contents = file_object.read()
12+
print(contents)
13+
14+
# 打开文件,以每次一行的方式检查文件
15+
file_name = 'pi_digits.txt'
16+
with open(file_name) as file_object:
17+
for line in file_object:
18+
print(line)
19+
20+
# 打开文件,以每次一行的方式检查文件,print语句后面也有一个换行符,默认文件末尾有一个换行符
21+
# 使用rstrip()函数消除换行符
22+
file_name = 'pi_digits.txt'
23+
with open(file_name) as file_object:
24+
for line in file_object:
25+
print(line.rstrip())
26+
27+
# 打开文件,逐字检查
28+
file_name = 'pi_digits.txt'
29+
with open(file_name) as file_object:
30+
for line in file_object.read():
31+
print(line.rstrip())
32+
33+
# 将打开的各行存储到一个列表中,并且可以在with代码块之外使用这个列表
34+
file_name = 'pi_digits.txt'
35+
36+
with open(file_name) as file_object:
37+
lines = file_object.readlines()
38+
39+
for line in lines:
40+
print(line.rstrip())

part_10/greet_me.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- coding: cp936 -*-
2+
# ʹÓÃjson.dump()´æ´¢Êý¾Ý
3+
4+
import json
5+
6+
filename = 'username.json'
7+
with open(filename) as f_obj:
8+
username = json.load(f_obj)
9+
print("Welcome back, " + username + "!")
10+
11+
12+
13+
14+
15+

part_10/guest.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fajf�żٰ�����

part_10/guest_10_3.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: cp936 -*-
2+
# дÈëÓû§ÊäÈëµÄÏûÏ¢
3+
4+
file_name = 'guest.txt'
5+
6+
with open(file_name, 'a') as file_object:
7+
message = input("What dou you want to see? : ")
8+
file_object.write(message)
9+
10+
11+
12+

part_10/learning_python.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
I love Python.
2+
In Python you can do everything.
3+
In Python you can do anything.
4+
5+

part_10/learning_python_10_1.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding: cp936 -*-
2+
3+
# 以不同的方式打开文件,并显示出他们
4+
5+
6+
file_name = "learning_python.txt"
7+
8+
# 方法1,全部读取打开
9+
with open(file_name) as file_object:
10+
content = file_object.read()
11+
print(content)
12+
13+
# 方法2,一次打开一行
14+
with open(file_name) as file_object:
15+
content = file_object.readline()
16+
print(content)
17+
18+
# 采用循环的方式,依次打开每一行
19+
with open(file_name) as file_object:
20+
while True:
21+
content = file_object.readline()
22+
print(content)
23+
if not content:
24+
break
25+
26+
# 方法3,每次一行的方式读取整个文件,并将每行作为一个元素存储在一个列表中
27+
# 可以采用for循环读取列表中的所有元素
28+
with open(file_name) as file_object:
29+
contents = file_object.readlines()
30+
print(contents)
31+
32+
pi_string = ''
33+
for content in contents:
34+
pi_string += content.strip()
35+
36+
print(pi_string)
37+
pi_string.replace('love','hate')
38+
print("\n" + pi_string)
39+
40+
41+
42+
# 方法4,采用fileinput模块
43+
import fileinput
44+
for line in fileinput.input("learning_python.txt"):
45+
print(line)
46+
47+
48+
49+
50+
aaa = "www.w3cschool.cc"
51+
print ("菜鸟教程旧地址:", aaa)
52+
print ("菜鸟教程新地址:", aaa.replace("w3cschool.cc", "runoob.com"))
53+
54+
aaa = "this is string example....wow!!!"
55+
print (aaa.replace("is", "was", 3))
56+
57+
58+
aaa = "I reallly like dogs."
59+
aaa = aaa.replace("dogs", "cats")
60+
print (aaa)
61+

part_10/number.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1, 3, 5, 7, 11, 13]

part_10/number_reader.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: cp936 -*-
2+
# ʹÓÃjson.dump()´æ´¢Êý¾Ý
3+
4+
import json
5+
6+
numbers = [1, 3, 5, 7, 11, 13]
7+
8+
filename = 'number.json'
9+
with open(filename) as f_obj:
10+
numbers = json.load(f_obj)
11+
12+
print(numbers)
13+
14+
15+
16+
17+
18+

part_10/number_writer.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- coding: cp936 -*-
2+
# ʹÓÃjson.dump()´æ´¢Êý¾Ý
3+
4+
import json
5+
6+
numbers = [1, 3, 5, 7, 11, 13]
7+
8+
filename = 'number.json'
9+
with open(filename, 'w') as f_obj:
10+
json.dump(numbers, f_obj)
11+
12+
13+
14+
15+
16+

part_10/pi_digits.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3.1415926535
2+
8979323846
3+
2643383279
4+

0 commit comments

Comments
 (0)