Introduction to Programming Concepts (for True Beginners!)

Slides: http://gdiphilly.github.io/intro-programming-concepts
Girl Develop It Philly

Welcome!

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

Some "rules"

  • We are here for you!
  • Every question is important
  • Help each other
  • Have fun

What we'll cover today

  • What is a computer program?
  • Variables
  • Memory and Storage
  • Logical Operators
  • If/Then/Else Statements
  • Loops
  • Comments
  • Syntax vs. Semantics vs. Style
  • Front-end vs. Back-end development

Computer Programs

Computer programs are everywhere.


  • They run parts of your car
  • They let you check your email on your phone
  • They comb through millions of results to match your search terms on the web


But how do you go from an idea to a computer program? And what IS a computer program?

What is Code?

Turns out you're not the only person to wonder this.

Paul Ford wrote a 38,000-word essay called "What is Code" that was published on June 11, 2015, in Bloomberg Businessweek, and it captured the attention of many.

Computer Programs

At its core, a computer program is a way of getting something done.

One of the ways to accomplish "getting something done" is a list of instructions that tells the computer exactly what to do and allows interaction with human beings.

Those instructions have to be written in a "language" that the computer can understand.

Computer Programs

Computers don't understand things the same way humans do. Words and symbols that make sense to human eyes need to be translated down to binary for the computer to understand.

Binary is a system of expressing everything using only 1s and 0s.

For example, in ASCII (one system of encoding in binary), the lower case letter 'a' is represented as '1100001'.

'GDI' in ASCII is 01000111 01000100 01001001!

Computer Programs - Levels of Abstraction

From human-readable languages to binary:


Image credit: https://blog.malwarebytes.org/intelligence/2012/09/so-you-want-to-be-a-malware-analyst/

Computer Programs - Levels of Abstraction

Baking a cake....

  • Programmers who work with high-level languages buy a box of cake mix at the store and add some fun extras (walnuts, gummy bears, Nutella). They can focus on the final product without worrying about exactly how the ingredients were prepared.
  • Programmers who work with low-level languages harvest the wheat and collect the eggs from the henhouse. They have more control over the details of their cake implementation but must labor over every ingredient.

Programming Languages

So which human-readable programming language do you choose to use?


It depends on the task you're trying to accomplish!


Different languages are designed to do different things.

Programming Languages

  • To develop websites, you might use HTML, JavaScript, Python, Java, or PHP.
  • To create databases and move information in and out of them, you might use MySQL, SQL, or Postgres.
  • To write applications like Microsoft Word or Adobe Acrobat, you might use C++ or Java.
  • To automate your operating system or an application like Word, you might use Bash or Visual Basic.

Computers are dumb

  • Computers can only do what they have instructions for, and they take those instructions VERY literally.
  • As a result, you have to tell them every single thing you want them to do and every single thing to take into consideration.
  • If you leave any instructions out (or put them in the “wrong” order), the results might not be what you expect.

PEBKAC

Ever heard of the acronym "PEBKAC"?


Problem Exists Between Keyboard And Chair

In other words, user error.


Remember, when you're writing a program and the computer is executing it, the computer is only doing what you told it to do. If it's not working the way you expect, look for an error in your program.

Hello, World! - Syntax

Just like English has the concepts of vocabulary and grammar, so do computer languages!


In computer programming, the syntax of a computer language is what defines if the combination of letters and symbols used are considered a valid construction in that language.

Syntax mistakes may cause your code to throw compile time errors.

Hello, World!

When you're first playing around and learning the syntax of a new computer language, one of the most common first exercises is called "Hello, World!".


The "Hello, World!" program is one that outputs the text "Hello, World!" on the screen for the user.


This task is usually relatively simple to accomplish in most programming languages, so it's a good warm-up exercise.

Hello, World!

There are many, many, many examples in many, many, many languages.


Let's try it!

We're going to use a website called https://repl.it/ to try out "Hello, World!" in a language or two.

Getting started

Once you have a project and a language, you'll want to program your project in that language.


Let's talk about some of the basic foundational building blocks of all programming languages.


Keep in mind that each language may have a different syntax for expressing these concepts, but the underlying concepts will be the same through them all.

Variables

A variable is a storage container for information.


A variable has a name that you use to refer to it, and a value, which is the information it contains.


For example:

  • age = 28
  • color = blue


There are different types of variables based on the information that you want to store.

Integers and Floats

Integer variables are used to store integers (0 as well as positive and negative whole numbers).


Float variables are used to store numbers with fractional values (that is, containing decimal places).


For example, consider Amazon.com:

  • The number of books you can order is stored as an integer (e.g., 2)
  • The price of the books is stored as a float (e.g., 15.99)


Not every programming language makes a distinction between these two (and some have even finer-grained distinctions!), but it's good to be aware of.

Strings

String variables are used to store text. A string is made up of characters like letters, numbers, and symbols.


For example, on Amazon.com:

  • In your profile, information like your name, address, state, and city are stored as strings.
  • An item's description is stored as a string


Usually when you're creating a string variable, you will need to surround the value with either single or double quotation marks (either ' or ") to specify the start and end of the string. (Remember how "Hello, World!" had quotes?)

Numbers as strings

You can store a number as a string variable, but then you typically can't use it in any mathematical calculations.


For example, credit card numbers are integers, but you would never use them in a mathematical calculation, so you could store them either way.


As another example, consider postal codes. If you store them as integers, then you can only support codes from the USA! Other countries use postal codes with letters in them (e.g., BL1 1AD is in the UK).

Boolean Variables

Boolean variables are used to store the value TRUE or FALSE (yes or no).


Booleans are most frequently used to control whether certain functionality is enabled or disabled.


For example, back on Amazon.com, if your login credentials validate, this may be stored as a "true" (yes). As a result, your account information displays, your cart is populated, etc.

Storage and Memory

You may be asking yourself how exactly the computer "stores" these variables. As anyone who's shopped for a computer knows, there's a lot of terms when it comes to computer storage and memory. Let's briefly touch on them.

Memory

ROM or Read-Only Memory is for the most part not editable.


It's used by the computer to store important pieces of data when switched on, like data about the operating system.

Memory

RAM or Random Access Memory is where the computer does its "thinking".


This is where programs temporarily store data like variables while executing programs. Data in RAM is frequently overwritten and is destroyed when you turn off your computer.

If your program needs to save data for later use, you may need to make use of long-term storage.

Storage

Storage is a more permanent type of memory. The computer user can utilize it to keep data around even after the computer is turned off.


Examples include the hard drive on the computer, a thumb drive, or an external hard drive.

Files and databases can be saved in a computer's storage.

Hello, World! - Semantics

Colorless green ideas sleep furiously.

Noam Chomsky

It is possible to create an English sentence that is grammatically correct but meaningless or self-contradictory. The same is true of programming languages.

In coding, semantics refers to the meaning of the symbols you use.

Semantic mistakes may cause your code to throw run time errors.

Operators

Now that we have variables storing data for us, what can we do with them?


We use operators to compare, combine, or evaluate combinations of variables in order to produce some desired output. We could then store this output in another variable, if we wanted!

Operators

Some examples of operators:


  • = or == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)
  • and
  • or

If/Then/Else

If/Then/Else statements evaluate a condition and take actions based on the result.


If the given condition is true, Then the computer will do the actions you specify in the If clause. (In some languages, the word "then" is implied.)


Else (otherwise), if the given condition is false, the computer will do the actions you specify in the Else clause.

If/Then/Else

Let's practice If/Then/Else in the language Ruby.


Let's go back to repl.it and choose the language "Ruby" under "Popular" languages.

If/Then/Else

Copy or type the following into the left-hand side and click the "play" button:


cart_total = 8

if cart_total > 2
    print "You get free shipping!"
end
				
  • Change the numbers and see what happens.
  • Change the operators (greater than, less than, etc.) and see what happens

If/Then/Else

Let's make this a little more complicated and add an "else" statement after the "if" statement!


cart_total = 8

if cart_total > 2
    print "You get free shipping!"
else
    print "You'll have to pay for shipping."
end
				
  • Change the numbers and see what happens.
  • Change the operators (greater than, less than, etc.)
  • Change the string outputs.

Loops

A loop is a list of instructions that repeats until a certain condition is met.


For example:

  • In real life: As long as it's raining, use an umbrella.
  • On Ebay.com: Allow bids on this item until the auction timer hits 0.


One reason to use a loop is to reduce the number of lines of code you need to write to accomplish a task.

Loops

Important: Beware the infinite loop!


An infinite loop is a loop that will never meet the condition to stop. For example, if you're waiting for a variable (like that ebay timer) to reach 0, but the timer variable keeps *increasing* instead of *decreasing*, the loop will continue forever!


Your program will keep running until it uses up all your computer's RAM memory. This is (generally) BAD!

An exception to this rule is a worker program, which may run constantly so that it can listen for certain kinds of input and respond without being "turned on".

Loops

There are two kinds of loops:

  • While loops
  • For loops

While loops

A while loop allows code to be executed repeatedly based on a given condition, often based on the value of a variable. Usually your code will need to change the variable within the body of the loop.



number_of_bottles = 99

while number_of_bottles > 0:
	print number_of_bottles
	print ' bottles of beer on the wall'
	number_of_bottles = number_of_bottles - 1
print 'no more bottles of beer!'
				

Let's try this in repl.it. This time, choose the language "Python" under "Popular".

While loops

Things to try:

  • Change the numbers to see what happens.
  • Change the operators (greater than, less than, etc.)
  • Change the string statements.


Careful not to create an infinite loop! If you do, you may need to force-quit your browser!

For loops

For loops are very similar to while loops. They just have a different syntax. In a for loop, you set up the conditional AND increment or decrement your variable in the same line.


Redoing our last example with a for loop, again in Python:


for number_of_bottles in range(99, 0, -1):
	print number_of_bottles
	print ' bottles of beer on the wall'
print 'no more bottles of beer!'
				

Comments

Some programmers like to leave comments in their code. Comments are not executed by the program. They're just there for you (and other programmers) to read.


Comments should explain why your code is doing what it's doing.


For example, in PHP, comments are preceded by either a # or a //.


# The following code prints something
print 'hello, world!';
				

Hello, World! - Style

Code with style! (Just like everybody else).

Style describes sets of rules that coders apply to their code. These rules can help to make the code more readable, prevent errors, and enforce consistency.

Linters are tools that know about these rules and can check your code for consistency with them. Coding teams use linters to ensure code in their projects meets certain standards.

Style mistakes may cause your code to throw no errors at all! (But may annoy your teammates).

Front-end vs. Back-end

These terms are often used in describing types of web developers.

You may see them in job postings or course descriptions.

They correspond roughly to client-side and server-side development.

Front-end development

Front-end developers work with the part of a web application you can see and interact with. In general this is what we think of as the web page.

Front-end developers may do some design as well as coding.

Technologies: HTML, CSS, Javascript, JQuery, Dreamweaver, Photoshop

Back-end development

Back-end developers work with the part of a web application that stores and processes data. They will often write code to manipulate the database or interact with other systems outside of the web site.

Technologies: PHP, Ruby, Node, SQL

What we learned today

  • What is a computer program?
  • Variables
  • Memory and Storage
  • Logical Operators
  • If/Then/Else Statements
  • Loops
  • Comments
  • Syntax vs. Semantics vs. Style
  • Front-end vs. Back-end development

Coding Classes Resources (most are free) and Books

Thank you for attending!

Please help us make this class better by completing this short survey:


http://bit.ly/programming-concepts-survey