class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture 5:] .title[Organization and control Flow] ] .footer[[/lecture/5/](/lecture/5/)] --- # Python scripts ```python # Copyright (c) 2020 Jonathan Anderson # Permission is granted to copy and modify this code for any purpose. # # This is an example of a simple Python script for questing. # First, gather some basic information from the user. name = input('What is your name? ') quest = input('What is your quest? ') v = input('What is the average airspeed of an unladen swallow? ') # Greet the brave dame or knight. print(f'Greetings, brave {name}!') # Convert speed into a floating-point number and calculate quest time. v = float(v) print(f'Your quest for {quest} will be aided when the swallow flies 792 m.') print(f'This should take {792 / v} s.') ``` ??? What can we see in this file (which is available as [quest.py](quest.py))? * comments * copyright * type conversion * variables --- # What we just saw -- ### Comments \# Helpful descriptive text -- for **people**, not the computer! -- \# Everything from `#` to the end of the line -- ### Copyright -- * writing code is a _creative act_ -- * more like an English assignment than Math! --- # Organizing your writing ### How would you organize: -- * a text? -- * a letter? -- * an essay? -- * a report? -- * a book? --- # Organizing Python -- | Writing text | Writing code | |--------------|--------------| -- | phrases | expressions | -- | sentences | statements | ??? So far we've only seen two kinds of statements: the **expression statement** and the **assignment statement**. As we progress through the course, however, we'll learn about more. -- | paragraphs | functions | ??? We've used functions in this course, but we haven't written our own yet. That will change. -- | chapters and parts | modules | ??? We've also used modules: `engi1020` and `math` are examples. Towards the end of the course, we'll also see how to _create_ modules. --- # Aside: valid names ### What _is_ a valid name? -- .center[ What sorts of names have we seen thus far? ] -- .floatleft[ ### Python 2: * letters, numbers and underscores * don't start with a number ] -- ### Python 3: Adds **some** Unicode characters (full details available at [PEP 3131](https://www.python.org/dev/peps/pep-3131)) ??? ### Examples of valid Python 3 identifiers: **Chinese:** 你好 (Mandarin ni hao, Cantonese lai hao) **Hindi:** नमस्ते (namaste) **Bengali:** হ্যালো (Hyālō) **Urdu:** ہیلو (helo) **Arabic:** مرحبا (mar haban) **Hebrew:** שלום (shalom) ### Invalid identifiers: --- # You are here <img align="right" alt="Location marker" width="300" src="https://cdn.pixabay.com/photo/2016/01/14/14/37/marker-1140068_960_720.png" /> -- ### Where in the world? -- ### Where in my program's execution? -- ### So far: -- * one statement at a time -- * a **straight line** through a script ??? Trace through execution of Monty Python Python script --- # Example: an epic quest <img src="../flowcharts/sequential.png" align="right" height="450"/> -- ### _Sequential_ control flow ??? Using sequential control flow, we can make a computer execute our instructions far, far faster than we ever could. That's pretty cool, but... is that all we can do? -- ### ... but is that all we can do? --- # Algorithms ### Previous definition: -- > step-by-step procedure **with decisions** -- #### Decisions within procedures: -- * is the cake golden brown? -- * is the lug nut loose? -- * is the error within acceptable tolerances? --- # Flowchart <img src="https://upload.wikimedia.org/wikipedia/commons/9/91/LampFlowchart.svg" alt="A simple flowchart" align="right" width="300"/> Visual representation of a procedure -- (imperative knowledge, -- **like code**) -- ### Boxes represent _activities_ -- ### Diamonds represent _decisions_ -- You can trace the **flow** of logic through the procedure with your finger -- ... _digitally_ -- 😁 ??? ### _Control flow_ vs _data flow_ --- class: big .center[ <img src="https://www.ibiblio.org/apollo/Documents/GeminiCatchUpAndRendezvousAnalysisAndSimulationReport4-58-59.png" alt="An algorithm programmed in the Gemini computer" width="900"/> ] --- # A little more recently: .center[ <img src="MissionStateDiagram.png" alt="State diagram for the Killick-1 CubeSat" width="900"/> ] --- # Promotion decisions -- ### Term 3+: _is your promotion average greater than 60%?_ -- ### Actually, it's a little more complicated than that... --- class: big .center[ <img src="../flowcharts/term-3.png" width="850" alt="Term 3 promotion decisions"/> ] --- # Flowcharts <img src="https://upload.wikimedia.org/wikipedia/commons/9/91/LampFlowchart.svg" alt="A simple flowchart" align="right" width="300"/> ### Exercise (just for fun): > Draw a flowchart! -- ### Any process that has: * decisions * actions -- ### _Explain_ it to someone ??? This exercise doesn't need to be submitted --- # Control flow in programming -- ### Conditional control flow > either do this or else do that, based on a _condition_ -- ### Looping > do this over and over, as long as a _condition_ is satisified --- # Control flow in Python ### Conditional control flow: ```python if condition: do stuff elif another and condition: do other stuff else: do some other stuff ``` -- ### Looping: later! -- ### Note -- new _keywords_ -- and indentation --- # Example: a possibly-epic quest .center[ <img src="../flowcharts/if-knights.png" alt="A flowchart for not-so-brave knights" height="450"/> ] --- # More Pythonesque? .center[ <img src="../flowcharts/if-knights-python.png" alt="A flowchart using more Python-like syntax" height="450"/> ] --- # Exercise* .footnote[ * Just for fun, not for submission ] ### Make [quest.py](quest.py) use conditional control flow -- <img src="../flowcharts/if-knights.png" align="right" alt="A flowchart for not-so-brave knights" width="400"/> ### Use this logic, or... * What if the knight has a particularly awesome name? * What if the swallow can't travel the necessary distance in time? --- # Exercises .floatleft[ ### In Gradescope: | n | Topic | Description | |---|--------------|-------------| | 0 | Expressions | 42 | | 1 | Expressions | Cantilever beam | | 2 | Variables | Age | | 3 | Conditionals | EOPA | ] .floatright[ ### On your own: * Flowchart * Noble quest ] --- class: big, middle (here endeth the lesson)