# Welcome! <img src="http://www.qwantz.com/comics/comic2-3075.png" alt="Dinosaur Comic about Sherlock Holmes and computers" class="floatright" linkToFull="true" width="650"/> Please enjoy this comic while you sign into Top Hat: [tinyurl.com/1020-th-2024-25W](https://tinyurl.com/1020-th-2024-25) <img src="1020-th-2024-25W-1200.png" width="225"/> --- class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture 0:] .title[Introduction] ] .footer[[/lecture/0/](/lecture/0/)] --- # Who are you? .column[ ### You: First semester? Been around? Have a degree? ] -- .column[ ### Me: Associate Professor in [ECE](https://mun.ca/engineering/ece) B.Eng., M.Eng. from Memorial Ph.D. from [University of Cambridge](https://www.cst.cam.ac.uk) Research: computer/cyber security ] --- # Why are you here? ??? Top Hat: "why are you here" and "how do you feel" --- # Why study programming? -- ### Career, entrepreneurship, changing the world... -- ### Responsibility ??? Even if you don't write much software in your career, we live in a **digital world that cannot be mastered without some understanding of the way computers work**. Engineers use computers in lots of ways, whether we put computers into the systems we just use computer software to help us design things. Engineers are **responsible for their use of computers**: you can't blame your tools for faulty design! "My software gave me the wrong answer" is no better an excuse than "my calculator is broken", so we need to **understand how to gain confidence that software is working correctly**. -- ### Important way of thinking about problem-solving -- * algorithmic / computational thinking comes up in **every** Engineering discipline! ??? Computer programming gives us **a helpful way of solving problems**, not to mention **a structured way of thinking about problems and solutions**. Every engineer should be able to express solutions to certain classes of problems using *algorithms* (structured, step-by-step solutions to problems). Many first-year Engineering students think this is the last they'll hear of programming, but Statistics begs to differ, as does Fluid Dynamics, as does seismic analysis... even effective use of **spreadsheets** is greatly helped by some fundamental understanding of programming. And, of course, good software designers are **always in demand in the job market!** -- * interesting problems to solve... can be _fun!_ ??? Finally, whether or not you currently believe it, I hope to show you this term that **programming can be fun!** --- # Solving problems with software -- .tightlist[ * **specify** engineering processes using _propositional logic_ * **recognize** the pervasiveness of computing in engineered systems and **explain** its _systemic impacts_ [...] * **explain** how computers store values, _compute_ on them * **choose** _data types_ to solve engineering problems * **analyze** software; explain how it works (or why it doesn't) * **construct** solutions using _imperative programming_ * **design** _algorithms_ to solve engineering problems though _top-down procedural decomposition_ ] ??? Q: What do you notice about these learning outcomes for ENGI 1020? A: None of them say anything about **Python**! This course is about how to **think programmatically**, not how to **program in Python**. The lessons you take away from this course should be portable to all kinds of programming languages, and even to non-programming situations where structured, analytical thinking is called for — which ought to be **most of Engineering**! We use software to address incredibly complex problems, from building self-driving cars (or [self-driving submarines](https://merlin.creait.mun.ca), as some folks do here at Memorial) to [training ship's crews](https://www.mi.mun.ca//departments/centreformarinesimulation) to securing networks against hackers (as in [my own research](https://www.engr.mun.ca/~anderson/research)). In fact, computer programs are among the most complex artifacts ever built. --- <img src="toolbox.png" alt="Toolbox" class="floatright" width="300"/> # Programming toolbox -- * logic -- * computation -- * data types -- * imperative programming... -- * ... in Python ??? That said, we do need to put this theory into practice, and that requires a programming language. Like a **musician**, theory alone is insufficient: in order to _really_ get it, you need to **lock yourself away in a practice room** and work on it until it moves from your **head** to your **gut**. Just like you can apply musical theory to a piano or a guitar or any other instrument, so we can apply our programming tookbox to any programming language, but in our exercises, assignments and labs, we will use the Python programming language. Python is a very useful language because it supports many different ways of thinking about programming, it includes lots of useful software tools for interacting with users and other computers, and it provides a good foundation from which you can learn other programming languages. However, please remember throughout the course that our goal is to learn about **programming in general**, not just a particular programming language. --- # How to succeed -- ### Active engagement: ??? A good way to underperform in this course is to come to lectures, passively consume them, only do the things you are explicitly told to do and let you lab partner do things without your understanding. The value you get out of this course will be proportional to the effort you put in to **explore** and **understand** the material. Specifically, it is helpful to: -- * Try things out ??? When I show you some example code and explain how it works, don't take my word for it: try it yourself! Try running the same code with different inputs to see if you can predict how it will react. Try finding different solutions to the same problem or to slightly different problems to gauge your own understanding of the material. -- * Practice, practice, practice! ??? Software design and development, like other kinds of Engineering design, blends both **science and art**. I will teach you things about designing software-based solutions to problems, but you can't really say you've learned design unless you practice designing. This is much like learning to play a musical instrument, you need to learn some theory, but **you also need to practice**. The [Tools page](../../resources/tools) provides details on how to get set up with software tools for writing, compiling and testing Python code. Please take a look at this page and make sure that you are comfortable with **at least one** of these tools during the **first week of lectures**. -- * Stay curious ??? In class or labs, in Office Hour or the Success Centre, with friends or with me, via Top Hat or in Brightspace, if you don't understand something, **ask**. -- * Work together **and** independently ??? Most students will find that, to help them truly succeed, they need to **both** work with other students on some things **and** work alone on others. In this course you will encounter lots of exercises and examples that you should work through, and you may find it helpful to do so with your peers so that you can help each other out as your grow in your understanding of the material. You will also have (at least) one partner with whom you will work on prelab questions, experiments and writing. So, there will be plenty of opportunity to work in groups and in pairs to help you form your ideas about programming and how it's done. It's also a good idea to complete at least some of the exercises **by yourself** to check that you're able to think algorithmically yourself, not just when you're working with others. After you've formed your understanding together, checking your understanding individually can help give you confidence that _you_ are building mastery of this material. **Assignments must be completed individually.** -- * Stay ahead by [managing your time](https://uwaterloo.ca/future-students/missing-manual/high-school/top-time-management-tips-students) --- # Resources -- ### Course website: https://engi1020.ca -- .floatright[ <img alt="Guttag, Introduction to Computation and Programming Using Python" src="https://images-na.ssl-images-amazon.com/images/I/51LkVe5w8wL._SX384_BO1,204,203,200_.jpg" width="250"/> ] ### Textbook useful but _optional_ -- ### [Lab kit](https://shop.mun.ca/CourseSearch/?course%5B%5D=1,202402,ENGI,ENGI1020,001&) _mandatory_ -- ### Web resources: Gradescope, Top Hat... https://www.gradescope.ca/courses/23847 https://app-ca.tophat.com/e/308346 -- (also linked from Brightspace) --- # Human resources -- * office hours: in the course outline -- * online: email me (address in the course outline) -- ### Lab instructor: Alice Faisal (CSF-4111) -- ### Engineering One Success Center: -- Tutors: consult “Engineering One” Brightspace shell -- Supplemental Instruction (SI) leader: Jacob Penney --- # Actions for today ### Ensure you can access the following resources: -- **Brightspace** (grades) https://online.mun.ca/d2l/home/615597 -- **Gradescope** (assignments, labs) https://www.gradescope.ca/courses/23847 -- **Top Hat** (lectures, interactive response) https://app-ca.tophat.com/e/308346 -- #### If not, please email me _today_ --- # What computers are -- <img src="../images/computer.png" alt="Computer structure" width="400" class="floatright"/> -- ### Memory * a.k.a., _random access memory_ * stores information in _bits_ -- ### Central processing unit * performs _computation_ -- ### Input/output devices ??? These three functional components allow the computer to **compute**, to **remember** and to **communicate**. There are lots of other things inside of a modern computer, but they all exist to serve and facilitate these main tasks. This overall structure is the same for the computer you're using to read these lecture notes, the Arduino computers that you'll use in the lab, the computer(s) inside your watch and even the computers that run cars, lighting fixtures, solar panel trackers, etc.! All of these computers have the **same structure**. What's different about them is the power of their CPUs, the quantity of their memories and the kinds of I/O devices they have. --- # What computers do -- ### Remember things (memory) * numbers * _strings_ of characters (words, sentences, etc.) * program instructions -- ### Follow instructions (CPU) --- # CPUs follow instructions ??? The CPU is the brain of the computer, and we will spend most of our time thinking about how it does its work (later courses in programming, microprocessors and data structures spend more time on these). ### "How do you eat an elephant" joke -- ### One step at at time ??? The key thing to keep in mind is that a CPU **follows instructions**, and it does this **one step at a time**. The CPU follows a **sequence of _instructions_**. -- * Computers are dumb -- * Computers are fast ??? Computers aren't capable of thinking, but they *can* execute simple instructions very quickly! -- ### Where do the instructions come from? --- # Program instructions -- ## What is knowledge? -- ### Declarative / propositional knowledge ??? Logical **propositions** are statements that can be either true or false. The propositions on this slide are all true — at least today! We leave as an exercise for the reader determining which of these propositions could be false, or alternatively, determining just how bad a day it would be if each of them _were_ false! -- $\pi$ is 3.1415926..., my name is Jon, it's warm, I like Engineering -- ### Imperative / procedural knowledge ??? **Imperative** or **procedural** knowledge concerns things that you **know how** to do. The procedural examples on this slide include activities for which you could write instructions (using the **imperative** verb tense); someone else could follow these instructions in order to do the task. The instructions might be encoded in a math textbook, a shop manual or a recipe book, but the fundamental construction of a procedure is the same. -- How to calculate $\sqrt{24}$, how to change a tire or bake a cake ??? Such a set of step-by-step instructions for accomplishing a task is called an **algorithm**. -- ... a.k.a., an _algorithm_ --- # Algorithm <img src="bakeoff.jpg" alt="The Great British Bake-off" width="500" class="floatright"/> -- _A step-by-step procedure with decisions_ ??? ### Why the Great British Bake-off? An algorithm is like a recipe -- ### Example algorithm: 1. Let $y = \frac{x}{2}$. 2. If $2y = x$, $x$ is even. ??? Note that this is more than just a mathematical formula or equation: it involves a step-by-step approach that may (for many algorithms) be difficult to represent as an equation. -- ### ["bake until golden brown"](http://www.bbc.co.uk/food/recipes/mary_berrys_perfect_34317), ["for each tire"](http://www.instructables.com/id/How-to-Rotate-Your-Cars-Tires)... --- # Another algorithm ## Sum of integers -- e.g., find the sum of the set $S = \{4, 7, 2, 11, 5, 8, 1\}$ -- ### Mathematically: $$\sum\_{i=1}^n S\_{1..n}$$ -- ### Algorithmically? ??? ### Suggested methodology: * work out an example or two * break it into steps * explain the steps to someone else (pretend they're a computer) -- (do this as an exercise) --- # Software ??? What is software? ### Softer than hardware? ### There's also "firmware", but... -- > Description of instructions for a computer ??? Software, whether written in C++, Java, Python or another programming language, is a way of **describing algorithms to a computer**. Consider the following implementations of the algorithm from above that checks whether or not a number is even. -- #### You express meaning using a _programming language_ -- .floatleft[ **Python:** ```python y = x / 2 if (2 * y == x): print(x, 'is even') ``` ] -- .floatleft[ **C++:** ```cpp int y = x / 2; if (2 * y == x) cout << x << " is even\n"; ``` ] ??? You should note that these two code snippets look a bit different, as the details of the two programming languages are different, but the **fundamentals of algorithms are the same**. In both the Python and C++ examples, a program has to translate the programmer-readable _source code_ into computer-readable _machine code_ for the CPU to execute. This process will happen mostly transparently to us as we work with Python via [online Python environments]({{% relref "/resources/tools/online" %}}) or [_integrated development environments_ (IDEs)]({{% relref "/resources/tools/ides" %}}). I have provided information about getting started with Python tools on the [tools page]({{< relref "/resources/tools" >}}). -- **Also:** Assembly, C, C#, Go, Java, Matlab, Perl, R, Rust, Scala, SPIN... -- #### Your code is translated into _machine instructions_ --- # Programming languages -- ### vs natural languages * like natural languages * unlike natural languages ??? Like natural languages, a medium for expressing semantics Unlike natural languages, highly constrained (more like math). Allows succinct yet powerful constructions. -- ### Syntax and semantics -- * _syntax_: rules of **well-formed** language -- * _semantics_: the **meaning** of it all --- # Write some software ## Yes, right now! -- 1. Think about a problem -- , e.g., what is $ 1 + 2 \times 3 - 4$? -- 2. Compute an answer -- 2. Check your answer with Python Type `1 + 2 * 3 - 4` into, e.g., https://www.pythonmorsels.com/repl, then press Enter --- # What did you just do? -- ### Wrote an _expression_ -- ### Expression was _evaluated_ -- ## What is an expression? ### Algebra: -- values, operators that **evaluate** -- to a **value** -- ### Programming: -- the same! -- (even operator precedence) --- # Exercise 0 ### Submit a Python expression that evaluates to 42 -- Submit `.py` file [to Gradescope](https://www.gradescope.ca/courses/8953) -- .floatleft[ ### Not: ```python Python 3.9.1 >>> x = 21 >>> y = 21 >>> x + y 42 ``` ] -- .floatleft[ ### Or: ```python x = 21 y = 21 print(x + y) ``` ] -- .floatleft[ ### Just: ```python 21 + 21 ``` ] -- .floatleft[ ### Or even: ```python 42 ``` ] -- .floatright[ #### See: ["Resources" > "Tools"](../../resources/tools) ] --- class: big # Questions? --- class: big, middle (here endeth the lesson)