Skip to content

Instantly share code, notes, and snippets.

@alexiasa
Last active June 2, 2018 03:24
Show Gist options
  • Save alexiasa/555c2c521abe086757d00a8138cc320c to your computer and use it in GitHub Desktop.
Save alexiasa/555c2c521abe086757d00a8138cc320c to your computer and use it in GitHub Desktop.
Python and Smalltalk
Notes about Smalltalk. Smalltalk influenced essentially all of the subsequent object-oriented languages. A few of the modern Smalltalk frameworks include Amber and Seaside which are geared toward web development.
"this is a comment in Smalltalk. we are going to declare and assign variables."
| x y | "declare the variable x and y"
x := 1 "assign x a value of 1"
y:= $q "assign y the character 'q'"
# this is a comment in Python. we are going to declare and assign variables.
x = 1 # declare and assign x a value of 1
y = 'q' # declare and assign y a value of 'q'
********
"printing to the screen in Smalltalk"
Transcript show: 'Hello World'.
# printing to the screen in Python
print('Hello World')
********
"conditional statements in Smalltalk"
x > 1
ifTrue: [Transcript show: 'x is greater than 1'.]
ifFalse: [Transcript show: 'x is less than or equal to 1'.]
# conditional statements in Python
if x > 1:
print('x is greater than 1')
else:
print('x is less than or equal to 1')
********
"looping in Smalltalk"
| x |
x := 10
x timesRepeat: [Transcript show: 'this is an iteration'.]
1 to: x do: [Transcript show: 'this is an iteration'.]
[x > 5] whileTrue: [x := x + 1. Transcript show: 'this is an iteration'.]
# looping in Python
x = 10
# note: off the top of my head, I don't recall if Python has an equivalent to timesRepeat method in the looping sense.
while x != 0:
print('this is an iteration')
x--
********
# arrays and collections in Smalltalk
| myArray x |
myArray := #(1 2 3 $a $b $c).
x := myArray at: 4
"array indices in Smalltalk begin at 1. the above statement yields $a
# arrays and collections in Python
myList = [1, 2, 3, 'a', 'b', 'c']
x = myList[3]
# list indices in Python begin at 0. the above statement yields 'a'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment