Introduction to Computational Thinking Blockly Introduction to Python
Introduction to Computational Thinking Blockly Introduction to Python (C) Dennis Kafura 2016 1
CT@VT Advantages of Blockly n n Blockly code (nearly) always does something - even when it is not exactly what we want. The drag-and-drop interface means that we do not need to memorize the syntax of statements. Blockly prevents some kinds of mistakes by allowing only meaningful combination of blocks. The visual layout of an algorithms can help understand its structure (C) Dennis Kafura 2016 Slide 2
CT@VT Advantages of Python n n Write programs faster! Use the full capability of available programming facilities However, we have to learn its syntax. Fortunately, Block. Py shows the relationship between Blockly and Python! (C) Dennis Kafura 2016 Slide 3
CT@VT Blockly in Python click the tab (C) Dennis Kafura 2016 Slide 4
CT@VT What’s new/different here? import weather set temperatures = weather. get_forecasts(“Blacksburg, VA”) set cool = 0 set warm = 0 for each item temp in list temperatures: if temp < 70: set cool = cool + 1 elif temp >= 85: set warm = warm + 1 print(cool) print(warm) (C) Dennis Kafura 2016 Slide 5
CT@VT Indentation (C) Dennis Kafura 2016 Slide 6
CT@VT Indentation n Whitespace in front of lines of code. Used to indicate blocks that belong inside another block of code. The amount of space matters. t’s a h W (C) Dennis Kafura 2016 g wron ode c s i h th ? wit Slide 7
CT@VT Colons “: ” n n Colons at the end of for and if blocks indicate the start of indentation. They are mandatory for these two blocks. None of the other lines have colons at the end. import weather temperatures = weather. get_forecasts(“Blacksburg, VA”) cool = 0 warm = 0 for temp in list temperatures: if temp < 70: cool = cool + 1 elif temp >= 85: warm = warm + 1 print(cool) print(warm) (C) Dennis Kafura 2016 Slide 8
CT@VT Import n n Lots of the features that we need are included automatically in Python. Some need to be explicitly imported. The import line allows us access to useful additions. So far, our data and plot blocks have been imported. Go check your past code! Can you tell which blocks depend on the import line? (C) Dennis Kafura 2016 Slide 9
CT@VT Modules and functions module function (C) Dennis Kafura 2016 parameter Slide 10
CT@VT For today n n n Work on classwork problems in your cohort This is our first step into Python so don’t worry if it is awkward or difficult The concepts are the same – only the form is changing! (C) Dennis Kafura 2016 Slide 11
- Slides: 11