PROGRAMMING CURRICULUM with Python

Total Page:16

File Type:pdf, Size:1020Kb

PROGRAMMING CURRICULUM with Python 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.
Recommended publications
  • Chapter 7 Expressions and Assignment Statements
    Chapter 7 Expressions and Assignment Statements Chapter 7 Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment Statements Mixed-Mode Assignment Chapter 7 Expressions and Assignment Statements Introduction Expressions are the fundamental means of specifying computations in a programming language. To understand expression evaluation, need to be familiar with the orders of operator and operand evaluation. Essence of imperative languages is dominant role of assignment statements. Arithmetic Expressions Their evaluation was one of the motivations for the development of the first programming languages. Most of the characteristics of arithmetic expressions in programming languages were inherited from conventions that had evolved in math. Arithmetic expressions consist of operators, operands, parentheses, and function calls. The operators can be unary, or binary. C-based languages include a ternary operator, which has three operands (conditional expression). The purpose of an arithmetic expression is to specify an arithmetic computation. An implementation of such a computation must cause two actions: o Fetching the operands from memory o Executing the arithmetic operations on those operands. Design issues for arithmetic expressions: 1. What are the operator precedence rules? 2. What are the operator associativity rules? 3. What is the order of operand evaluation? 4. Are there restrictions on operand evaluation side effects? 5. Does the language allow user-defined operator overloading? 6. What mode mixing is allowed in expressions? Operator Evaluation Order 1. Precedence The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated (“adjacent” means they are separated by at most one operand).
    [Show full text]
  • A Concurrent PASCAL Compiler for Minicomputers
    512 Appendix A DIFFERENCES BETWEEN UCSD'S PASCAL AND STANDARD PASCAL The PASCAL language used in this book contains most of the features described by K. Jensen and N. Wirth in PASCAL User Manual and Report, Springer Verlag, 1975. We refer to the PASCAL defined by Jensen and Wirth as "Standard" PASCAL, because of its widespread acceptance even though no international standard for the language has yet been established. The PASCAL used in this book has been implemented at University of California San Diego (UCSD) in a complete software system for use on a variety of small stand-alone microcomputers. This will be referred to as "UCSD PASCAL", which differs from the standard by a small number of omissions, a very small number of alterations, and several extensions. This appendix provides a very brief summary Of these differences. Only the PASCAL constructs used within this book will be mentioned herein. Documents are available from the author's group at UCSD describing UCSD PASCAL in detail. 1. CASE Statements Jensen & Wirth state that if there is no label equal to the value of the case statement selector, then the result of the case statement is undefined. UCSD PASCAL treats this situation by leaving the case statement normally with no action being taken. 2. Comments In UCSD PASCAL, a comment appears between the delimiting symbols "(*" and "*)". If the opening delimiter is followed immediately by a dollar sign, as in "(*$", then the remainder of the comment is treated as a directive to the compiler. The only compiler directive mentioned in this book is (*$G+*), which tells the compiler to allow the use of GOTO statements.
    [Show full text]
  • Operators and Expressions
    UNIT – 3 OPERATORS AND EXPRESSIONS Lesson Structure 3.0 Objectives 3.1 Introduction 3.2 Arithmetic Operators 3.3 Relational Operators 3.4 Logical Operators 3.5 Assignment Operators 3.6 Increment and Decrement Operators 3.7 Conditional Operator 3.8 Bitwise Operators 3.9 Special Operators 3.10 Arithmetic Expressions 3.11 Evaluation of Expressions 3.12 Precedence of Arithmetic Operators 3.13 Type Conversions in Expressions 3.14 Operator Precedence and Associability 3.15 Mathematical Functions 3.16 Summary 3.17 Questions 3.18 Suggested Readings 3.0 Objectives After going through this unit you will be able to: Define and use different types of operators in Java programming Understand how to evaluate expressions? Understand the operator precedence and type conversion And write mathematical functions. 3.1 Introduction Java supports a rich set of operators. We have already used several of them, such as =, +, –, and *. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They usually form a part of mathematical or logical expressions. Java operators can be classified into a number of related categories as below: 1. Arithmetic operators 2. Relational operators 1 3. Logical operators 4. Assignment operators 5. Increment and decrement operators 6. Conditional operators 7. Bitwise operators 8. Special operators 3.2 Arithmetic Operators Arithmetic operators are used to construct mathematical expressions as in algebra. Java provides all the basic arithmetic operators. They are listed in Tabled 3.1. The operators +, –, *, and / all works the same way as they do in other languages.
    [Show full text]
  • THE MEANING of “EQUALS” Charles Darr Take a Look at the Following Mathematics Problem
    PROFESSIONAL DEVELOPMENT THE MEANING OF “EQUALS” Charles Darr Take a look at the following mathematics problem. has been explored by mathematics education researchers internationally (Falkner, Levi and Carpenter, 1999; Kieran, 1981, 1992; MacGregor Write the number in the square below that you think and Stacey, 1997; Saenz-Ludlow and Walgamuth, 1998; Stacey and best completes the equation. MacGregor, 1999; Wright, 1999). However, the fact that so many students appear to have this 4 + 5 = + 3 misconception highlights some important considerations that we, as mathematics educators, can learn from and begin to address. How difficult do you consider this equation to be? At what age do you In what follows I look at two of these considerations. I then go on to think a student should be able to complete it correctly? discuss some strategies we can employ in the classroom to help our I recently presented this problem to over 300 Year 7 and 8 students at students gain a richer sense of what the equals sign represents. a large intermediate school. More than half answered it incorrectly.1 Moreover, the vast majority of the students who wrote something other The meaning of “equals” than 6 were inclined to write the missing number as “9”. Firstly, it is worth considering what the equals sign means in a This result is not just an intermediate school phenomenon, however. mathematical sense, and why so many children seem to struggle to Smaller numbers of students in other Years were also tested. In Years 4, develop this understanding. 5 and 6, even larger percentages wrote incorrect responses, while at Years From a mathematical point of view, the equals sign is not a command 9 and 10, more than 20 percent of the students were still not writing a to do something.
    [Show full text]
  • It's the Symbol You Put Before the Answer
    It’s the symbol you put before the answer Laura Swithinbank discovers that pupils often ‘see’ things differently “It’s the symbol you put before the answer.” 3. A focus on both representing and solving a problem rather than on merely solving it; ave you ever heard a pupil say this in response to being asked what the equals sign is? Having 4. A focus on both numbers and letters, rather than H heard this on more than one occasion in my on numbers alone (…) school, I carried out a brief investigation with 161 pupils, 5. A refocusing of the meaning of the equals sign. Year 3 to Year 6, in order to find out more about the way (Kieran, 2004:140-141) pupils interpret the equals sign. The response above was common however, what really interested me was the Whilst reflecting on the Kieran summary, some elements variety of other answers I received. Essentially, it was immediately struck a chord with me with regard to my own clear that pupils did not have a consistent understanding school context, particularly the concept of ‘refocusing the of the equals sign across the school. This finding spurred meaning of the equals sign’. The investigation I carried me on to consider how I presented the equals sign to my out clearly echoed points raised by Kieran. For example, pupils, and to deliberate on the following questions: when asked to explain the meaning of the ‘=’ sign, pupils at my school offered a wide range of answers including: • How can we define the equals sign? ‘the symbol you put before the answer’; ‘the total of the • What are the key issues and misconceptions with number’; ‘the amount’; ‘altogether’; ‘makes’; and ‘you regard to the equals sign in primary mathematics? reveal the answer after you’ve done the maths question’.
    [Show full text]
  • Perl II Operators, Truth, Control Structures, Functions, and Processing the Command Line
    Perl II Operators, truth, control structures, functions, and processing the command line Dave Messina v3 2012 1 Math 1 + 2 = 3 # kindergarten x = 1 + 2 # algebra my $x = 1 + 2; # Perl What are the differences between the algebra version and the Perl version? 2 Math my $x = 5; my $y = 2; my $z = $x + $y; 3 Math my $sum = $x + $y; my $difference = $x - $y; my $product = $x * $y; my $quotient = $x / $y; my $remainder = $x % $y; 4 Math my $x = 5; my $y = 2; my $sum = $x + $y; my $product = $x - $y; Variable names are arbitrary. Pick good ones! 5 What are these called? my $sum = $x + $y; my $difference = $x - $y; my $product = $x * $y; my $quotient = $x / $y; my $remainder = $x % $y; 6 Numeric operators Operator Meaning + add 2 numbers - subtract left number from right number * multiply 2 numbers / divide left number from right number % divide left from right and take remainder take left number to the power ** of the right number 7 Numeric comparison operators Operator Meaning < Is left number smaller than right number? > Is left number bigger than right number? <= Is left number smaller or equal to right? >= Is left number bigger or equal to right? == Is left number equal to right number? != Is left number not equal to right number? Why == ? 8 Comparison operators are yes or no questions or, put another way, true or false questions True or false: > Is left number larger than right number? 2 > 1 # true 1 > 3 # false 9 Comparison operators are true or false questions 5 > 3 -1 <= 4 5 == 5 7 != 4 10 What is truth? 0 the number 0 is false "0"
    [Show full text]
  • Unicode Compression: Does Size Really Matter? TR CS-2002-11
    Unicode Compression: Does Size Really Matter? TR CS-2002-11 Steve Atkin IBM Globalization Center of Competency International Business Machines Austin, Texas USA 78758 [email protected] Ryan Stansifer Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901 [email protected] July 2003 Abstract The Unicode standard provides several algorithms, techniques, and strategies for assigning, transmitting, and compressing Unicode characters. These techniques allow Unicode data to be represented in a concise format in several contexts. In this paper we examine several techniques and strategies for compressing Unicode data using the programs gzip and bzip. Unicode compression algorithms known as SCSU and BOCU are also examined. As far as size is concerned, algorithms designed specifically for Unicode may not be necessary. 1 Introduction Characters these days are more than one 8-bit byte. Hence, many are concerned about the space text files use, even in an age of cheap storage. Will storing and transmitting Unicode [18] take a lot more space? In this paper we ask how compression affects Unicode and how Unicode affects compression. 1 Unicode is used to encode natural-language text as opposed to programs or binary data. Just what is natural-language text? The question seems simple, yet there are complications. In the information age we are accustomed to discretization of all kinds: music with, for instance, MP3; and pictures with, for instance, JPG. Also, a vast amount of text is stored and transmitted digitally. Yet discretizing text is not generally considered much of a problem. This may be because the En- glish language, western society, and computer technology all evolved relatively smoothly together.
    [Show full text]
  • Types of Operators Description Arithmetic Operators These Are
    UNIT II OPERATORS Types of Operators Description These are used to perform mathematical calculations like addition, subtraction, Arithmetic_operators multiplication, division and modulus These are used to assign the values for Assignment_operators the variables in C programs. These operators are used to compare the Relational operators value of two variables. These operators are used to perform logical operations on the given two Logical operators variables. These operators are used to perform bit Bit wise operators operations on given two variables. Conditional operators return one value Conditional (ternary) if condition is true and returns another operators value is condition is false. These operators are used to either Increment/decrement increase or decrease the value of the operators variable by one. Special operators &, *, sizeof( ) and ternary operator ARITHMETIC OPERATORS IN C: C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs. Arithmetic Operators/Operation Example + (Addition) A+B – (Subtraction) A-B * (multiplication) A*B / (Division) A/B % (Modulus) A%B EXAMPLE PROGRAM FOR C ARITHMETIC OPERATORS: // Working of arithmetic operators #include <stdio.h> int main() { int a = 9,b = 4, c; c = a+b; printf("a+b = %d \n",c); c = a-b; printf("a-b = %d \n",c); c = a*b; printf("a*b = %d \n",c); c = a/b; printf("a/b = %d \n",c); c = a%b; printf("Remainder when a divided by b = %d \n",c); return 0; } Output a+b = 13 a-b = 5 a*b = 36 a/b = 2 Remainder when a divided by b=1 Assignment Operators The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.
    [Show full text]
  • Programming Basics
    Readings and References • Reading » Fluency with Information Technology Programming Basics • Chapter 18, Fundamental Concepts Expressed in JavaScript • Other References INFO/CSE 100, Autumn 2004 » Games and Puzzles Fluency in Information Technology • Thomas Jefferson National Accelerator Facility, Office of Science Education http://www.cs.washington.edu/100 • http://education.jlab.org/indexpages/elementgames.html 25-Oct-2004 cse100-11-programming © 2004 University of Washington 1 25-Oct-2004 cse100-11-programming © 2004 University of Washington 2 The Plan Begin with HTML • We will learn JavaScript over the next few Basic HTML is static lectures the contents of the file are displayed as given • JavaScript is used with HTML in Web pages <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" • JavaScript is a contemporary programming language -- "http://www.w3.org/TR/html4/loose.dtd"> we will learn only its basics <html> <head> • You will program in a text editor and run your program <title>Simple A</title> </head> with your browser <body> What is 2.0 + 2.0? </body> </html> JavaScript is a way to make HTML “dynamic” 25-Oct-2004 cse100-11-programming © 2004 University of Washington 3 25-Oct-2004 cse100-11-programming © 2004 University of Washington 4 Add some “dynamic” content JavaScript in an HTML page Scripting languages let us create active pages » implement actions to be taken at run-time when the page is <script> block loaded or in response to user event in <head> <head> <title>Simple B</title> Language we are javascript <head>
    [Show full text]
  • Brief Overview of Python Programming Language
    Brief Overview Chapter of Python 3 In this chapter » Introduction to Python » Python Keywords “Don't you hate code that's not properly » Identifiers indented? Making it [indenting] part of » Variables the syntax guarantees that all code is » Data Types properly indented.” » Operators » Expressions — G. van Rossum » Input and Output » Debugging » Functions » if..else Statements » 3.1 INTRODUCTION TO PYTHON for Loop » Nested Loops An ordered set of instructions or commands to be executed by a computer is called a program. The language used to specify those set of instructions to the computer is called a programming language for example Python, C, C++, Java, etc. This chapter gives a brief overview of Python programming language. Python is a very popular and easy to learn programming language, created by Guido van Rossum in 1991. It is used in a variety of fields, including software development, web development, scientific computing, big data 2021-22 Chap 3.indd 31 19-Jul-19 3:16:31 PM 32 INFORMATICS PRACTICES – CLASS XI and Artificial Intelligence. The programs given in this book are written using Python 3.7.0. However, one can install any version of Python 3 to follow the programs given. Download Python 3.1.1 Working with Python The latest version of To write and run (execute) a Python program, we need Python is available on the to have a Python interpreter installed on our computer official website: or we can use any online Python interpreter. The https://www.python. interpreter is also called Python shell. A sample screen org/ of Python interpreter is shown in Figure 3.1.
    [Show full text]
  • UEB Guidelines for Technical Material
    Guidelines for Technical Material Unified English Braille Guidelines for Technical Material This version updated October 2008 ii Last updated October 2008 iii About this Document This document has been produced by the Maths Focus Group, a subgroup of the UEB Rules Committee within the International Council on English Braille (ICEB). At the ICEB General Assembly in April 2008 it was agreed that the document should be released for use internationally, and that feedback should be gathered with a view to a producing a new edition prior to the 2012 General Assembly. The purpose of this document is to give transcribers enough information and examples to produce Maths, Science and Computer notation in Unified English Braille. This document is available in the following file formats: pdf, doc or brf. These files can be sourced through the ICEB representatives on your local Braille Authorities. Please send feedback on this document to ICEB, again through the Braille Authority in your own country. Last updated October 2008 iv Guidelines for Technical Material 1 General Principles..............................................................................................1 1.1 Spacing .......................................................................................................1 1.2 Underlying rules for numbers and letters.....................................................2 1.3 Print Symbols ..............................................................................................3 1.4 Format.........................................................................................................3
    [Show full text]
  • Using Regex to Parse Attribute-Value Pairs in a Macro Variable Rowland Hale, Syneos Health
    PharmaSUG 2021 - Paper QT-134 Using Regex to Parse Attribute-Value Pairs in a Macro Variable Rowland Hale, Syneos Health ABSTRACT In some circumstances it may be deemed preferable to pass multiple values to a macro not via multiple parameters but via a list of attribute-value pairs in a single parameter. Parsing more complex parameter values such as these may seem daunting at first, but regular expressions help us make light of the task! This paper explains how to use the PRXPARSE, PRXMATCH and the lesser known PRXPOSN regular expression functions in SAS® to extract in robust fashion the values we need from such a list. The paper is aimed at those who wish to expand on a basic knowledge of regular expressions, and although the functions are applied and explained within a specific practical context, the knowledge gained will have much potential for wider use in your daily programming work. INTRODUCTION The author occasionally comes across seasoned and capable SAS programmers who haven’t taken the plunge into the powerful world of regular expressions. Not so surprising, given that regular expressions look, well, downright scary, don’t they? For sure, regex patterns can appear complex, but, on the other hand, when you see a series of three (or more!) “normal” (i.e. non regex) string handling functions, all mind-bogglingly nested on a single line of code (and, if in a macro context, each further buried inside a %SYSFUNC macro function) and realise that this can all be replaced with a relatively simple regular expression, you may start to wonder which is the easier to deal with.
    [Show full text]