camels = 42
water = 0.001
print("There are %d camelss"%(camels))
print("There are %d camels and also %g litres of water"%(2334343, 0.0011))
print("There are {} camels and {} litres of water".format(water, camels))
print("There are {cam} camels and {h20} litres of water".format(h20=water, cam=camels ))
var1 = "This will have {} dogs and {} cats"
print(var1.format(3, 4))
print(var1.format(100, 45))
var2 = f"There are {camels} camels and {water} volume of water"
print(var2)
try:
fin = open('worerererereerererds.txt')
print("file was succesfully opened!!!")
except:
print("hey dude, give a better filename")
a = 10
b = 20
print(a+b)
pin = 90
try:
print(pinx) # throws Name Error
except SyntaxError:
print("succesfully caught the syntax error")
except NameError:
print("hey now i am catching the name error")
else:
pass
finally:
import turtle
import math
bob = turtle.Turtle()
def rectangle(t,length):
for i in range(4):
t.fd(length)
t.lt(90)
def circle(t,r):
n_of_edge=70
circle_length = 2*math.pi*r
angle = 360.0/n_of_edge
length_of_edge = circle_length/n_of_edge
for _ in range(int(n_of_edge)):
t.fd(length_of_edge)
t.lt(angle)
def arc(t,r):
n_of_edge=700
circle_length = 2*math.pi*r
angle = 360.0/n_of_edge
length_of_edge = circle_length/n_of_edge
for _ in range(int(n_of_edge)/2):
t.fd(length_of_edge)
t.lt(angle)
class Time(object):
def __init__(self,hour,minute,second):
self.hour = hour
self.minute = minute
self.second = second
def print_time(self):
print("%.2d:%.2d:%.2d"%(self.hour,self.minute,self.second))
c = Time(111.233333,.233333,.233333)
c.print_time()
output: 111:00:00
假设说不要初始化函数
class Time(object):
# def __init__(self,hour,minute,second):
# self.hour = hour
# self.minute = minute
# self.second = second
def print_time(self):
print("%.2d:%.2d:%.2d"%(self.hour,self.minute,self.second))
此时:
c = Time(111.233333,.233333,.233333)
c.print_time()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-a6174a4498c2> in <module>
----> 1 c = Time(111.233333,.233333,.233333)
2 c.print_time()
TypeError: Time() takes no arguments
通过**.**可以直接给对象c中的字典赋值。
c.hour = 111
c.minute = 222
c.second = 99
c.print_time()
output:
111:222:99
p1.__dict__
output: {'hour': 111, 'minute': 222, 'second': 99}
新建一个文件my_own_add.py
var_1 = 250
var_2 = 360
def my_add(a,b):
return a+b
此时使用如下命令调用包中的一个函数my_add(a,b)和常量var_1,var_2
$ python3
Python 3.7.3 (default, Mar 6 2020, 22:34:30)
[Clang 11.0.3 (clang-1103.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import my_own_add
>>> my_own_add.var_1
250
>>> my_own_add.var_2
360
>>> my_own_add.my_add(11111,22222)
33333
#直接指定要import这个函数,这样使用起来就不用加点
>>> from my_own_add import my_add
>>> my_add(222,333)
555
如果说一个文件特别杂乱
f_txt = open("words.txt")
String_txt = f_txt.read()
import re
#Set stop words
# filter = ['the', 'a', 'an','and','of','in','The','to','s']
filter = []
String_txt = re.sub("[\n\s+\.\!\/_,$%^*(+\"\')]+|[+——()?【】“”!,。?、~@#¥%……&*:()]+"," ",String_txt)
list_txt = String_txt.split(' ')
如果一个文件特别工整,比如这样
aa
aah
aahed
aahing
aahs
aal
aalii
aaliis
可以用:
#read the word list
f_txt = open("words.txt")
String_txt = f_txt.read()
# to lower_case
String_txt = String_txt.lower()
list_txt = String_txt.split('\n')
def most_frequent(string_input):
list_txt = string_input.split(' ')
dict_count = {}
for _ in list_txt:
if _ not in filter:
# print(_)
dict_count[_] = 0
for _ in list_txt:
if _ not in filter:
# print(_)
dict_count[_] += 1
dict_count_sorted = sorted(dict_count.items(), key=lambda item: item[1],reverse=True)
# print(dict_count)
return (dict_count,dict_count_sorted)
dir_demo = '/Users/lianda_duan/Downloads/lab_4_assignment'
import os
file_path_list = []
def walk(dirname = dir_demo):
for name in os.listdir(dirname):
path = os.path.join(dirname, name)
if os.path.isfile(path):
# print(path)
file_path_list.append(path)
else:
walk(path)
walk()
for _ in file_path_list:
print(_)
file_path_list = [i for i in file_path_list if str(i).split("/")[-1].split(".")[-1] == 'mp3']
for _ in file_path_list:
print(_)
import filecmp
list_check = list(range(len(file_path_list)))
# list_check =
# [i if i.split("/")[-1]=='mp3' for i in list_check]
dup_dic = {}
# initialize dic
for _ in [_.split("/")[-1] for _ in file_path_list]:
dup_dic[_] = []
# print(dup_dic)
for index_file_path_be_compared in list_check:
# if index_file_path_be_compared is not len(file_path_list)-2:
for index_file_dest in list_check:
if filecmp.cmp(file_path_list[index_file_path_be_compared], file_path_list[index_file_dest]):
dup_dic[file_path_list[index_file_path_be_compared].split("/")[-1]].append(file_path_list[index_file_dest].split("/")[-1])
# list_check.pop(index_file_dest)
# print(filecmp.cmp(c_1, c_2))
# print(filecmp.cmp(c_1, c_3))
for _ in dup_dic.items():
print(_,'\n')
import sys
print(sys.executable)
例如所得的路径为:
/Users/lianda_duan/opt/anaconda3/bin/python
如果想在这个路径下调用pip安装一个包,可以使用以下指令:
#Run terminal command to install wordcloud in current python working environment.
!/Users/lianda_duan/opt/anaconda3/bin/python -m pip install wordcloud
from PIL import Image
from wordcloud import WordCloud
import matplotlib.pyplot as plt
#The higher the frequence, the larger front size the words will be in.
wc = WordCloud(background_color="yellow",width=3000,height=2000, max_words=200,relative_scaling=0.8,normalize_plurals=False).generate_from_frequencies(dictionary_for_word_cloud)
plt.imshow(wc)
其中:
dictionary_for_word_cloud
是一个字典。
fout = open("newFileMadeByMe.txt", mode='w')
fout.write("this is the first line")
fout.write("this is the first line")
1、(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库
git init
2、把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数点“.”,意为添加文件夹下的所有文件
git add .
3、用命令 git commit告诉Git,把文件提交到仓库。引号内为提交说明
git commit -m 'first commit'
4、关联到远程库
git remote add origin 你的远程库地址
如:
git remote add origin https://github.com/githubusername/demo.git
5、获取远程库与本地同步合并(如果远程库不为空必须做这一步,否则后面的提交会失败)
git pull --rebase origin master
6、把本地库的内容推送到远程,使用 git push命令,实际上是把当前分支master推送到远程。执行此命令后会要求输入用户名、密码,验证通过后即开始上传。
git push -u origin master
*、状态查询命令
git status
git查看远程仓库地址命令
git remote -v
# 递归
def Fibonacci_Recursion_tool(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return Fibonacci_Recursion_tool(n - 1) + Fibonacci_Recursion_tool(n - 2)
def Fibonacci_Recursion(n):
result_list = []
for i in range(1, n + 1): result_list.append(Fibonacci_Recursion_tool(i))
return result_list
git clone https://github.com/inwk6312-summer2020/gwertfdfdty.git