PROGRAMMING CURRICULUM with Python
Total Page:16
File Type:pdf, Size:1020Kb
PARC VLP PROGRAMMING CURRICULUM With Python WWW.PARCROBOTICS.ORG Overview Study of programming languages, paradigms and data structures. Chapter 1: Programming Basics Sections A. What is programming? B. What is a programming language? C. Writing source code D. Running your code E. Using IDE Chapter 2: Programming Syntax Sections A. Why Python?A. Why Python? B. Basic statementsB. Basic statements and expressions and expressions C. Troubleshooting issues C. Troubleshooting issues Chapter 3: Variables and Data Types Sections A. IntroductionA. to Introductionvariables and to data variables types and data types B. WorkingB. Working with variables with variables across Languages across Languages C. Working with numbers C. Working with numbers D. Working with strings E. WorkingD. with commentsWorking with strings E. Working with comments Chapter 4: Conditional Code Sections: A. Making decisions in code B. Exploring conditional code C. Working with simple conditions D. Conditionals across languages PAN-AFRICAN ROBOTICS COMPETITION 1 Chapter 1 SECTION A What is programming? Programming is the process of converting ideas into instructions that a computer can understand and execute. These instructions are specific and sequential. You can think of it as a recipe. Let's you want to prepare your favorite food; you would need first a list of ingredients and then a set of instructions as to which ingredients go in first. If you have ever cooked before or watched someone cook before you will know that the amount of each ingredient can dramatically affect the outcome. Computers are very literal. They try to execute our commands exactly. When we give them bad instructions, we might introduce bugs or even make the computer crash. In programming, a bug is when something unexpected happens, whereas a crash is when your program stops early or freezes, just like if you use the wrong ingredients in your cake. Instead of a fluffy, moist cake, you could have a flat, runny disaster. The other characteristic of computer instructions is that they're sequential, just like the steps in a recipe. The order of the steps will impact your final product. It's all about giving computers the right instructions and the correct sequence of steps to produce the desired result. This is important because programming is just as much about finding errors and preventing crashes as it is about writing the instructions or code that the computer understands. So, to recap, programming is how we communicate with the digital world. It's characterized by having instructions that are specific and sequential. And when we get our code right, just like in a recipe, the end result is a program that helps us to enjoy a bit more of our world. Programming is an increasingly important skill, whether you aspire to a career in software development, or in other fields. PAN-AFRICAN ROBOTICS COMPETITION 2 Programming involves activities such as analysis; developing understanding; generating algorithms; verifying the requirements of algorithms, including their correctness and resources consumption; and implementation (commonly referred to as coding) of algorithms in a target programming language. PAN-AFRICAN ROBOTICS COMPETITION 3 SECTION B What is a programming language? When you want to provide instructions to a computer, you use what's called a programming language. There are hundreds, if not thousands, of programming languages available, each made up of its own syntax, or rules, and semantics, or meaning. Let me give you an analogy. In English, we would write, "Good morning”, but in French, we would write, Bonjour. Semantically, they mean the same thing. Likewise, programming languages also have unique syntax rules. Let's look at a few examples of the Hello, world program. Maybe you've already heard of it before. It's the most basic program used for decades to get started with a new programming language. It helps to highlight the differences in syntax between languages. First, let us look at a language called C++. This is how we would get the program to display Hello, world when run. #include Int main() { std::cout<<”Hello World”; Return 0; } Now, it may look strange to you, but that's okay. Soon you'll be able to understand exactly what's happening. For now, I want you to just notice the syntax used in the language. This consists of double quotes around the words Hello, world, the use of a semicolon at the end of statements, and there's a return keyword right before a closing curly brace. All of these things are part of the syntax, or rules, of the C++ programming language. Let's look at a few more examples. This language is called JavaScript. It's a very popular language used in virtually every website these days. This is how we would display Hello, world using JavaScript. document.write(‘Hello, world’) PAN-AFRICAN ROBOTICS COMPETITION 4 Notice the use of different syntax. First, the words Hello, world are now inside of single quotes. We don't have any curly braces. Instead, we see something about document.write. And finally, here's Hello, world in Python print(“Hello world”) Similar syntax to JavaScript, you see the use of parentheses and the use of double quotes has returned, just like we saw with C++. Now you may be wondering, what's the point of having so many different languages, especially if they do the same thing? Why not just have one programming language and we all use it? The main reason why we have so many languages is that each comes with its own set of strengths and weaknesses. Some are ideal for programming small devices with limited memory, whereas other were made to handle complex mathematical computations. Regardless of the language you choose, ultimately, that language has to be broken down into the only one that computers understand, machine language. Machine language is extremely complex for us to write directly because it's mostly just a series of numbers. Professor Mike DeHaan compared machine code to DNA molecules. Can you imagine trying to read a DNA sequence atom by atom? That would take you forever, and this is why we have these other languages that we call high-level languages. They're closer to human languages, comprised of keywords, structure, and syntax that's easier for us to learn and understand. PAN-AFRICAN ROBOTICS COMPETITION 5 SECTION C Writing source code Writing source code is what programmers do all day. It's the instructions we have for the computer, and it's written in plain text. In other words, we write it without special formatting, like bold, italic, or different font types. It's mostly just the actual characters. This means that word processing applications aren't suitable for writing code, because by default, they insert bits of information in files that prevent them from being plain text. Instead, you can use a text editor. If you're on windows, you already have a text editor available to you. Notepad. And for Mac users, there's TextEdit. Both applications allow you to write source code. Let's look at an example. I'll be using Notepad since I'm on a windows. We're going to go ahead and open up Notepad, when using TextEdit the first thing that you'll notice is this formatting bar. By default, TextEdit stores files in Rich Text Format. That means you can do things like bold or italics, or even underline. But this is not suitable for writing source code. To change it, we're going to go over to TextEdit, choose Preferences, and then we're going to choose plain text under the Format section. Also, make sure you uncheck all of the options down below. Great. We can close our Preferences pane, and let's restart TextEdit to make sure that our changes are still present. Perfect. Now that formatting bar is no longer here. If you're on a PC and you're using Notepad, you don't have to worry about this. It uses plain text by default. So, let's start off by doing our favorite program, Hello World. We're going to do the Python version. I'm going to type print, open parentheses, double quotes, hello world, exclamation mark, double quotes, and a closing parenthesis. We did it. We've written some source code. Source code can either be one line, like you see here, or thousands. It just depends on the needs of your program. Let's go ahead and save this file. By default, TextEdit and Notepad saves files with a .txt extension. This is because it's plain text. But it's a best practice to use an extension that corresponds to the programming language that's inside of your file. Since this code was written in Python, we're going to use the Python extension, which is ".py”. So let's save this as myFirstCode.py. PAN-AFRICAN ROBOTICS COMPETITION 6 Perfect. Each programming language has its own file extensions. For JavaScript, it's .js, Perl, .pl, whereas a language like Kotlin uses .kt. Now that we know how to write our code, we need to learn how to translate it into the language that the computer speaks. Machine language. This is a vital step to get it running on any device. SECTION D Running your code If we were to double click our Python file, it would just open in an editor, but it wouldn't do anything with the code that we put inside. Why is that? Well, it's because Python is an interpretive language. This means we have to interpret it into machine code and we haven't run the interpreter yet.