Download presentation
Presentation is loading. Please wait.
1
Benfeard Williams June 16th, 2016
How to Read Code Benfeard Williams June 16th, 2016 Learning the “grammar” of coding. Similar to a foreign language, reviewing the basics beneficial
2
Concepts You Will Learn
Programming Skill, science, engineering, art, creativity Problem-solving How to solve problems using computer programming Impact of computer science Scale and automation Foundation for future work There are many different ways to use code to address a problem – learn a style and type that works best for you Use code as a tool to help you avoid manual labor that would take you much longer
3
Things to know whenever you are reading code
Details to consider
4
Interpreted vs Compiled Code
Read and executed by another program on the target machine Easy to implement Compiled Expressed specifically for the target machine Faster performance Important to know for sharing and running shared code. Python and R can be installed on a computer and used to run shared (interpreted) code. However, some programs have specified instructions for installation, indicating that the code behind that program are compiled for that machine
5
Computers Do What You Tell Them
They are fast but not smart You need to plan exactly what the computer needs to do in order to solve a problem YOU need to know how to solve the problem in order for the computer to carry out that task for you. Even if you couldn’t in your lifetime perform a certain analysis, you can tell the computer exactly how to do it.
6
Declarative Knowledge
Statements of fact “A good health care plan improves the quality of medical care while saving money” “y is the square root of x if and only if y*y = x” Aka, if we want to know if y is the square root of x, we need to “fulfill” this statement. Aka, statement must be true
7
Imperative Knowledge How to accomplish something (recipe)
Start with a guess, g If g*g is close enough to x, then g is a good approximation of the square root of x Otherwise, create a new guess by averaging g and x/g. Using this new guess, go back to step 2 Get close Get closer (if g not close enough) Check
8
What will you see in code?
What is in the code that you need to pay attention to in order to understand what it’s doing What will you see in code?
9
Building Blocks Variables Data types Operators Functions (subroutines)
Name of a storage location Assign a value to a variable Data types Various classifications of data Operators Arithmetic: + - * / % ** Functions (subroutines) Sequence of instructions to perform a specific task Variable – where you are storing data in order to use later, with specific name (not a physical location)
10
Variables Names are unique and can be abstract
What is What is Declare a variable and assign a value Name and type Examples: int age = 18; age = 18 Example of assigning a name (age) to a piece of data (18) Can explicitly state data type (int age) or sometimes the computer will infer the type (age) – see next
11
Data Types Int (integer) Float (floating-point number)
4, 13, -7 Float (floating-point number) Work just like normal numbers ans = 2 * (7 + 4) – 1 ans = 21 Example: if we set some variable (ans) to a mathematical statement, the computer can run the math and set this as the variable
12
Data Types Char (characters) Str (string) Arithmetic manipulation
“HELLO”, “Echo 123” Arithmetic manipulation phrase = “My name is ” + “Benfeard” phrase = “My name is Benfeard” Known that they are text because they are in quotes (so “3” can NOT be used as a number – has no numerical value) Can use either single or double quotes, but must be consistent Anything within the quotes are interpreted as a single unit (string) and can add these strings together Now will form a new string
13
Data Types Arrays/List Manipulation Indexing elements
Collection of elements [ 1, 2, 3, 4, 5] [ ‘A’, ‘B’, ‘C’, ‘D’, ‘E’ ] Manipulation myArray = [1, 2, 3] myArray = myArray * 2 myArray = [1, 2, 3, 1, 2, 3] Indexing elements Want to have certain elements always together Careful with arithmetic manipulation, because might not act as you’d expect Whatever is on the RIGHT of the equals will be put together and the thing on the LEFT of the equals will have this value Computer knows the order of the things in you list, so you can use the index (list position) of each item to call it forward
14
Functions Allows you to easily recall a procedure or subroutine
def sum(a, b): return a+b Parameters values in the call, that you pass to the function to use as input Output Return value Call Function answer = sum(7,3) answer = 10 Exact syntax will depend on the coding language Define a function (write the part of the code) Call a function (use this pre-written code) - need to “pass” it a set of parameters - like a mathematical function, need some input Computer will “return” a value Can then set a variable ot be the output of a certain function, if passed inputs
15
Functions Example function that returns a value def greet(name):
print "Hello " + name Advantages Repeat code, call multiple times Flexible, call with different arguments Functions greet("Sue") Can pass the function a list of inputs to avoid typing the same thing repeteticly.
16
Booleans True or False Useful for comparisons
Greater than, less than Is equal to, is not equal to Supports algebraic operations and, or, not A variable can have the VALUE of True or False Be careful using these words in code if not using as values Computer will understand value as true or false can then ask computer whether something is true or false “Looking at my set of data: is the concentration greater than 10 mM and color is yellow?”
17
If Statements and Loops
If this statement is true, do something For loops For all values in an array, do something repeatedly While loops While a statement is true, do something repeatedly Way of asking a question to your computer – will evaluate data and make a decision for you Different types of loops for different reasons – use the best one to maximize the efficiency of your code
18
Understanding the code
19
Computers do what you tell them
They are fast but not smart You need to plan exactly what the computer needs to do in order to solve a problem Outline the solution to your problem Pseudocoding (step by step walkthrough of code) The computer reads the code as written – syntax is important YOU NEED TO PSEUDOCOE to outline and understand the logic behind the problem solving Good way of double checking
20
Algorithms Describe in words how to solve a problem
Like a recipe, must be detailed and precise How to make a Peanut Butter & Jelly Sandwich Ingredients: Two slices of bread Peanut Butter Jelly Spread peanut butter on one slice Spread jelly on the other slice Combine slices together Does this provide enough information for anyone to make the sandwich? Always ask yourself FIRST how to explain how to solve the problem in words Variables: ingredients Algorithm: instructions Broke code will often be missing a key logical part
21
How To Dissect Code Important information about function and usage
Opening a romeo.txt file Counting something Looping through lines Example of ok code Need more descriptive title Need comments throughout Need better, more useful header (what program used, etc) Like how you can change the language of your Word document spell check This done in a text editor, which is why the code has colors, line numbers, and etc to help you read the code First variable: filename with the string value of “romeo.txt” fileToProcess with a value of the output of the function “open” on the variable “filename” Indentation at line 10 indicates that we are within some loop – can see that it is a for-loop If you don’t know part of a code, you can always Google part of the code, or certain terms, with the language of code you are using Aka google: strip() python == means “if equal to” So this will add 1 to the count, then restart the loop until the list of words is exhausted Splitting lines into words? Words of length 4? Increase counter Print count at the end
22
Computers Read In Order
Because this code is out of logical order (the computer reads sequentially), the computer does not know what to do at line 9 because it does not recognized the non-declared variable fileToProcess
23
Important Resources Sakai webpage howtolearntocode.web.unc.edu
HtLtC teachers Stackoverflow.com ITS Research Computing
24
Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.