Mod 4 Python - Upd
Mod 4 Python - Upd
Two ways to do it
1. Interactive: execute lines typed interactively in a Python console
2. Batch: execute an entire Python program
• Interactive execution requires a Python shell
• Start IDLE, shell is default
• In terminal, type “python3” ELEC3442 Embedded Systems 5
EXECUTING PROGRAMS FROM IDLE
• Start IDLE
• File > New window to create
a new text editor window
• Type in code
• Select Run > Run Module
• Python shell will be opened
to execute the code
http://www.skulpt.org/
a a
b a
a b
b
ELEC3442 Embedded Systems 8
RANGE
• append(), remove(), reverse(), and sort() do not return valuesELEC3442 Embedded Systems 25
DICTIONARY
• Similar to array
• But work with keys and values instead of indices
• Each value stored in a dictionary is accessed by a key
>>>studentID = { >>>studentID = {
[“Peter”]: 112233, studentID[“Peter”] = 112233
[“Mary”]: 112256, studentID[“Mary”] = 112256
[“John”]: 115678 studentID[“John”] = 115678
} }
• Remove a value
>>>studentID.pop(“Peter”) >>>del studentID[“Peter”]
ELEC3442 Embedded Systems 26
CONTROL FLOW
If temp>25:
print(‘It is hot!’)
print(‘Goodbye.’)
ELEC3442 Embedded Systems 27
IF-ELSE STATEMENT
if <condition>:
<indented code block 1>
else:
<indented code block 2>
<non-indented statement>
if temp>25:
print(‘It is hot!’)
else:
print(‘It is not hot!’)
print(‘Goodbye.’)
ELEC3442 Embedded Systems 28
FOR LOOP
• Executes a block of code for every element in a sequence
>>> i=0
>>> while i<3:
print(i)
i=i+1
0
1
2
>>>