1 of 85

Everything You Need to Code, You Learned in Kindergarten

Special thanks to our slide sponsor:

Join the conversation with #ONA17

2 of 85

Your Spirit Guides

Lindsey�COOK

Digital Skills Editor�The New York Times

@Lindzcook

Ashlyn�STILL

Graphics Editor�Reuters

@Ashlynstill

3 of 85

What is Programming?

4 of 85

Programming Concepts

  • Algorithm: A set of instructions

5 of 85

Warm-Up

  • Pair up with 1 partner
  • Draw a triangle, a square and a circle on a sheet of paper
    • Each can be shaded or unshaded
    • The shapes vary in size
    • Can be in any location (overlapping, inside each other, etc)
    • DON’T let your partner see

6 of 85

7 of 85

Warm-Up

  • Now instruct your partner on how to draw your shape
    • They can’t look at what you drew
    • You can’t look at what they are drawing

8 of 85

What worked? What didn’t?

9 of 85

Object 1:

Shape: Circle

Width: 2 inches

Shaded? Yes

Coordinates: (3, 3)

Object 2:

Shape: Square

Width: 4 inches

Shaded? No

Coordinates: (1, 2), (1, -2), (4,2), (4,-2)

Object 3:

Shape: Triangle

Width: 4 inches

Shaded? Yes

Coordinates: (-4,-1), (-3,5), (1.2, 0)

10 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name.
    • Example: Name = “Lindsey”

11 of 85

Object 1:

Shape = “Circle”

WidthInInches = 2

Shaded = TRUE

Coordinates = [3, 3]

Object 2:

Shape = “Square”

WidthInInches = 4

Shaded = FALSE

Coordinates: = [[1, 2], [1, -2], [4,2], [4,-2]]

Object 3:

Shape = “Triangle”

WidthInInches = 4

Shaded = TRUE

Coordinates = [[-4,-1], [-3,5], [1.2, 0]]

12 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name, has a type.
    • Name = “Lindsey” String (ie text)
    • DaysofONA = 3 Numbers
    • HavingFunYet = TRUE Boolean (True or False)
    • PeopleHere = [“Lindsey”, “Ashlyn”] Array (collection, has length)

13 of 85

Object 1:

Shape = “Circle” String

WidthInInches = 2 Number

Shaded = TRUE Boolean

Coordinates = [3, 3] Array

Object 2:

Shape = “Square” String

WidthInInches = 4 Number

Shaded = FALSE Boolean

Coordinates: = [[1, 2], [1, -2], [4,2], [4,-2]]

Object 3:

Shape = “Triangle” String

WidthInInches = 4 Number

Shaded = TRUE Boolean

Coordinates = [[-4,-1], [-3,5], [1.2, 0]] Array

14 of 85

More Practice With Variables

15 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name and type.
    • String: Text value; Stored in quotes
    • Number: Can be stored as whole number (integer) or decimal (float)
    • Boolean: True or False values
    • Array: A collection of elements. Can be different types or the same type. Has a length.

16 of 85

You are an ONA Object.

17 of 85

More Practice With Variables

ONA Objects Have Four Variables:

  • Name (String)
  • YearsAtJob (Number)
  • FirstTimeAtONA (Boolean)
  • NamesofColleaguesHere (Array)

18 of 85

More Practice With Variables

ONA Object

  • Name = “Lindsey”
  • YearsAtJob = 0
  • FirstTimeAtONA = FALSE
  • NamesofColleaguesHere = [“Charo”, “Eric”]

19 of 85

More Practice With Variables

ONA Object

  • Name = “Ashlyn”
  • YearsAtJob = 1
  • FirstTimeAtONA = FALSE
  • NamesofColleaguesHere = []

20 of 85

Together, we are an Array of these ONA Objects.

21 of 85

More Practice With Variables

ThisRoom = [ person1, person2, person3, person 4, person5, person6, person7]

22 of 85

More Practice With Variables

ThisRoom = [ person1, person2, person3,

person 4, person5, person6, person7]

Programs start counting at 0.

0

1

2

3

4

5

6

23 of 85

ONAObject[0].Name

Who’s number 1?

24 of 85

More Practice With Variables

ONAObject[2].Name

ONAObject[15].Name

25 of 85

More Practice With Variables

ONAObject[8].Name

ONAObject[3].Name

26 of 85

More Practice With Variables

ONAObject[28].Name

ONAObject[42].Name

27 of 85

More Practice With Variables

ONAObject[48].YearsatJob

ONAObject[35].YearsatJob

28 of 85

More Practice With Variables

ONAObject[12].YearsatJob

ONAObject[7].YearsatJob

29 of 85

More Practice With Variables

ONAObject[22].FirstTimeatONA

ONAObject[39].FirstTimeatONA

30 of 85

More Practice With Variables

ONAObject[44].FirstTimeatONA

ONAObject[49].FirstTimeatONA

31 of 85

More Practice With Variables

Arrays coming, put your thinking cap on!

32 of 85

More Practice With Variables

ONAObject[4].NamesofColleaguesHere

ONAObject[19].NamesofColleaguesHere

33 of 85

More Practice With Variables

ONAObject[34].NamesofColleaguesHere[1]

ONAObject[21].NamesofColleaguesHere[1]

34 of 85

More Practice With Variables

ONAObject[26].NamesofColleaguesHere[0]

ONAObject[1].NamesofColleaguesHere[0]

35 of 85

More Practice With Variables

36 of 85

Red Light, Green Light

37 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name. Can be type string, number, boolean or array
  • Conditional: Performs a different action based on if a boolean value evaluates to true or false

38 of 85

Conditional

Performs a different action based on if a boolean value evaluates to true or false

If someCondition is True:

Do something

If someCondition is False:

Do something else (or do nothing)

39 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

Paces = 0

#create a new variable named Paces and set it to #zero. This is a comment! Meaning you can see it but #the computer knows it isn’t code

40 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(FirstTimeatONA)

Paces = Paces + 2

Translation: Set Paces equal to whatever Paces is now, plus 2.

41 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(Name is “Eric”)

Paces = Paces - 1

42 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(Name is “Ashlyn”)

Paces = Paces - 1

Else

Paces = Paces + 3

43 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(YearsAtJob > 3)

Paces = Paces + 3

44 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(YearsAtJob is 1)

Paces = Paces * 1

45 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(NamesofColleaguesHere.length > 3)

Paces = Paces - Name.length

Else

Paces = Paces + Name.length

46 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(Name.length > 5)

Paces = Paces + 5

47 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(Name.length > 5)

Paces = Paces + 5

Else if (Name.length is 6)

Paces = Paces + 6

48 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(Name.length > 7)

Paces = Paces + 7

Else if (Name.length is 8)

Paces = Paces + 8

Else if (Name.length < 5)

Paces = Paces + 4

49 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(Name.length > 7)

Paces = Paces + 7

Else if (Name.length is 8)

Paces = Paces + 8

Else if (Name.length < 5)

Paces = Paces + 4

Else

Paces = Paces - 4

50 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name. Can be type string, number, boolean or array
  • Conditional: Performs a different action based on if a boolean value evaluates to true or false

51 of 85

Conditionals: Red Light, Green Light

For all items in ONAObject:

If(YearsAtJob is 1)

Paces = Paces * 1

52 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name. Can be type string, number, boolean or array
  • Conditional: Performs a different action based on if a boolean value evaluates to true or false
  • Loop: Repeat a block of code while a condition is true or a certain number of times

53 of 85

Loops: Red Light, Green Light

For all items in ONAObject:

If(YearsAtJob is 1)

Paces = Paces + 1

If (ONAObject[0].YearsAtJob is 1)

ONAObject[0].Paces = ONAObject[0].Paces + 1

If (ONAObject[1].YearsAtJob is 1)

ONAObject[1].Paces = ONAObject[1].Paces + 1

If (ONAObject[2].YearsAtJob is 1)

ONAObject[2].Paces = ONAObject[2].Paces + 1

54 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name. Can be type string, number, boolean or array
  • Conditional: Performs a different action based on if a boolean value evaluates to true or false
  • Loop: Repeat a block of code while a condition is true or a certain number of times

55 of 85

Let’s Write a News Story

56 of 85

Let’s Write a News Story

57 of 85

Let’s Write a News Story

58 of 85

59 of 85

Let’s Write a News Story

60 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name. Can be type string, number, boolean or array
  • Conditional: Performs a different action based on if a boolean value evaluates to true or false
  • Loop: Repeat a block of code while a condition is true or a certain number of times
  • Function: A collection of computer statements that achieve a specific purpose in an overall program. Can be called many times when writing code. Takes in variables.

61 of 85

Let’s Write a News Story

WriteNewsStory(variables here!){

#Code here

}

62 of 85

What Variables Do You Need?

63 of 85

Let’s Write a News Story

WriteNewsStory(variables here!){

#Code here

}

64 of 85

Let’s Write a News Story

First Paragraph: Macy’s Inc. (M) on Wednesday reported fiscal third-quarter net income of $118 million.

65 of 85

Let’s Write a News Story

First Paragraph: Macy’s Inc. (M) on Wednesday reported fiscal third-quarter net income of $118 million.

66 of 85

Let’s Write a News Story

First Paragraph: Macy’s Inc. (M) on Wednesday reported fiscal third-quarter net income of $118 million.

Variables needed: Company (string), abbreviation (string), day (string), quarter (number/string), netIncome (Number)

67 of 85

Let’s Write a News Story

WriteNewsStory(variables here!){

#Code here

}

68 of 85

Let’s Write a News Story

WriteNewsStory(Company, abbreviation, day, quarter, netIncome){

NewsStory = Company + “ “ + abbreviation + “ on ” + day + “ reported fiscal ” + quarter + “- quarter net income of $” + netIncome + “ million .”

}

Macy’s Inc. (M) on Wednesday reported fiscal third-quarter net income of $118 million.

69 of 85

Let’s Write a News Story

WriteNewsStory(Company, abbreviation, day, quarter, netIncome){

NewsStory = Company + “ “ + abbreviation + “ on ” + day + “ reported fiscal ” + quarter + “- quarter net income of $” + netIncome + “ million .”

}

Apple Inc. (AAPL) on Thursday reported fiscal second-quarter net income of $871 million.

70 of 85

Let’s Write a News Story

WriteNewsStory(Company, abbreviation, day, quarter, netIncome){

NewsStory = Company + “ “ + abbreviation + “ on ” + day + “ reported fiscal ” + quarter + “- quarter net income of $” + netIncome + “ million .”

}

Home Depot Inc. (HD) on Friday reported fiscal fourth-quarter net income of $30 million.

71 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name. Can be type string, number, boolean or array
  • Conditional: Performs a different action based on if a boolean value evaluates to true or false
  • Loop: Repeat a block of code while a condition is true or a certain number of times
  • Function: A collection of computer statements that achieve a specific purpose in an overall program. Can be called many times when writing code. Takes in variables.
  • Pseudocode: An outline of your computer program. A high level and human-readable look of an algorithm that doesn’t follow any one language

72 of 85

Let’s Write a News Story

function WriteNewsStory(Company, abbreviation, day, quarter, netIncome){

//function in Javascript

var NewsStory = Company + “ “ + abbreviation + “ on ” + day + “ reported fiscal ” + quarter + “- quarter net income of $” + netIncome + “ million .”;

}

73 of 85

Let’s Write a News Story

def WriteNewsStory(Company, abbreviation, day, quarter, netIncome):

# function in Python

NewsStory = Company + “ “ + abbreviation + “ on ” + day + “ reported fiscal ” + quarter + “- quarter net income of $” + netIncome + “ million .”

74 of 85

Programming Concepts

  • Algorithm: A set of instructions
  • Variable: Stores information, has a name. Can be type string, number, boolean or array
  • Conditional: Performs a different action based on if a boolean value evaluates to true or false
  • Loop: Repeat a block of code while a condition is true or a certain number of times
  • Function: A collection of computer statements that achieve a specific purpose in an overall program. Can be called many times when writing code. Takes in variables.
  • Pseudocode: An outline of your computer program. A high level and human-readable look of an algorithm that doesn’t follow any one language
  • Syntax: The expected grammar of specific programming languages

75 of 85

Let’s Write a News Story

76 of 85

Let’s Write a News Story

WriteNewsStory(variables here!){

#Code here

}

77 of 85

Fizz Buzz Test

78 of 85

Fizz Buzz Test

  • Print Out Numbers 1-100
  • For multiples of 3, print Fizz instead of the number
  • For multiples of 5 print Buzz instead of the number
  • For numbers that are multiples of both 5 and 3, print FizzBuzz

79 of 85

Fizz Buzz Test: Hints

For number 1-100 (loop)

//write conditionals here!

  • Assume you can “print” with print
  • Think about the order of your conditionals. Remember the first one that is “true” wins!
  • Want to go all the way to code? Another hint: % is an extra math operator that means there is no remainder if you divide (aka is divisible by)
    • 15 % 5 = 0
    • 15 % 3 = 0
    • 15 % 2 = 1

80 of 85

Fizz Buzz Test

For number 1-100 (loop)

If number divisible by 3 AND divisible by 5

Print FizzBuzz

Else if number divisible by 3

Print Buzz

Else if number divisible by 5

Print Fizz

Else

Print number

81 of 85

for myNumber in xrange(1,101):

if myNumber % 5 == 0 and myNumber % 3 == 0:

print “FizzBuzz”

elif myNumber % 3 == 0:

print “Buzz”

elif myNumber % 5 == 0:

print “Fizz”

else:

print myNumber

82 of 85

Where Do I Go From Here?

83 of 85

84 of 85

Where Do I Go From Here?

85 of 85

We Support

  • Code Academy
  • Start with a Project
  • Join the News Nerdery Slack channel
  • Make friends with a coder in your organization