标签归档:language-agnostic

什么是“一流”物品?

问题:什么是“一流”物品?

在给定的编程语言中,什么时候将对象或其他东西称为“一流”,为什么?它们与没有语言的语言有何不同?

编辑。当一个人说“一切都是对象”时(就像在Python中一样),他的确意味着“一切都是一流的”吗?

When are objects or something else said to be “first class” in a given programming language, and why? In what do they differ from languages where they are not?

EDIT. When one says “everything is an object” (like in Python), does he indeed mean that “everything is first-class”?


回答 0

简而言之,这意味着对对象的使用没有任何限制。它与任何其他对象相同。

第一类对象是一个实体,可以动态创建,销毁,传递给函数,将其作为值返回,并具有编程语言中其他变量所具有的所有权利。

根据语言,这可能意味着:

  • 可表示为匿名文字值
  • 可存储在变量中
  • 可存储在数据结构中
  • 具有固有身份(独立于任何给定名称)
  • 在与其他实体平等方面具有可比性
  • 可作为参数传递给过程/功能
  • 由于过程/功能可退还
  • 在运行时可构造
  • 可打印
  • 易读
  • 在分布式过程之间可传播
  • 在外部运行过程中可存储

来源

在C ++函数中,它们本身不是一流的对象,但是:

  • 您可以覆盖’()’运算符,以使它具有对象函数,这是一流的。
  • 函数指针是一流的。
  • boost bind,lambda和function确实提供了一流的功能

在C ++中,类不是一流的对象,但这些类的实例是。在Python中,类对象都是一流的对象。(有关将类作为对象的更多详细信息,请参见此答案)。

这是Javascript一流函数的示例:

// f: function that takes a number and returns a number
// deltaX: small positive number
// returns a function that is an approximate derivative of f
function makeDerivative( f, deltaX )
{
    var deriv = function(x)
    { 
       return ( f(x + deltaX) - f(x) )/ deltaX;
    }
    return deriv;
}
var cos = makeDerivative( Math.sin, 0.000001);
// cos(0)     ~> 1
// cos(pi/2)  ~> 0

来源

不是第一类对象的实体称为第二类对象。C ++中的函数是第二类,因为它们不能动态创建。

关于编辑:

编辑。当一个人说“一切都是对象”时(就像在Python中一样),他的确意味着“一切都是一流的”吗?

对象一词可以宽松地使用,并不意味着是一流的。将整个概念称为“一流实体”可能更有意义。但是在Python中,它们的目标是使所有东西都达到一流。我相信发表你讲话的人的意图是一流的。

In short, it means there are no restrictions on the object’s use. It’s the same as any other object.

A first class object is an entity that can be dynamically created, destroyed, passed to a function, returned as a value, and have all the rights as other variables in the programming language have.

Depending on the language, this can imply:

  • being expressible as an anonymous literal value
  • being storable in variables
  • being storable in data structures
  • having an intrinsic identity (independent of any given name)
  • being comparable for equality with other entities
  • being passable as a parameter to a procedure/function
  • being returnable as the result of a procedure/function
  • being constructible at runtime
  • being printable
  • being readable
  • being transmissible among distributed processes
  • being storable outside running processes

Source.

In C++ functions themselves are not first class objects, however:

  • You can override the ‘()’ operator making it possible to have an object function, which is first class.
  • Function pointers are first class.
  • boost bind, lambda and function do offer first class functions

In C++, classes are not first class objects but instances of those classes are. In Python both the classes and the objects are first class objects. (See this answer for more details about classes as objects).

Here is an example of Javascript first class functions:

// f: function that takes a number and returns a number
// deltaX: small positive number
// returns a function that is an approximate derivative of f
function makeDerivative( f, deltaX )
{
    var deriv = function(x)
    { 
       return ( f(x + deltaX) - f(x) )/ deltaX;
    }
    return deriv;
}
var cos = makeDerivative( Math.sin, 0.000001);
// cos(0)     ~> 1
// cos(pi/2)  ~> 0

Source.

Entities that are not first class objects are referred to as second-class objects. Functions in C++ are second class because they can’t be dynamically created.

Regarding the edit:

EDIT. When one says “everything is an object” (like in Python), does he indeed mean that “everything is first-class”?

The term object can be used loosely and doesn’t imply being first class. And it would probably make more sense to call the whole concept ‘first class entities’. But in Python they do aim to make everything first class. I believe the intent of the person who made your statement meant first class.


回答 1

“当一个人说“一切都是对象”时(就像在Python中一样),他的意思确实是“一切都是一流的”吗?”

是。

Python中的所有内容都是正确的对象。即使是其他语言中的“原始类型”也是如此。

您会发现像这样的对象2实际上具有相当丰富和复杂的界面。

>>> dir(2)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

由于所有内容都是Python中的一流对象,因此很少有晦涩的特殊情况。

例如,在Java中,有一些原始类型(int,bool,double,char)不是适当的对象。这就是Java必须将Integer,Boolean,Double和Character作为一流类型引入的原因。对于初学者来说,这可能很难教-尚不清楚为什么原始类型和类必须同时存在。

这也意味着对象的类本身就是对象。这与C ++不同,C ++中的类在运行时并不总是有明显的存在。

对象的类型2type 'int'对象,它具有方法,属性和类型。

>>> type(2)
<class 'int'>

内置类型的类型inttype 'type'对象。这也具有方法和属性。

>>> type(type(2))
<class 'type'>

“When one says “everything is an object” (like in Python), does he indeed mean that “everything is first-class”?”

Yes.

Everything in Python is a proper object. Even things that are “primitive types” in other languages.

You find that an object like 2 actually has a fairly rich and sophisticated interface.

>>> dir(2)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__str__', '__sub__', '__truediv__', '__xor__']

Because everything’s a first-class object in Python, there are relatively few obscure special cases.

In Java, for example, there are primitive types (int, bool, double, char) that aren’t proper objects. That’s why Java has to introduce Integer, Boolean, Double, and Character as first-class types. This can be hard to teach to beginners — it isn’t obvious why both a primitive type and a class have to exist side-by-side.

It also means that an object’s class is — itself — an object. This is different from C++, where the classes don’t always have a distinct existence at run-time.

The type of 2 is the type 'int' object, which has methods, attributes, and a type.

>>> type(2)
<class 'int'>

The type of a built-in type like int is the type 'type' object. This has methods and attributes, also.

>>> type(type(2))
<class 'type'>

回答 2

“头等舱”意味着您可以按常规方式对它们进行操作。在大多数情况下,这仅意味着您可以将这些一等公民作为函数的参数传递,或从函数中返回它们。

对于对象而言,这是不言而喻的,但对于功能甚至类而言,并不总是那么明显:

void f(int n) { return n * 2; }

void g(Action<int> a, int n) { return a(n); }

// Now call g and pass f:

g(f, 10); // = 20

这是C#中的一个示例,其中函数实际上不是一流的对象。因此,以上代码使用一个小的解决方法(即称为的通用委托Action<>)将函数作为参数传递。其他语言(例如Ruby)甚至可以将类和代码块视为普通变量(或在Ruby中为常量)。

“First class” means you can operate on them in the usual manner. Most of the times, this just means you can pass these first-class citizens as arguments to functions, or return them from functions.

This is self-evident for objects but not always so evident for functions, or even classes:

void f(int n) { return n * 2; }

void g(Action<int> a, int n) { return a(n); }

// Now call g and pass f:

g(f, 10); // = 20

This is an example in C# where functions actually aren’t first-class objects. The above code therefore uses a small workaround (namely a generic delegate called Action<>) to pass a function as an argument. Other languages, such as Ruby, allow treating even classes and code blocks as normal variables (or in the case of Ruby, constants).


回答 3

《计算机程序的结构和解释》中的幻灯片2A(1986)中,引用了Christopher Stracey的话

头等公民的权利和特权:

  • 由变量命名。
  • 作为参数传递给过程。
  • 作为过程值返回。
  • 纳入数据结构

From a slide in Structure and Interpretation of Computer Programs, lecture 2A (1986), which in turns quotes Christopher Stracey:

The rights and privileges of first-class citizens:

  • To be named by variables.
  • To be passed as arguments to procedures.
  • To be returned as values of procedures.
  • To be incorporated into data structures

回答 4

国际海事组织,这是用来用自然语言描述事物的那些隐喻之一。该术语本质上用于将功能描述为第一类对象的上下文中。

如果您考虑使用面向对象的语言,我们可以为对象赋予各种功能,例如:继承,类定义,传递给代码的其他部分的能力(方法参数),存储在数据结构中的能力等。与通常不视为对象的实体相同,就像Java脚本中的函数一样,此类实体被视为第一类对象。

头等舱本质上是指不作为第二等舱(具有降级的行为)处理。本质上,嘲讽是完美的还是无法区分的。

IMO this is one of those metaphors used to describe things in a natural language. The term is essentially used in context of describing functions as first class objects.

If you consider a object oriented language, we can impart various features to objects for eg: inheritance, class definition, ability to pass to other sections of code(method arguments), ability to store in a data structure etc. If we can do the same with an entity which is not normally considered as a object, like functions in the case of java script, such entities are considered to be first class objects.

First class essentially here means, not handled as second class (with degraded behaviour). Essentially the mocking is perfect or indistinguishable.


教初学者编程的最佳方法?[关闭]

问题:教初学者编程的最佳方法?[关闭]

原始问题

我目前正在教我的兄弟编程。他是一个初学者,但非常聪明。(他实际上想学习)。我注意到我们的某些会议在次要细节上陷入了停滞,而且我感觉自己组织得不够好。(但是这篇文章的答案很有帮助。

我可以做些什么更好地有效地教他?我可以用逻辑顺序来逐个概念地进行研究吗?我应该避免复杂到以后吗?

我们正在使用的语言是Python,但是欢迎提供任何语言的建议。


如何帮助

如果您有好的,请在答案中添加以下内容:

  • 初学者练习和项目构想
  • 教学初学者的资源
  • 截屏视频/博客文章/免费电子书
  • 打印适合初学者的书籍

通过链接描述资源以便我看看。我希望每个人都知道我肯定已经使用了其中一些想法。您的意见将汇总在此帖子中。


面向初学者的在线资源


推荐给初学者的印刷书籍

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 there a logical order that I can use to run through concept by concept? Are there complexities I should avoid till later?

The language we are working with is Python, but advice in any language is welcome.


How to Help

If you have good ones please add the following in your answer:

  • Beginner Exercises and Project Ideas
  • Resources for teaching beginners
  • Screencasts / blog posts / free e-books
  • Print books that are good for beginners

Please describe the resource with a link to it so I can take a look. I want everyone to know that I have definitely been using some of these ideas. Your submissions will be aggregated in this post.


Online Resources for teaching beginners:


Recommended Print Books for teaching beginners


回答 0

我不得不和几个初学者(从来没有写过任何代码)程序员一起工作,今年秋天我将与高中生一起进行课后工作坊。这是我最接近文档的内容。这项工作仍在进行中,但希望对您有所帮助。

1)FizzBu​​zz。从命令行程序开始。您可以非常快速地编写一些有趣的游戏或工具,并且无需首先学习GUI工具即可非常快速地学习所有语言功能。这些早期的应用程序应该足够简单,您无需使用任何真正的调试工具即可使其运行。

如果没有别的,像FizzBu​​zz这样的项目都是好项目。您的前几个应用程序不必处理数据库,文件系统,配置等。这些概念使大多数人感到困惑,并且当您仅学习语法和基本框架功能时,您实际上并不需要更多的复杂性。

一些项目:

  • 你好,世界!
  • 以我的出生年份为基础,计算我的年龄(仅(现在-然后)没有月份修正)。(简单的数学,输入,输出)
  • 询问方向(上,下,左,右),然后告诉用户他们的命运(掉进洞里,找到蛋糕,等等)。(布尔逻辑)
  • FizzBu​​zz,但每秒计数一次。(循环,计时器和更多逻辑)
  • 根据他们的年龄,有些人真的很喜欢应用程序,该应用程序会在一定间隔内对用户进行随机侮辱。(如果将间隔设为随机,则循环,数组,计时器和随机)

2)简单项目一旦他们掌握了语言功能,就可以开始一个项目(简单,有趣的游戏效果很好。)。您应该尝试使第一个项目能够在6到12个小时内完成。不要花时间来架构它。让他们设计它,即使它很烂。如果失败了,请谈论发生的事情以及失败的原因,然后选择另一个主题并重新开始。

从这里开始介绍工具的调试功能。即使您通过阅读代码可以看到问题,也应该教他们如何使用工具,然后向他们展示如何看到它们。这具有教调试工具和教如何在没有工具的情况下识别错误的双重目的。

一旦项目开始运行,或者如果项目开始运行,则可以使用它来引入重构工具。如果您随后可以使用一些您从未计划过的简单功能来扩展项目,那就很好了。这通常意味着重构和大量的调试,因为很少有人会在第一次编写甚至一半不错的代码。

一些项目:

  • man子手游戏
  • 试验机器人(VexMindstorms是可选的)

3)真实项目开始一个实际项目可能需要一些时间。使用适当的源代码控制,并提出时间表。像实际项目一样运行此项目,如果没有其他必要的经验,则必须使用这些工具。

显然,您需要针对每个人进行调整。我发现的最重要的事情是,即使第一个简单的应用程序也可以应用于人们感兴趣的内容。

一些项目:

  • 俄罗斯方块
  • 基于文本文件的博客引擎
  • 更先进的机器人工作

I’ve had to work with several beginner (never wrote a line of code) programmers, and I’ll be doing an after school workshop with high school students this fall. This is the closest thing I’ve got to documentation. It’s still a work in progress, but I hope it helps.

1) FizzBuzz. Start with command line programs. You can write some fun games, or tools, very quickly, and you learn all of the language features very quickly without having to learn the GUI tools first. These early apps should be simple enough that you won’t need to use any real debugging tools to make them work.

If nothing else things like FizzBuzz are good projects. Your first few apps should not have to deal with DBs, file system, configuration, ect. These are concepts which just confuse most people, and when you’re just learning the syntax and basic framework features you really don’t need more complexity.

Some projects:

  • Hello World!
  • Take the year of my birth, and calculate my age (just (now – then) no month corrections). (simple math, input, output)
  • Ask for a direction(Up, down, left, right), then tell the user their fate (fall in a hole, find a cake, ect). (Boolean logic)
  • FizzBuzz, but count once every second. (Loops, timers, and more logic)
  • Depending on their age some really like an app which calls the users a random insult at some interval. (Loops, arrays, timers, and random if you make the interval random)

2) Simple Project Once they have a good grasp of language features, you can start a project(simple, fun games work good.). You should try to have the first project be able to be completed within 6-12 hours. Don’t spend time to architect it early. Let them design it even if it sucks. If it falls apart, talk about what happened and why it failed, then pick another topic and start again.

This is where you start introducing the debugging capabilities of your tools. Even if you can see the problem by reading the code you should teach them how to use the tools, and then show them how you could see it. That serves the dual purpose of teaching the debugging tools and teaching how to ID errors without tools.

Once, or if, the project gets functional you can use it to introduce refactoring tools. Its good if you can then expand the project with some simple features which you never planned for. This usually means refactoring and significant debugging, since very few people write even half decent code their first time.

Some projects:

  • Hangman game
  • Experimenting with robotics(Vex and Mindstorms are options)

3) Real Project Start a real project which may take some time. Use proper source control, and make a point to have a schedule. Run this project like a real project, if nothing else its good experience having to deal with the tools.

Obviously you need to adjust this for each person. The most important thing I’ve found is to make even the first simple apps apply to what the person is interested in.

Some projects:

  • Tetris
  • Text file based blog engine
  • More advanced robotics work

回答 1

您可以尝试使用Alice。这是一个3D程序,专门用于入门编程课。

新程序员的两个最大障碍通常是:

  • 语法错误
  • 动力(写一些有意义而有趣的东西,而不是做作)

爱丽丝使用拖放界面来构建程序,从而避免了语法错误的可能性。通过Alice,您可以构建3D世界,并具有代码控制(简单)的3D字符和动画,通常比实现链表更有趣。

经验丰富的程序员可能会视爱丽丝为玩具,嘲笑拖放代码行,但研究表明这种方法行之有效。

免责声明:我在Alice上工作。

You could try using Alice. It’s a 3D program designed for use in introductory programming classes.

The two biggest obstacles for new programmers are often:

  • syntax errors
  • motivation (writing something meaningful and fun rather than contrived)

Alice uses a drag and drop interface for constructing programs, avoiding the possibility of syntax errors. Alice lets you construct 3D worlds and have your code control (simple) 3D characters and animation, which is usually a lot more interesting than implementing linked lists.

Experienced programmers may look down at Alice as a toy and scoff at dragging and dropping lines of code, but research shows that this approach works.

Disclaimer: I worked on Alice.


回答 2

我推荐徽标(又名乌龟)来介绍基本概念。它提供了具有即时图形反馈的良好沙箱,并且您可以演示循环,变量,函数,条件等。此页面提供了出色的教程。

徽标后,移至Python或Ruby。我推荐Python,因为它基于ABC,它是为教学编程而发明的。

在教授编程时,我必须赞同EHaskins关于简单项目然后是复杂项目的建议。最好的学习方法是从确定的结果和可衡量的里程碑开始。它使类保持重点,允许学生建立技能,然后再利用这些技能,并给学生一些向朋友炫耀的东西。不要低估了为自己的工作展示某些东西的力量。

从理论上讲,您可以坚持使用Python,因为Python几乎可以完成任何事情。这是教授面向对象的程序设计和(大多数)算法的好工具。您可以像命令行一样在交互式模式下运行Python,以了解其工作方式,或一次运行整个脚本。您可以动态运行解释的脚本,也可以将它们编译为二进制文件。有成千上万的模块可以扩展功能。您可以制作与Windows捆绑在一起的图形计算器一样的图形计算器,也可以制作IRC客户端或其他任何东西。

XKCD更好地描述了Python的功能:

之后,您可以迁移到C#或Java,尽管它们没有提供Python所没有的很多功能。这些方法的好处是它们使用C语言风格的语法,许多(我敢说最多吗?)语言都使用C语言风格的语法。您无需担心内存管理,但是您可以习惯使用语言解释器的更多自由和更少的处理。Python强制使用空格和缩进,这在大多数情况下是很好的,但并非总是如此。使用C#和Java,您可以在保持强类型的同时管理自己的空格。

从那里开始,标准就是C或C ++。这些语言的自由几乎是存在的。现在,您将负责自己的内存管理。没有垃圾收集可以帮助您。在这里,您可以教授真正的高级算法(例如mergesort和quicksort)。在这里,您可以了解为什么“细分错误”是一个诅咒词。在这里,您可以下载Linux内核的源代码并凝视Abyss。首先编写循环缓冲区和用于字符串操作的堆栈。然后继续前进。

I recommend Logo (aka the turtle) to get the basic concepts down. It provides a good sandbox with immediate graphical feedback, and you can demostrate loops, variables, functions, conditionals, etc. This page provides an excellent tutorial.

After Logo, move to Python or Ruby. I recommend Python, as it’s based on ABC, which was invented for the purpose of teaching programming.

When teaching programming, I must second EHaskins’s suggestion of simple projects and then complex projects. The best way to learn is to start with a definite outcome and a measurable milestone. It keeps the lessons focused, allows the student to build skills and then build on those skills, and gives the student something to show off to friends. Don’t underestimate the power of having something to show for one’s work.

Theoretically, you can stick with Python, as Python can do almost anything. It’s a good vehicle to teach object-oriented programming and (most) algorithms. You can run Python in interactive mode like a command line to get a feel for how it works, or run whole scripts at once. You can run your scripts interpreted on the fly, or compile them into binaries. There are thousands of modules to extend the functionality. You can make a graphical calculator like the one bundled with Windows, or you can make an IRC client, or anything else.

XKCD describes Python’s power a little better:

You can move to C# or Java after that, though they don’t offer much that Python doesn’t already have. The benefit of these is that they use C-style syntax, which many (dare I say most?) languages use. You don’t need to worry about memory management yet, but you can get used to having a bit more freedom and less handholding from the language interpreter. Python enforces whitespace and indenting, which is nice most of the time but not always. C# and Java let you manage your own whitespace while remaining strongly-typed.

From there, the standard is C or C++. The freedom in these languages is almost existential. You are now in charge of your own memory management. There is no garbage collection to help you. This is where you teach the really advanced algorithms (like mergesort and quicksort). This is where you learn why “segmentation fault” is a curse word. This is where you download the source code of the Linux kernel and gaze into the Abyss. Start by writing a circular buffer and a stack for string manipulation. Then work your way up.


回答 3

麻省理工学院的《使用Python编程的温和介绍》是一门很好的python类。它全部在线免费提供,您不必一定要成为MIT超级学生才能了解它。

编辑[ 贾斯汀标准版 ]

本类使用这本免费的在线书籍:如何像计算机科学家一样思考
我绝对会发现它非常有用。

A good python course is MIT’s A Gentle Introduction to Programming Using Python. It’s all free online, and you don’t have to be an MIT uberstudent to understand it.

Edit [Justin Standard]

This course uses this free online book: How To Think Like a Computer Scientist
I’m definitely finding it quite useful.


回答 4

Python软件包VPython-普通凡人的3D编程(视频教程)。

代码示例:

from visual import *

floor = box (pos=(0,0,0), length=4, height=0.5, width=4, color=color.blue)
ball = sphere (pos=(0,4,0), radius=1, color=color.red)
ball.velocity = vector(0,-1,0)
dt = 0.01

while 1:
    rate (100)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y < ball.radius:
        ball.velocity.y = -ball.velocity.y
    else:
        ball.velocity.y = ball.velocity.y - 9.8*dt

VPython弹跳球http://vpython.org/bounce.gif

Python package VPython — 3D Programming for Ordinary Mortal (video tutorial).

Code example:

from visual import *

floor = box (pos=(0,0,0), length=4, height=0.5, width=4, color=color.blue)
ball = sphere (pos=(0,4,0), radius=1, color=color.red)
ball.velocity = vector(0,-1,0)
dt = 0.01

while 1:
    rate (100)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y < ball.radius:
        ball.velocity.y = -ball.velocity.y
    else:
        ball.velocity.y = ball.velocity.y - 9.8*dt

VPython bouncing ball http://vpython.org/bounce.gif


回答 5

从Python中的Turtle图形开始。

我将使用Python随附的乌龟图形。它是直观,简单的,您可以在不深入语法的情况下使用此环境引入许多编程概念,例如迭代和过程调用。考虑以下python中的交互式会话:

>>> from turtle import *
>>> setup()
>>> title("turtle test")
>>> clear()
>>>
>>> #DRAW A SQUARE
>>> down()        #pen down
>>> forward(50)   #move forward 50 units
>>> right(90)     #turn right 90 degrees
>>> forward(50)
>>> right(90)
>>> forward(50)
>>> right(90)
>>> forward(50)
>>>
>>> #INTRODUCE ITERATION TO SIMPLIFY SQUARE CODE
>>> clear()
>>> for i in range(4):
        forward(50)
        right(90)
>>>
>>> #INTRODUCE PROCEDURES   
>>> def square(length):
        down()
        for i in range(4):
            forward(length)
            right(90)
>>>
>>> #HAVE STUDENTS PREDICT WHAT THIS WILL DRAW
>>> for i in range(50):
        up()
        left(90)
        forward(25)
        square(i)
>>>
>>> #NOW HAVE THE STUDENTS WRITE CODE TO DRAW
>>> #A SQUARE 'TUNNEL' (I.E. CONCENTRIC SQUARES
>>> #GETTING SMALLER AND SMALLER).
>>>
>>> #AFTER THAT, MAKE THE TUNNEL ROTATE BY HAVING
>>> #EACH SUCCESSIVE SQUARE TILTED

在尝试完成最后两个任务时,他们将进行很多失败的尝试,但是这些失败在视觉上将是有趣的,并且他们将很快学会,因为他们试图弄清为什么它没有达到他们的期望。

Begin with Turtle graphics in Python.

I would use the turtle graphics which comes standard with Python. It is visual, simple and you could use this environment to introduce many programming concepts like iteration and procedure calls before getting too far into syntax. Consider the following interactive session in python:

>>> from turtle import *
>>> setup()
>>> title("turtle test")
>>> clear()
>>>
>>> #DRAW A SQUARE
>>> down()        #pen down
>>> forward(50)   #move forward 50 units
>>> right(90)     #turn right 90 degrees
>>> forward(50)
>>> right(90)
>>> forward(50)
>>> right(90)
>>> forward(50)
>>>
>>> #INTRODUCE ITERATION TO SIMPLIFY SQUARE CODE
>>> clear()
>>> for i in range(4):
        forward(50)
        right(90)
>>>
>>> #INTRODUCE PROCEDURES   
>>> def square(length):
        down()
        for i in range(4):
            forward(length)
            right(90)
>>>
>>> #HAVE STUDENTS PREDICT WHAT THIS WILL DRAW
>>> for i in range(50):
        up()
        left(90)
        forward(25)
        square(i)
>>>
>>> #NOW HAVE THE STUDENTS WRITE CODE TO DRAW
>>> #A SQUARE 'TUNNEL' (I.E. CONCENTRIC SQUARES
>>> #GETTING SMALLER AND SMALLER).
>>>
>>> #AFTER THAT, MAKE THE TUNNEL ROTATE BY HAVING
>>> #EACH SUCCESSIVE SQUARE TILTED

In trying to accomplish the last two assignments, they will have many failed attempts, but the failures will be visually interesting and they’ll learn quickly as they try to figure out why it didn’t draw what they expected.


回答 6

关键是有问题的人需要解决一些问题。如果您没有要编写的程序(以及一些明智且定义明确的程序,而不是“我要编写下一本Quake!”),则您将无法学习编程,因为您没有动力去激励自己。 。我的意思是,您可以读一本书,并对语言的语法和语义有一个大概的了解,但是除非您拥有要编写的程序,否则您将永远无法理解。

如果存在这种推动力,那么其他所有内容都只是次要的细节。

The key thing is that the person in question needs to have some problem that they want solving. If you don’t have a program that you want to write (and something sensible and well-defined, not “I want to write the next Quake!”) then you can’t learn to program, because you have nothing to motivate you. I mean, you could read a book and have a rough understanding of a language’s syntax and semantics, but until you have a program that you want written you’ll never grasp the nettle.

If that impetus exists then everything else is just minor details.


回答 7

我不知道这里是否有人提到过,但是您可能想看看Zed Shaw的学习Python的艰难方法》

希望这可以帮助

I don’t know if anyone has mentioned this here, yet, but You might want to check out Zed Shaw‘s Learn Python the Hard Way

Hope this Helps


回答 8

http://tryruby.hobix.com/“>尝试使用Ruby(在您的浏览器中)

http://tryruby.hobix.com/”>Try Ruby (In Your Browser)


回答 9


回答 10

这是一本很棒的书,我的小兄弟曾经学习过:

http://pine.fm/LearnToProgram/

当然,最重要的是在读完本书后立即开始一个真正有用的某种程序。

This is a fantastic book which my little brothers used to learn:

http://pine.fm/LearnToProgram/

Of course, the most important thing is to start on a real, useful program of some kind IMMEDIATELY after finishing the book.


回答 11

如果他有兴趣,次要细节不是很好的部分吗?使用python,您已经将它的GUI切断了,这样混乱就消失了。为什么不选择一个项目,一个游戏之类的东西并实现它。经典的hi-lo数字猜谜游戏可以在命令行中以20-30行代码(具体取决于语言)简单地实现,并为您提供变量,条件,循环和用户输入。

If he’s interested, aren’t the minor details the good parts? Using python, you’ve already cut the GUI off of it so that confusion is gone. Why not pick a project, a game or something, and implement it. The classic hi-lo number guessing game can be simply implemented from the command line in 20-30 lines of code (depending on language of course) and gives you variables, conditions, loops, and user input.


回答 12

我只是让他写大量的代码。让他开车去做你们所做的一切,并且随时可以回答问题。

信不信由你,经过几个月的编写大量笨拙的代码之后,他将开始明白这个想法,并开始编写更好的程序。到那时,您可以陷入细节(内存等)的泥潭,还可以讨论一般的设计原则。

我听说,伟大的艺术家与平庸的艺术家之间的区别是,每一次练习,无论大小,他们都会有所进步。让您的兄弟练习一下,他每次坐在键盘上都会使他进步。

编辑:[贾斯汀标准]

埃斯特万,这让我想起最近的编码恐怖后,我认为你是对的。但是我认为仍然值得寻找方法来指导他的实践。没问题,我希望他编写尽可能多的代码。这就是我要样例项目的原因之一。

I’d just let him write tons of code. Let him drive in everything you guys do, and just be available to answer questions.

Believe it or not, after a few months of writings tons of crappy code, he’ll start to get the idea and start writing better programs. At that point, you can get bogged down in details (memory, etc), and also talk about general design principles.

I’ve heard that what separates the great artists from the mediocre ones, is that every time they practice, they improve on something, no matter how small. Let your brother practice, and he’ll improve every time he sits down at the keyboard.

Edit: [Justin Standard]

Esteban, this reminds me of a recent coding horror post, and I do think you are right. But I think its still worthwhile to find methods to guide his practice. No question, I want him writing as much code as he knows how to do. Thats one reason I’m asking for sample projects.


回答 13

首先,像其他所有人一样开始工作:使用Hello World程序。这很简单,并且给了他们基本的程序布局感觉。尝试回想起您第一次编程的时间以及其中一些概念的难易程度-从简单开始。

在Hello World之后,继续创建一些基本变量,算术,然后创建布尔逻辑和if / else语句。如果您有一本旧的编程教科书,请查看一些早期示例,并让他复习这些示例。只是不要尝试一次过多地介绍所有内容,否则将会使您感到困惑和困惑。

First of all, start out like everyone else does: with a Hello World program. It’s simple, and it gives them a basic feel for the layout of a program. Try and remember back to when you were first programming, and how difficult some of the concepts were – start simple.

After Hello World, move on to creating some basic variables, arithmetic, then onto boolean logic and if/else statements. If you’ve got one of your old programming textbooks, check out some of the early examples and have him run through those. Just don’t try to introduce too much all at once, or it will be overwhelming and confusing.


回答 14

在教您的兄弟编程时,您应该谨记的一点是,不要太依赖您。通常,当我发现自己在帮助他人时,他们会开始将我视为所有问题的答案书,而不是尝试寻找答案,他们只是问我。通常最好的老师是实验,每当您的兄弟有一个问题,例如“如果我在字符串中加2,会发生什么?”。您应该告诉他尝试一下,亲自看看。我还注意到,当我无法将概念传达给某人时,这有助于查看一些示例代码,在这里我们可以分别查看每个段并逐个进行解释。附带说明一下,刚接触编程的人经常会遇到面向对象编程的想法,

Something you should be very mindful of while teaching your brother to program is for him not to rely too heavily on you. Often when I find myself helping others they will begin to think of me as answer book to all of their questions and instead of experimenting to find an answer they simply ask me. Often the best teacher is experimentation and every time your brother has a question like “What will happen if I add 2 to a string?” you should tell him to try it out and see for himself. Also I have noticed that when I cannot get a concept through to someone, it helps to see some sample code where we can look at each segment individually and explain it piece by piece. As a side note people new to programming often have trouble with the idea of object oriented programming, they will say they understand it when you teach it to them but will not get a clear concept of it until actually implementing it.


回答 15

我曾经教编程,与您想学习的大多数学生相比,您的兄弟有一个主要优势:)

如果您决定和C一起去,那么一个朋友的站点会提供一些程序,这些程序可以使用较早的一代记住的基本键入程序。他们中较复杂的人使用ncurses,这在某种程度上抵消了它们作为教学辅助工具的使用,但是其中一些是很小的小东西,您无需学习即可学习负载。

我个人认为Python和Ruby将成为出色的第一语言。

编辑: 一夜之间出现的初学者编程作业列表可能正是您想要的。

I used to teach programming and your brother has one main advantage over most of my students he wants to learn :)

If you decide to go with C a friend has a site that has the sort of programs those of use from older generations remember as basic type-ins. The more complex of them use ncurses which sort of negates their use as a teaching aid somewhat but some of them are tiny little things and you can learn loads without being taught to.

Personally I think Python and Ruby would make great first languages.

EDIT: list of beginner programming assignments appeared overnight might be just what you are looking for.


回答 16

这确实取决于您兄弟的学习风格。许多人会变得肮脏,只有动手才能学得更快,随着他们的进步和积累知识,使概念和全局变得清晰。

我,我更喜欢从大局开始,深入研究细节。我想知道的第一件事是它们如何融合在一起,然后是所有面向对象的蠢事,然后是类和实例等等。在学习语法之前,我喜欢了解基本概念和一些理论。我有一个优势,因为20年前我用BASIC编写了一些游戏,但此后没什么。

在实际编写代码之前,通过总体任务说明,计划和/或流程图,然后详细说明一些伪代码(倾向于最终使用的语法)来掩盖生产过程,这可能是有用的。

这里的黄金法则是保持学生的学习风格。

It really depends on your brother’s learning style. Many people learn faster by getting their hands dirty & just getting into it, crystallising the concepts and the big picture as they progress and build their knowledge.

Me, I prefer to start with the big picture and drill down into the nitty-gritty. The first thing I wanted to know was how it all fits together then all that Object-oriented gobbledygook, then about classes & instances and so-on. I like to know the underlying concepts and a bit of theory before I learn the syntax. I had a bit of an advantage because I wrote some games in BASIC 20 years ago but nothing much since.

Perhaps it is useful to shadow a production process by starting with an overall mission statement, then a plan and/or flowchart, then elaborate into some pseudo code (leaning towards the syntax you will ultimately use) before actually writing the code.

The golden rule here is to suss out your student’s leaning style.


回答 17

如果您的兄弟可以使用iTunes,则可以下载新南威尔士大学的Richard Buckland开设的计算机科学入门类的视频讲座。他是一位引人入胜的讲师,涵盖计算和C语言的基础知识。如果没有其他要求,请告诉您的兄弟在后台播放视频,某些概念可能会因渗透而陷入。:)

COMP1917高等计算-2008会话1 http://deimos3.apple.com/WebObjects/Core.woa/Browse/unsw.edu.au.1504975442.01504975444

如果链接不起作用,请使用以下路径:

主页-> iTunes U->工程-> COMP1917高等计算-2008第1节

If your brother has access to iTunes, he can download video lectures of an introductory computer science course given by Richard Buckland at the University of New South Wales. He’s an engaging instructor and covers fundamentals of computing and the C language. If nothing else, tell your brother to play the vids in the background and some concepts might sink in through osmosis. :)

COMP1917 Higher Computing – 2008 Session 1 http://deimos3.apple.com/WebObjects/Core.woa/Browse/unsw.edu.au.1504975442.01504975444

If the link doesn’t work, here’s a path:

Home -> iTunes U –> Engineering –> COMP1917 Higher Computing – 2008 Session 1


回答 18

有一本非常适合学习python的Wikibook

我不知道其他语言的Wikibook怎么样,但是我个人从Wikibook中学习了python,就像2007年2月一样

ps-如果您不熟悉Wikibook,则基本上是书籍创作的Wikipedia版本。这很难描述,但是如果您查看那里的几本书,就会知道它是如何工作的

there’s a wikibook that is pretty good for learning python.

I don’t know how the wikibooks are for other languages, but I personally learned python from the wikibook as it was in Feb 2007

ps – if you’re unfamiliar with wikibooks, it’s basically the wikipedia version of book authoring. it’s sort of hard to describe, but if you check out a few of the books on there you’ll see how it works


回答 19


回答 20

我认为Python是个好主意。我会给他一些基本任务,让他自己做,并告诉他,他遇到的任何死胡同都可以通过去Google来解决。至少对我来说,独自解决一个问题总是比别人告诉我解决方案更好。

一些可能的项目(无特定顺序):

  • 硬币翻转模拟器。让用户输入所需的硬币投掷次数。执行它并显示结果以及正面或反面的百分比。

  • 使用带有菜单的温度转换器,该菜单需要用户输入以选择用户想要执行的转换类型。选择转换并完成转换后,应返回主菜单。

    这是具有相同概念的扩展转换器的示例:http : //pastebin.org/6541

  • 编写一个使用数字输入并显示要转换为的字母等级的程序。最终将根据if和elif语句评估输入,以找到适合的位置。

  • 进行一次简单的测验,以进行多项选择或填写空白问题。最后,它将显示用户的操作方式。他可以选择任何想要的问题。

  • 输入一些(可能很大)的便士,并将其转换成更大的面额。例如,149便士= 1美元,1个季度,2个角钱和4个便士。

  • 创建一个简单的列表管理器。能够添加/删除列表以及在这些列表中添加/删除条目。这是圣诞节清单管理器的示例:http : //pastebin.org/6543

  • 创建一个将生成的程序,然后测试输入的数字是否形成一个魔方(带有2D数组)。这是一些示例代码,但实际上应该在每个步骤中打印出正方形,以显示用户在正方形上的位置:http : //pastebin.org/6544

我还建议使用xTurtle或其他图形模块做一些事情,以使事情变得混乱,并避免他变得无聊。当然,这是非常实际的编程,而不是很多人真正会使用python的脚本,但是我给出的示例几乎直接取自于我通过python学习时,并且对我来说非常有用。祝好运!

I think Python is a great idea. I would give him a few basic assignments to do on his own and tell him that any dead ends he hits can probably be resolved by a trip to google. For me, at least, solving a problem on my own always made it stick better than someone telling me the solution.

Some possible projects (in no particular order):

  • Coin flip simulator. Let the user input a desired number of trials for the coin flipping. Execute it and display the results along with the percentage for heads or tails.

  • Make a temperature converter with a menu that takes user input to choose which kind of conversion the user wants to do. After choosing the conversion and doing it, it should return to the main menu.

    Here’s an example of an extended converter with the same idea: http://pastebin.org/6541

  • Make a program that takes a numeric input and displays the letter grade it would translate to. It’ll end up evaluating the input against if and elif statements to find where it fits.

  • Make a simple quiz that goes through several multiple choice or fill in the blank questions. At the end it will display how the user did. He can pick any questions he wants.

  • Take an input of some (presumably large) number of pennies and convert it into bigger denominations. For example, 149 pennies = 1 dollar, 1 quarter, 2 dimes, and 4 pennies.

  • Create a simple list manager. Be able to add/delete lists and add/delete entries in those lists. Here’s an example of a christmas list manager: http://pastebin.org/6543

  • Create a program that will build and then test whether entered numbers form a magic square (with a 2D array). Here’s some sample code, but it should really print out the square at each step in order to show where the user is in terms of buliding the square: http://pastebin.org/6544

I would also suggest doing some stuff with xTurtle or another graphics module to mix things up and keep him from getting boring. Of course, this is very much practice programming and not the scripting that a lot of people would really be using python for, but the examples I gave are pretty much directly taken from when I was learning via python and it worked out great for me. Good luck!


回答 21

只是取笑!

令人惊讶的是,如果您尝试Kojo, Scala可能是最简单的

Just make it fun !

Amazingly Scala might be the easiest if you try Kojo


回答 22

如果您的兄弟喜欢拼图,我建议您使用Python Challenge。在一对一的教程中,我不会将其用作正式的教学工具,但是当您不在一起挑战自己并享受一些乐趣时,他可以做这件事。

If your brother likes puzzles, I would recommend Python Challenge. I wouldn’t use it as a formal teaching tool in a 1 on 1 tutorial, but it’s something he can do when you’re not together to challenge himself and have some fun.


回答 23


回答 24

在浏览了几本免费的电子书之后,我发现最适合学习编程的书是O’Reily Press出版的Head First Programming。它使用Python作为语言,并从一开始就为您提供要处理的程序。他们比“ Hello World”更有趣。我花在上面的钱很值得,而且由于花了一点时间,您也许可以在Ebay或Amazon上找到更便宜的二手书。

After going through a few free e-books, I found the best book for learning to program was Head First Programming published by O’Reily Press. It uses Python as the language and gives you programs to work on from the very start. They are all more interesting that ‘Hello World’. It’s well worth the money I spent on it, and since it’s been out for a bit you may be able to find a cheaper used copy on Ebay or Amazon.


回答 25

如果您想教编程的基础知识,而又不特定于语言,则有一个名为 在MIT中创建 Scratch。它旨在帮助人们发展编程技能。用户创建Scratch项目时,他们会学习创建条件,循环等。还有一个Scratch项目社区,可以下载项目的表单-这样,您就可以浏览其他人的程序并查看其构建方式。

If you want to teach the basics of programming, without being language specific, there is an application called Scratch that was created in MIT. It’s designed to help people develop programming skills. As users create Scratch projects, they learn to create conditions, loops, etc. There is a also a community of scratch projects, form which projects can be downloaded – that way you can explore other people’s programs and see how they were built.


回答 26

我认为,一旦他掌握了基础知识(变量,循环等),就应该尝试帮助他找到他感兴趣的特定事物,并帮助他了解实现它的必要性。我知道,如果我感兴趣的话,我会更倾向于做某事。另外,确保让他为一些棘手的问题而奋斗,没有什么比自己解决这个问题更令人满意的了。

I think that once he has the basics (variables, loops, etc) down you should try to help him find something specific that he is interested in and help him learn the necessities to make it happen. I know that I am much more inclined and motivated to do something if it’s of interest to me. Also, make sure to let him struggle though some of the tougher problems, nothing is more satisfying than the moment you figure it out on your own.


回答 27

通过学习如何使用流程图和PDL以与语言无关的方式解决问题,使我受到了教育(程序设计语言)。几周后,我学会了将编写的PDL转换为语言。我很高兴能以这种方式学习,因为我花了大部分时间来编程,解决了各种问题,而又不受语言的束缚。我使用的语言一直是实现细节,而不是设计的一部分。

必须通过将问题分解为基本步骤来解决问题是一项关键技能。我认为这是将那些可以编程的人与那些不能编程的人分开的事情之一。

就如何处理语言概念的顺序而言,我认为最简单的方法是决定要考虑一个项目并根据需要解决这些概念。这样,您就可以根据需要将它们应用到您感兴趣的事情上。学习语言时,最好记住几个简单的项目,而一些项目要具有渐进的复杂性。确定这些内容将帮助您确定所需的概念及其顺序。

I was taught by learning how to solve problems in a language agnostic way using flowcharts and PDL (Program Design Language). After a couple weeks of that, I learned to convert the PDL I had written to a language. I am glad I learned that way because I have spent the majority of my years programming, solving problems without being tied to a language. What language I use has always been an implementation detail and not part of the design.

Having to solve the problem by breaking it down into it’s basic steps is a key skill. I think it is one of the things that separates those that can program from those that can’t.

As far as how you tackle the order of concepts of a language I believe the easiest way is to decide that is to have a project in mind and tackle the concepts as they are needed. This lets you apply them as they are needed on something that you are interested in doing. When learning a language it is good to have several simple projects in mind and a few with progressive complexity. Deciding on those will help you map out the concepts that are needed and their order.


回答 28

我还建议您观看一些截屏视频-它们通常是在特定技术而非语言的背景下创建的,尽管如果显示Python代码,则可以:)。关键是-它们是由一些优秀的程序员创建的,看着优秀的程序员编程是一件好事。您和您的兄弟也可以进行一些对等编程,这可能是一个更好的主意。只是别忘了解释为什么您要以这种方式而不是那样的方式做事。我认为学习编程的最好方法是从好的例子中去,甚至不要去看坏的例子。

I would recommend also watching some screencasts – they are generally created in context of a specific technology not a language, though if there’s Python code displayed, that’ll do :). The point is – they’re created by some good programmers and watching how good programmers program is a good thing. You and your brother could do some peer programming as well, that might be an even better idea. Just don’t forget to explain WHY you do something this way and not that way. I think the best way to learn programming is from good examples and try not to even see the bad ones.


回答 29

罗伯特·雷德(Robert Read)撰写了一份有用的指南,《如何成为程序员》,其中涵盖了初学者会有所帮助的广泛编程问题。

Robert Read wrote a useful guide, How to be a Programmer, which covers a wide area of programming issues that a beginner would find helpful.