This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
programming:python:start [2018/12/06 01:07] dwheele [Loops] |
programming:python:start [2026/02/07 19:28] (current) dwheele [Class] |
||
|---|---|---|---|
| Line 12: | Line 12: | ||
| ===== Data types ===== | ===== Data types ===== | ||
| + | List | ||
| + | |||
| + | <code python> | ||
| + | a = [1, | ||
| + | print (a[0]) | ||
| + | 1 | ||
| + | print (a[1:2]) | ||
| + | 4, 5 | ||
| + | </ | ||
| + | |||
| + | <code python> | ||
| + | a = ' | ||
| + | print (a[4:]) | ||
| + | ' | ||
| + | </ | ||
| + | |||
| + | Dictionary (like a hash map) | ||
| + | |||
| + | <code python> | ||
| + | a = {} | ||
| + | |||
| + | a[' | ||
| + | a[' | ||
| + | print (a) | ||
| + | | ||
| + | </ | ||
| ===== Functions ===== | ===== Functions ===== | ||
| - | def myfunction(aNumber=1, | + | <code python> |
| + | def myfunction(aNumber=1, | ||
| + | result = '' | ||
| + | for i in range(aNumber): | ||
| + | | ||
| + | return result | ||
| + | </ | ||
| ===== Syntax ===== | ===== Syntax ===== | ||
| - | Indents matter. | + | Indents matter, usually 4 spaces. |
| ===== Loops ===== | ===== Loops ===== | ||
| Line 38: | Line 70: | ||
| else: | else: | ||
| print ' | print ' | ||
| + | </ | ||
| + | |||
| + | ===== Class ===== | ||
| + | |||
| + | <code python> | ||
| + | class Boy() | ||
| + | def setName(self, | ||
| + | | ||
| + | def getName(self): | ||
| + | | ||
| + | </ | ||
| + | |||
| + | self is like *this* in Java. If you don't provide " | ||
| + | |||
| + | <code python> | ||
| + | b = Boy() | ||
| + | b.setName(" | ||
| + | </ | ||
| + | |||
| + | ===== Display ===== | ||
| + | |||
| + | (Don't forget the parentheses.) | ||
| + | |||
| + | <code python> | ||
| + | print (" | ||
| </ | </ | ||