Giter Site home page Giter Site logo

benpython's People

Contributors

zilongxuan001 avatar

Stargazers

 avatar

Watchers

 avatar

benpython's Issues

ex33 for循环不受里面i=i+1的影响

下列代码的for循环里,带有i=i+1,但是i不受到i=i+1的影响

numbers =[]

for i in range(6):
	print("At the top i is %d"%i)
	numbers.append(i)
	
	# i= i+1
	print("Numbers now: ", numbers)
	print("At the bottom i is %d" %i)

print("The numbers: ")

for num in numbers:
	print(num)

在cmd里运行,结果为
image
如果去掉i=i+1,结果为
image

ex36 调试的技巧

调试的技巧

自己简化

1.不要使用debugger,因为你得到多是无用的信息。
2. 最好的调试程序的方法——print你想要检查的关键环节,打印出关键变量。
3. 程序写一点,运行一点,再修改一点。不要等完成后再去运行。
来源:习题 36: 设计和调试

命名文件和编写文件应注意的事项

  1. 文件名最好加上文件的标题 如 ex33whiel.py
    2.git commit -m""里,可以加上文件内容 如git commit -m" add ex33 while"
    3.在notepad++里做各种练习时,做完一个就直接ctrl+k,加上#,保存一个,然后再做下一个。不要在原习题上做下一个练习。

ex38 如何阅读理解代码

如何阅读和理解理解代码

1.打印你要理解的代码
2.在纸上通读代码并做标记
(1)函数和函数的功能
(2)变量的初始赋值
(3)多次出现的变量
(4)不包含else的if语句,这种语句正确吗?
(5)while循环有结束点吗?
(6)记下你看不懂的代码
3.写下你的注解:
(1)各函数的使用方法
(2)变量的用途
(3)其他帮助理解的内容
4.如果还有难以理解的代码,则逐行或逐个跟踪函数的变量值。
可以把难以理解代码再打印一份,写出你要追踪的每个变量的值。
5.如果你理解了,回到电脑前,再读一次,看能否找到新的问题点。

来源:http://old.sebug.net/paper/books/LearnPythonTheHardWay/ex38.html

ex32 list的操作

list的操作

(1)pop 删除某个位置上的元素,返回的是删除的元素
例如 删除末尾元素 elements.pop()
删除特定位置元素 elements.pop(4)

(2)del 删除某个位置上的元素,直接删除
例如 del elements[0]

(3)inert 在某个位置上插入某个元素 insert(插入位置,元素名称)
例如 elements.insert(3,'good')

(4)remove 删除某元素,若list里有多个相同元素,则删除第一个相同元素。
例如 elements.remove('good')

(5)count 计算该元素在list中出现的次数。
例如 elements.count('good')

(6) index 计算该元素在list中的位置,如该元素不存在,则抛出异常。如有多个相同元素,则给出第一个元素位置。
例如 elements.index('good')

(7)append 在列表末尾添加元素
例如 elements.append(4)

(8)extend 将某个列表中的元素合并到列表的末尾
例如 elements.extend(['today','morning'])

(9)sort 对列表的元素进行排序,同时存在字符串和数字,则无法排序。
例如 elements.sort()

(10)reverse 对列表的元素进行倒序,同时存在字符串和数字,则无法排序。
例如 elements.reverse()

ex36 if 语句规则

if语句规则

自己将其简化

1.if语句必须含至少一个else。
2.不该执行到的else语句,应该有个结束循环的函数。
3.if语句最好只有1层,不要超过2层。如果有2层,将第二层新建一个函数。
4.if语句就像段落,if、elif和else组合,就像一个句子组合。每个组合的前后要空一行,以示区分。
5. 布尔测试要简单,如果复杂,可以将运算赋值给一个变量。
来源:习题 36: 设计和调试

ex39 python如何读取mystuff.append("hello")

python如何读取mystuff.append("hello")
(1)Python看到mystuff,就去寻找这个变量。看创建变量的位置,检查其是否是函数参数,或者是否是全局变量。
(2)找到了mystuff,处理句点. 。查看mystuff内部变量,因为mystuff是个列表,所以Python知道mystuff支持列表的函数
(3)处理append。将append的名称和mystuff支持的函数一一对比,如果有这个函数,则调用该函数。
(4)处理括号。Python遇见括号,就知道这是个函数,开始调用函数。
(5)如何调用函数。Python将apend('hello')转换为append(mystuff, 'hello')。

参考来源:http://old.sebug.net/paper/books/LearnPythonTheHardWay/ex39.html

github一个小错误——远程文件和本地文件不同步

因为github里出现了readme.txt,但本地文件夹没有这个文件,所以提交的时候出现了下列错误

error: failed to push some refs to '[email protected]:zilongxuan001/benpython.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方法
在gitbash里输入
git pull --rebase origin master
进行代码合并

再输入
git push origin master,即可。

ex24的新知识——def函数的return多个值,赋予多个变量和格式化符号

1. 如果def的函数return三个值的话,那么可以直接赋值给三个变量

‘beans, jars, crates = secret_formula(start_point)`

2. 如果def的函数return三个值的话,那么可以在三个格式化符号后直接加上这个函数

print("We'd have %d beans, %d jars, and %d crates."%secret_formula(start_point))
源代码如下

def secret_formula(started):
	jelly_beans =started *500
	jars = jelly_beans/1000
	crates = jars /100
	return jelly_beans, jars, crates


start_point  = 10000
beans, jars, crates = secret_formula(start_point)

print("With a starting point of: %d" % start_point)
print("We'd have %d beans, %d jars, and %d crates."%(beans, jars, crates))

start_point =start_point/10

print("We can also do that this way:")
print("We'd have %d beans, %d jars, and %d crates."%secret_formula(start_point))

ex35 判读输入是否为数字

str=input('> ')
str.isalnum() 如果所有字符都是数字或者字母,则为True,否则为False。
str.isalpha() 如果所有字符都是字母,则为True,否则为False。
str.isdigit() 如果所有字符都是数字,则为True,否则为False。
str.islower() 如果所有字符都是小写,则为True,否则为False。
str.isupper() 如果所有字符都是大写,则为True,否则为False。
str.istitle() 如果所有单词都是首字母大写,则为True,否则为False。
str.isspace() 如果所有字符都是空白字符、\t、\n、\r,则为True,否则为False。
参考来源:判断Python输入是否为数字

ex05 格式化字符串

height=74 #inches
weight=180 #lbs

print('He\'s %d inches.' %(height))
print('He is %d cm.' %(height*2.54))
print('He\'s %.2f lbs.'%(weight))
print('He is %.3f kg.' %(weight*0.4635924))

正确写法:print('He is %.3f kg.' %(weight*0.4635924))
错误写法
1.
·print('He is %.3f kg.' %(height*0.4635924))·
原因:应该为weight,不是height.

·print('He is %f.3 kg.' %(weight*0.4635924))·
原因: 应该是 %.3f。

运行python提示错误
image

格式化字符串(var)

image

students=[{'name':'wanger','grade':1},{'name':'zhaosi','grade':2}]
for student in students:
	print("%(name)s grade is %(grade)d"%student)

ex05犯的错误——格式化符号要加%

print('If I add %d, %d, and %d I get %d.' (age, height, weight, age + height + weight))
因为没有加%,导致出现

image
正确应该是
print('If I add %d, %d, and %d I get %d.' %(age, height, weight, age + height + weight))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.