I don't begin teaching someone programming by explaining objects and types.
I generally start by introducing three fundamental concepts: variables, conditionals, and loops. And I keep it simple to begin with:
a = 1
print(a) # 1
a = 2
print(a) # can you guess what it will print?
Then I add conditionals:
if a == 2:
print("it is two!")
else:
print("it is not two...")
And I only really cover 'for' at first:
groceries = ["ham", "cheese", "eggs"]
for item in groceries:
print(item)
Along the way I explain some of the primitive data types: string, integer, float; and containers such as list and dictionary. And that is usually enough to get started with simple tasks. I tend to elide what functions are and just call them "commands" until later on so that I can demonstrate why looping is so cool:
import turtle
turtle.setup()
for amount in range(100):
turtle.forward(amount)
turtle.left(75)
And that usually drives home the point: grouping commands together to repeat them as many times as we wish using loops; variables hold data; and conditionals let us do different things depending on whether something is True or False.
I haven't had much trouble with this approach for years. I don't even get to explain iterables to newbies most of the time! Once in a while someone tries something like:
a = "foo"
a + 1
And they get TypeError or they pass in an object of the wrong type to a function and get ValueError. Early on this usually isn't a problem because some things just don't make sense like adding numbers and strings. However it can get confusing when learning how to look up functions and use them because we can only informally document what kinds of things a given function will take in its signature... it's an advantage and disadvantage of the duck-typing philosophy. It's a wart but one that I haven't really encountered with anyone I've taught until they're pretty far along and able to help themselves.
I don't begin teaching someone programming by explaining objects and types.
I generally start by introducing three fundamental concepts: variables, conditionals, and loops. And I keep it simple to begin with:
Then I add conditionals: And I only really cover 'for' at first: Along the way I explain some of the primitive data types: string, integer, float; and containers such as list and dictionary. And that is usually enough to get started with simple tasks. I tend to elide what functions are and just call them "commands" until later on so that I can demonstrate why looping is so cool: And that usually drives home the point: grouping commands together to repeat them as many times as we wish using loops; variables hold data; and conditionals let us do different things depending on whether something is True or False.I haven't had much trouble with this approach for years. I don't even get to explain iterables to newbies most of the time! Once in a while someone tries something like:
And they get TypeError or they pass in an object of the wrong type to a function and get ValueError. Early on this usually isn't a problem because some things just don't make sense like adding numbers and strings. However it can get confusing when learning how to look up functions and use them because we can only informally document what kinds of things a given function will take in its signature... it's an advantage and disadvantage of the duck-typing philosophy. It's a wart but one that I haven't really encountered with anyone I've taught until they're pretty far along and able to help themselves.