How to teach beginners reversing a string in Python?

I am teaching a course "Introduction to Computer Programming" to the first year math students. One has to assume that this is the first exposure of students to computer programming. Here are the main goals of my teaching: Students should learn and understand the basics of Python. Eventually they need to master sufficiently many Python tools so that they are able to select the righ

如何教在Python中反转字符串的初学者?

我正在给一年级数学学生授课“计算机程序设计入门”课程。 人们不得不假设这是学生第一次接触计算机编程。 这是我教学的主要目标: 学生应该学习和理解Python的基础知识。 最终,他们需要掌握足够多的Python工具,以便他们能够为给定问题选择正确的工具。 同时,他们必须学习计算机编程解决问题的基本技能。 我的教学方法是为每个新引入的概念提供一系列激励学生的问题和挑逗。 例如,当引入字符串和列表时,自然问题是

Nicest way to pad zeroes to string

什么是填充数字字符串最左边的零的最pythonic方式,即,所以数字字符串具有特定的长度? Strings: >>> n = '4' >>> print n.zfill(3) 004 And for numbers: >>> n = 4 >>> print '%03d' % n 004 >>> print format(n, '03') # python >= 2.6 004 >>> print '{0:03d}'.format(n) # python >= 2.6 004 >>> print '{foo:03d}'.format(foo=n) # python >

最好的方式填充零字符串

什么是填充数字字符串最左边的零的最pythonic方式,即,所以数字字符串具有特定的长度? 字符串: >>> n = '4' >>> print n.zfill(3) 004 对于数字: >>> n = 4 >>> print '%03d' % n 004 >>> print format(n, '03') # python >= 2.6 004 >>> print '{0:03d}'.format(n) # python >= 2.6 004 >>> print '{foo:03d}'.format(foo=n) # python >= 2.6

Best ways to teach a beginner to program?

Original Question I am currently engaged in teaching my brother to program. He is a total beginner, but very smart. (And he actually wants to learn). I've noticed that some of our sessions have gotten bogged down in minor details, and I don't feel I've been very organized. (But the answers to this post have helped a lot.) What can I do better to teach him effectively? Is ther

教新手入门的最佳方法?

原始问题 我目前正在教我的兄弟编程。 他是一个初学者,但非常聪明。 (他实际上想学习)。 我注意到,我们的一些会议已经陷入了一些小细节,我不觉得自己一直很有组织。 (但这篇文章的答案有很大帮助。) 我能更好地教他有效地做什么? 有没有一种逻辑顺序可以用来贯穿概念概念? 到了晚些时候我应该避免复杂吗? 我们正在使用的语言是Python,但欢迎任何语言的建议。 如何帮助 如果你有好的,请在你的回答中

Access class member without initialization of object

In the Python module "Wave" I can use the following syntax: import wave wave.open("test.wav", "rb") This works perfectly fine.Let's say I wanted to use my own initialization of a class: class Wave(): def __init__(self): pass; def Open(self, fileName, Type): return True; # Just testing Now If I have "main" which calls this class, why can't I

无需初始化对象的访问类成员

在Python模块“Wave”中,我可以使用以下语法: import wave wave.open("test.wav", "rb") 这工作得很好。让我们说我想用我自己的类的初始化: class Wave(): def __init__(self): pass; def Open(self, fileName, Type): return True; # Just testing 现在,如果我有称为这个类的“主”,为什么我不能执行以下操作?: if Wave.Open("testing.wav", "rb"): print "The file is open" TypeError:

python local variables vs self

What's the difference between self.x+self.y and x+y in the code below? class m2: x, y = 4, 5 def add(self, x, y): return self.x + self.y def add2(self, x, y): return x + y >>> x = m2() >>> print "x.add(self.x + self.y = )", x.add(1, 2) x.add(self.x + self.y = ) 9 >>> print "x.add2(x + y = )", x.add2(1, 2) x.add2(x + y = ) 3 Why does se

python局部变量vs self

下面代码中self.x+self.y和x+y什么区别? class m2: x, y = 4, 5 def add(self, x, y): return self.x + self.y def add2(self, x, y): return x + y >>> x = m2() >>> print "x.add(self.x + self.y = )", x.add(1, 2) x.add(self.x + self.y = ) 9 >>> print "x.add2(x + y = )", x.add2(1, 2) x.add2(x + y = ) 3 为什么self.x + self.y返回9而x + y返回3 ? 在

Using raw input inside of a class (python 2.7)

I was wondering how to use raw input inside of a class, so instead of passing the name 'Ryan' we could pass a variable inside of the object and ask for that variable later on. such as: name = raw_input("What is your name? ") Here is the code I have: class Talk: def __init__(self, name): self.name = name print "Hey there, " + self.name

在类中使用原始输入(python 2.7)

我想知道如何在类中使用原始输入,所以不要传递名称'Ryan',我们可以在对象内部传递一个变量,然后再请求该变量。 如: name = raw_input("What is your name? ") 这里是我有的代码: class Talk: def __init__(self, name): self.name = name print "Hey there, " + self.name def printName(self): print self.name talk = Talk('Ryan') 通

Trying to create an object via a user's input

I'm trying to create an employee object via user input, but I'm running into issues with my code. When I run this nothing happens and it's not throwing any errors either. class employee(object): def __init__(self,name,pay_rate,monday,tuesday,wednesday,thursday,friday,saturday,sunday): self.create_employee() self.name = name self.pay_rate = pay_rate

试图通过用户的输入创建一个对象

我试图通过用户输入创建一个employee对象,但我遇到了我的代码问题。 当我运行这个没有任何反应,它也没有抛出任何错误。 class employee(object): def __init__(self,name,pay_rate,monday,tuesday,wednesday,thursday,friday,saturday,sunday): self.create_employee() self.name = name self.pay_rate = pay_rate self.monday = monday self.tuesday = tuesday self.w

Creating a Python classmethod

I've familiarized myself with the concept, most notably by watching Raymond Hettinger's excellent video and reading the accepted answer here and I am wondering what I got wrong. class ReadHTML(object): def __init__(self, url): page = urlopen(url).read() self.page = page @classmethod def from_file(cls, path): page = open(path).read() return cl

创建一个Python类方法

我已经熟悉了这个概念,特别是通过观看Raymond Hettinger的优秀视频并阅读接受的答案,我想知道我错了什么。 class ReadHTML(object): def __init__(self, url): page = urlopen(url).read() self.page = page @classmethod def from_file(cls, path): page = open(path).read() return cls(page) 这工作 r = ReadHTML('http://example.com') print r.page 而这不是 r = Read

Python saying I'm passing in too many parameters to my function?

I have some python code that contains unit tests like below: class SunCalcTestCases(unittest.TestCase): """Tests for `suncalc.py`.""" def near(val1, val2): return abs(val1 - val2) < (margin or 1E-15) def test_getPositions(self): """Get sun positions correctly""" sunPos = suncalc.getPosition(self.date, self.lat, self.lng) az = sunPos["azimuth"]

Python说我传递了太多的参数给我的函数?

我有一些包含如下单元测试的Python代码: class SunCalcTestCases(unittest.TestCase): """Tests for `suncalc.py`.""" def near(val1, val2): return abs(val1 - val2) < (margin or 1E-15) def test_getPositions(self): """Get sun positions correctly""" sunPos = suncalc.getPosition(self.date, self.lat, self.lng) az = sunPos["azimuth"] res = self.near(az

function as class attribute becomes a bound method

I noticed that if I define a class attribute equal to a function when I create an instance of that class the attribute becomes a bound method. Can someone explain me the reason of this behaviour? In [9]: def func(): ...: pass ...: In [10]: class A(object): ....: f = func ....: In [11]: a = A() In [12]: a.f Out[12]: <bound method A.func of <__main__.A object at

函数作为类属性成为绑定方法

我注意到,如果我在创建该类的实例时定义了一个等于某个函数的类属性,该属性将成为一个绑定方法。 有人能解释我这种行为的原因吗? In [9]: def func(): ...: pass ...: In [10]: class A(object): ....: f = func ....: In [11]: a = A() In [12]: a.f Out[12]: <bound method A.func of <__main__.A object at 0x104add190>> In [13]: a.f() -----------------------------------