自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(37)
  • 收藏
  • 关注

原创 正则表达式

python的正则表达式是十分有用的,学会后匹配几乎任何格式都没有问题,应该熟练掌握。http://www.runoob.com/python/python-reg-expressions.html# 总结# ^ 匹配字符串的开始。# $ 匹配字符串的结尾。# \b 匹配一个单词的边界。# \d 匹配任意数字。# \D 匹配任意非数字字符。# x? 匹配一个可选的 x 字符 (换言之,...

2018-06-05 23:27:05 138

原创 python找出因数与质因数

    最近有一个学弟问我一些求解质因数的问题,帮他解决问题的同时自己也试着写了几个差不多效果的脚本,有很多不同的思路,以下是相关脚本。n = int(input("input number: ")) # 输入数字fac = [] # 定义一个列表存放因子for i in range(2, n): # 这里的逻辑和你一样 if n % i == 0: fac.appen...

2018-06-04 21:16:12 27845

原创 借由字典的匹配学习类中方法的调用

现在有两个字典,想要根据id匹配。但这不是目的,我想尝试的是类中方法的自调用和局部,全局变量的用法。#coding : utf-8class MatchCase(object): def __init__(self, case_1, case_2): # 定义类的属性参数 self.case_1 = case_1 self.case_2 = case_2...

2018-06-02 00:13:12 178

原创 装饰器的一些问题

    装饰器是为了再不改变函数内部结构的情况下附加给方法一些功能,就像是给方法套上了一层有功能的外壳。如果把一个方法比作一个人,那么装饰器就是它的一些工具,用不同的装饰器可以让这个人做不同的事,但本身外物并没有改变人的内部结构。    装饰器本身也是一个函数,一般的装饰器会涉及三个函数,我将其记为dec,wrap和func。dec是指最外层的函数,也就是装饰器本身,而wrap是内层函数,并不执行...

2018-05-20 20:34:20 133

原创 类的定义以及调用

#coding: utf-8class Bubble(object): # 首先定义一个类,如果没有继承的父类,则括号里为object即可 def __init__(self, *numbers): # __init__方法,在调用类的时候首先会调用它,它可以说明类的属性,也就是给这个类定义用什么样的参数。*numbers说明numbers是一个tuple,为动态个数的参数 ...

2018-05-20 15:04:40 698

原创 insertion_sort.py 插入排序

动态图:https://visualgo.net/zh/sortingpython语句:#coding: utf-8# 插入排序input = raw_input("input several numbers: ")numbers = map(int,input.split(' '))n = len(numbers)for j in range(n-1): for i in ran...

2018-05-06 22:18:45 125

原创 ex30.py else和if

people = 30cars = 40buses = 15if cars > people: print "We should take the cars."elif cars < people: print "We should not take the cars."else: print "We can't decide."if buses > car...

2018-05-06 21:14:19 161

原创 ex29.py if语句

#coding: utf-8people = 20cats = 30dogs = 15if False and True: # 看到了吗?if后面跟的是一些判断语句,如果为True就执行,否则就不执行 print "Too many cats! The world is doomed."if people > cats: print "Not many cats! The ...

2018-05-06 21:12:50 166

原创 ex28.py 布尔表达式练习

#coding: utf-8print True and True print 2 or "dog" #当这样的情况下返回的若是真则返回第一个操作符,假则返回第二个操作符print 1 == 1 and 1 == 2print 'test' and 'test'print 1 == 1 or 2 != 1, "\n"print True and 1 == 1print False a...

2018-05-06 21:12:04 184

原创 ex27.txt 逻辑关系

逻辑术语and 与or 或not 非!= 不等于== 等于>= 大于等于<= 小于等于True 真False 假真值表NOT: 与之相反 not False True not True FalseOR: 有一个真即为真 True or False True True or True True False or True Tr...

2018-05-06 21:11:04 129

原创 selection_sort.py 选择排序

动态图:https://visualgo.net/en/sortingpython语句:#coding: utf-8# 选择排序input = raw_input("input several numbers: ")numbers = map(int,input.split(' '))n = len(numbers)for j in range(1,n): key = j - 1...

2018-05-06 21:09:33 195

原创 bubble_sort.py 冒泡排序

动态图:https://visualgo.net/en/sortingpython语句:#coding: utf-8# 冒泡排序input = raw_input("input several numbers: ")numbers = map(int, input.split(' '))n = len(numbers)for j in range(1,n): for i in ra...

2018-05-06 21:07:37 239

原创 ex26.py 调试程序

原来有错误的程序:def break_words(stuff): """This function will break up words for us.""" words = stuff.split(' ') return wordsdef sort_words(words): """Sorts the words.""" return sor

2018-05-05 20:26:06 153

原创 ex25.py 定义函数并在shell中调取函数

    这个单元不仅仅是编写这样的python程序然后运行这么简单了,这次我们要首先在编译器里面编写一段python代码,然后到shell中去运行。    下面是python程序:#coding: utf-8# 首先我们看到这个程序和其他的程序有所不同在于它并不能运行什么,他只是申明了很多函数,我们要做的仅仅是先把它打出来# 去shell中看看他有没有错,同时在shell中调用它包含的函数。...

2018-05-05 16:45:58 246

原创 ex24.py 更多的练习

#coding: utf-8# 这个程序没什么特别的,就是不断练习以前学过的东西# 第一部分:转义符 \'单引号 \\反斜杠 \n换行 \t缩进符print "Let's practice everything."print "You\'d need to know\'bout escapes with \\ that do \n newlines and \t tabs."# \'这里...

2018-05-05 16:20:12 148

原创 ex21.py 带有返回值的函数

#coding: utf-8# 简单的函数调用,返回一个值def add(a, b): print "ADDING %d + %d" % (a, b) return a + bdef substract(a, b): print "SUBSTRACTING %d - %d" % (a, b) return a - bdef multiply(a, b): print "MUL...

2018-05-05 16:18:43 185

原创 ex20.py 函数和文件

#coding: utf-8# 这个脚本是为了打印一些文件内容,你在运行时需要一个大于三行的文件作为参数,比如:python ex20.py test.txtfrom sys import argv # 从包中调用argv模块script, input_file = argv # 第一个变量总是script,第二个变量是要输入的文件def print_all(f): # 定义第一个函数...

2018-05-05 16:17:43 227

原创 ex19.py 函数和变量

#coding: utf-8# 这个脚本主要是为了用各种不同的方式调用函数,并且试图让你弄懂什么是全局变量和函数内部的变量# 下面这部分是函数的申明,它里面的函数变量和后面调用的变量名是不一样的,但是这不影响,因为函数申明时就好像一个指代关系# 就好像说cheese_count, boxes_of_crackers就是函数里面的第一第二个变量,以后调用函数只要在是这个位置的变量就可以,名字完...

2018-05-05 16:16:43 145

原创 ex18.py 基本函数使用

#coding: utf-8#这是简单函数的调用,无需输入任何参数,只需要在终端打开它就好了# this one is like your scripts with argvdef print_two(*args): # *args是告诉python把所有的参数都接受进来,然后放到args列表中去。 arg1, arg2 = args print "arg1: %r, arg2: %r"...

2018-05-04 22:06:37 7013

原创 ex17.py 更多文件和操作

#coding: utf-8# 我们这次试图讲一个文件中的内容复制到另外一个文件中,要运行这个程序要输入两个参数,一个是已经有的txt文件,一个是新的txt文件,比如python ex17.py test.txt new_text.txt (假定我已经有了一个test.txt的情况下)from sys import argv # 常规调用argv,使程序运行参数from os.path im...

2018-05-04 22:02:53 285

原创 ex16.py 读写文件

#coding: utf-8# 本例旨在只做一个简单的文本编辑器,运行这个程序需要输入一个你想创建的文件名,无所谓存在与否,比如:python ex16.py test.txtfrom sys import argv # 常规操作调用script, filename = argv # 注意这个script,在所有的参数里都第一个申明它,以后在使用它时也要作这样的申明print "We'...

2018-05-04 22:01:57 283

原创 ex15.py 读取文件

#coding: utf-8# 运行这个程序需要一个已经存在的文件,你需要先创建一个txt文件并在里面写入一些内容,比如python ex15.py iamcool.txtfrom sys import argv # sys是一个软件包,这句话的意思是从sys中调用出argv这个模块script, filename = argvtxt = open(filename) # 在txt(变...

2018-05-04 22:01:06 182

原创 ex14.py 提示和传递

from sys import argv # 运行这个程序你需要输入一个参数,也就是你的名字,比如:python ex14.py Jerryscript, user_name = argvprompt = '>' print "Hi %s, I'm the %s script." % (user_name, script)print "I'd like to ask you a...

2018-05-04 22:00:13 131

原创 ex13.py 参数,解包和变量

# coding: utf-8from sys import argv# 运行这个程序你需要三个参数,随便乱编一个吧,比如python ex13.py a b c# 在第一行又一个import语句,这是你将python特性(也称作模块)引入脚本的一种方法。简单的说,这个程序不再是一个可以单独的执行的程序了,而是一个f(x),你必须输入一些参数才能运行它# argv即“参数变量”(argum...

2018-05-04 21:58:58 329

原创 ex12.py 提示别人

# coding: utf-8age = raw_input("How old are you? ")height = raw_input("How tall are you? ")weight = raw_input("How much do you weight? ")print "So you are %r years old, %r cms tall,and %r kgs wei...

2018-05-04 21:57:19 99

原创 ex11.py 输入的问题

#coding: utf-8print "How old are you?",age = raw_input() # raw_input主要是输入字符串的信息,不管输入什么都是字符串,raw_input(提示信息:)print "How tall are you?",height = raw_input()print "How much do you weight?",weight =...

2018-05-04 11:20:28 85

原创 ex10.py 转移符号

#coding: utf-8tabby_cat = "\tI'm tabbed in." # 引用制表符(tab),即\t代表一个制表符(大空格)persian_cat = "I'm split \non a line." # \n代表换行(new line)backslash_cat = "I'm \\ a \\ cat." # \\代表\的意思fat_cat = """I'll d...

2018-05-03 23:06:59 161

原创 ex9.py 打印,打印,打印

#coding: utf-8# Here's some new strange stuff, remember type it exactly.days = "Mom Tue Wed Thu Fri Sat Sun" #定义一串字符串到days变量中months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" #这里注意转义符\后面加不同字母表示不同含...

2018-05-03 21:53:20 133

原创 ex8.py 打印,打印

#coding: utf-8formatter = "%r %r %r %r" #在这里引号内的内容既可以表示引用格式(即为“待补空位”)也可表示纯文本print formatter % (1, 2, 3, 4) #当作引用格式处理,里面填充数字print formatter % ('one', 'two', 'three', 'four') #将字符串进行填充print formatt...

2018-05-03 20:57:16 137

原创 linux终端的18个基本命令

linux的18个快速命令1.pwd 打印工作目录2.hostname 电脑名称3.mkdir 创建路径 eg:mkdir temp #生成当前位置文件夹mkdir ~/temp2 #生成指定路径文件夹mkdir -p temp1/temp2/temp3 #递归的生成目录,可以一次性生成一条路径4.cd 更改路径(注意空格)eg:cd Users/mazeyu/ #去指定目录cd .. #返回前一...

2018-05-03 19:49:33 200

原创 ex7.py 更多的打印

#coding: utf-8print "Mary had a little lamb." #输出字符串print "Its fleece was white as %s." % 'snow' #注意snow要加引号,否则就是一个变量,没定义过print "And everywhere that Mary went."print "." * 10 # What that do? #一个点输...

2018-05-03 19:47:48 75

原创 ex6.py 字符串和文本

#coding: utf-8x = "There are %d types of people." % 10 #定义x为一串数组变量binary = "binary" #定义binary为字符串“binary”do_not = "don't" y = "Those who know %s and those tho %s." % (binary, do_not) #输出一句话,其中两个单词...

2018-05-03 19:47:29 121

原创 ex5.py 更多的变量和打印

#coding: utf-8name = 'Jerry'age = 22 # not a lieheight = 74 # inchesweight = 180 # lbseyes = 'Black'teeth = 'White'hair = 'Black'print "Let's talk about %s." % nameprint "He's %d inches tall...

2018-05-03 19:47:10 79

原创 ex4.py 变量和命名

cars = 100 # variablespace_in_a_car = 4.0drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity = cars_driven * space_in_a_caraverage_passengers_per_ca...

2018-05-03 19:46:56 100

原创 ex3.py 数字和数学计算

#coding: utf-8print "I will now count my chickens:" #输入一些语句print "Hens", 25 + 30 / 6 #输出一句话,同时后面接计算出的数字,用“,”连接print "Roosters", 100 - 25 * 3 % 4 #%代表取余数符号print "Now I will count the eggs:" #输出一句...

2018-05-03 19:46:33 184

原创 ex2.py 注释和#号

#A comment, this is so you can read your program later.#Anything after the # is ignored by python.print "I could have code like this." #and the comment after is ignored#You can also use a comment...

2018-05-03 19:46:04 124

原创 ex1.py 第一个程序

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.'

2018-05-03 19:45:47 173

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除