88
WELCOME TO GA GENERAL ASSEMBLY Foundations of Programming Module 1:

Module 1: Programming Foundations of

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Foundations of Programming

Module 1:

Page 2: Module 1: Programming Foundations of

2 | © 2021 True Digital Academy

Modules of this course

Module 1: Foundations of Programming

Module 6:EDA with Pandas

Module 2: Introduction to

Python

Module 3: Python Data Structures

Module 5:Introduction to

Pandas

Module 4: Intermediate

Python

Page 3: Module 1: Programming Foundations of

3 | © 2021 True Digital Academy

“Computer Programming

The process of designing and building an executable computer program to accomplish a specific computing result or to perform a specific task.

Page 4: Module 1: Programming Foundations of

4 | © 2021 True Digital Academy

Module 1: Lesson Objectives

● Describe the skills that are involved in computational thinking● Understand the four components of computational thinking

○ Decomposition○ Pattern Recognition○ Abstraction○ Algorithms

● Write basic programs to solve problems using the concept of computational thinking

Page 5: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Foundations of Programming

Computational Thinking

Page 6: Module 1: Programming Foundations of

6 | © 2021 True Digital Academy

Computational Thinking

● Decomposition○ breaking down a complex problem or system

into smaller, more manageable parts● Abstraction

○ focusing on the important information only, ignoring irrelevant detail

● Pattern Recognition○ looking for similarities among and within

problems● Algorithms

○ developing a step-by-step solution to the problem, or the rules to follow to solve the problem

Page 7: Module 1: Programming Foundations of

7 | © 2021 True Digital Academy

Karel the Robot

Talk with Karel in Python language to learn the Introduction to Python

Karel is a very simple robot living in a very simple world.

Page 8: Module 1: Programming Foundations of

8 | © 2021 True Digital Academy

Karel in 2D world

Page 9: Module 1: Programming Foundations of

9 | © 2021 True Digital Academy

Instructions for Karel

move() Asks Karel to move forward one block. If there is a wall, Karel will be crash and turn off.

turn_left() Ask Karel to rotate 90 degrees to the left (counterclockwise).

put_beeper() Put down a beeper from the current square. (There must be at least one beeper

in bag, otherwise it will turn off.)

pick_beeper() Pick up a beeper from the current square. (There must be a beeper at Karel's

current location, otherwise it will turn off.)

Page 10: Module 1: Programming Foundations of

10 | © 2021 True Digital Academy

Our First Program

Page 11: Module 1: Programming Foundations of

11 | © 2021 True Digital Academy

move()pick_beeper()turn_left()move()turn_left()move()put_beeper()

Our First Program

Page 12: Module 1: Programming Foundations of

12 | © 2021 True Digital Academy

Quick Review: Programming Principles

● Programming is the process of creating a set of instructions with computer programming languages to tell a computer how to perform a task.

○ what the purpose is○ how to arrange it to achieve the goal

Page 13: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Function

Page 14: Module 1: Programming Foundations of

14 | © 2021 True Digital Academy

Karel moves a beeper to a ledge

Page 15: Module 1: Programming Foundations of

15 | © 2021 True Digital Academy

Solution

def main(): move() pick_beeper() move() turn_left() move() turn_left() turn_left() turn_left() move() move() put_beeper() move()

Page 16: Module 1: Programming Foundations of

16 | © 2021 True Digital Academy

Solution

def main(): move() pick_beeper() move() turn_left() move() turn_left() turn_left() turn_left() move() move() put_beeper() move()

Turn right

Page 17: Module 1: Programming Foundations of

17 | © 2021 True Digital Academy

“Function

A function is a group of statements that together perform a task.

Page 18: Module 1: Programming Foundations of

18 | © 2021 True Digital Academy

Defining a Function

def main(): move() pick_beeper() move() turn_left() move() turn_right() move() move() put_beeper() move()

def turn_right(): turn_left() turn_left() turn_left()

Page 19: Module 1: Programming Foundations of

19 | © 2021 True Digital Academy

Create Indentation by using TAB key

def main(): move() pick_beeper() move() turn_left() move() turn_right() move() move() put_beeper() move()

def turn_right(): turn_left() turn_left() turn_left()

Page 20: Module 1: Programming Foundations of

20 | © 2021 True Digital Academy

Don’t forget colon : and parentheses ()

def main(): move() pick_beeper() move() turn_left() move() turn_right() move() move() put_beeper() move()

def turn_right(): turn_left() turn_left() turn_left()

Otherwise, you’ll encounter a

SyntaxError: invalid syntax

Page 21: Module 1: Programming Foundations of

21 | © 2021 True Digital Academy

Keywords

def main(): move() pick_beeper() move() turn_left() move() turn_right() move() move() put_beeper() move()

def turn_right(): turn_left() turn_left() turn_left()

function header

Page 22: Module 1: Programming Foundations of

22 | © 2021 True Digital Academy

Keywords

def main(): move() pick_beeper() move() turn_left() move() turn_right() move() move() put_beeper() move()

def turn_right(): turn_left() turn_left() turn_left()

function header

function definition

code block

Page 23: Module 1: Programming Foundations of

23 | © 2021 True Digital Academy

Keywords

def main(): move() pick_beeper() move() turn_left() move() turn_right() move() move() put_beeper() move()

def turn_right(): turn_left() turn_left() turn_left()

function definition

The main function calls turn_right function

function header

Page 24: Module 1: Programming Foundations of

24 | © 2021 True Digital Academy

def turn_right(): turn_left() turn_left() turn_left()

Quick Review: Function

● Function is a combining many instructions into a single function call to solve subproblems.

● Function definition includes:○ function header○ code block

function definition

code block

function header

indentation

Page 25: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Decomposition

Page 26: Module 1: Programming Foundations of

26 | © 2021 True Digital Academy

Karel repairs the road

Page 27: Module 1: Programming Foundations of

27 | © 2021 True Digital Academy

Solution

def main(): move()

turn_right()move()put_beeper()turn_right()turn_right()move()turn_right()move()move()move()...

Page 28: Module 1: Programming Foundations of

28 | © 2021 True Digital Academy

“Decomposition

breaking a large problem into smaller pieces

Page 29: Module 1: Programming Foundations of

29 | © 2021 True Digital Academy

Decomposition

def main(): jump_in() put_beeper() jump_out() move() move() jump_in() put_beeper() jump_out()

Page 30: Module 1: Programming Foundations of

30 | © 2021 True Digital Academy

Decomposition

def main(): jump_in() put_beeper() jump_out() move() move() jump_in() put_beeper() jump_out()

Page 31: Module 1: Programming Foundations of

31 | © 2021 True Digital Academy

Decomposition

def main(): fill_hole() move() move() fill_hole()

Precondition = standing in front of the hole

Postcondition = the hole is behind Karel

Page 32: Module 1: Programming Foundations of

32 | © 2021 True Digital Academy

“Subroutine

subprogram that may be repeatedly called

Page 33: Module 1: Programming Foundations of

33 | © 2021 True Digital Academy

fill_hole() subroutine

def main(): fill_hole() move() move() fill_hole()

def fill_hole(): jump_in() put_beeper() jump_out()

Page 34: Module 1: Programming Foundations of

34 | © 2021 True Digital Academy

fill_hole() subroutine

def main(): fill_hole() move() move() fill_hole()

def fill_hole(): jump_in() put_beeper() jump_out()

def jump_in(): move() turn_right() move()

def jump_out(): turn_around() move() turn_right() move()

Page 35: Module 1: Programming Foundations of

35 | © 2021 True Digital Academy

fill_hole() subroutine

def main(): fill_hole() move() move() fill_hole()

def fill_hole(): jump_in() put_beeper() jump_out()

def jump_in(): move() turn_right() move()

def jump_out(): turn_around() move() turn_right() move()

Page 36: Module 1: Programming Foundations of

36 | © 2021 True Digital Academy

fill_hole() subroutine

def main(): fill_hole() move() move() fill_hole()

def fill_hole(): move_L() put_beeper() turn_around() move_L()

def move_L(): move() turn_right() move()

Page 37: Module 1: Programming Foundations of

37 | © 2021 True Digital Academy

fill_hole() subroutine

def main(): fill_hole() move() move() fill_hole()

def fill_hole(): move_L() put_beeper() turn_around() move_L()

def move_L(): move() turn_right() move()

def turn_around(): turn_left() turn_left()

Page 38: Module 1: Programming Foundations of

38 | © 2021 True Digital Academy

Quick Review: Decomposition

● Decomposition - The process of breaking a program down into smaller pieces by writing subroutines.

● Decomposition is the main process of programming because it makes easier to tackle and the code is easier to read and understand.

● Decomposition should be the first process of programming.

Page 39: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Control Flow: For loop

Page 40: Module 1: Programming Foundations of

40 | © 2021 True Digital Academy

“Control Flow

is the order in which instructions, statements and function calls being executed or evaluated when a program is running.

Page 41: Module 1: Programming Foundations of

41 | © 2021 True Digital Academy

For loop: know how many times the loop should run

def turn_around(): turn_left() turn_left()

def turn_around(): for i in range(2): turn_left()

For loop - Syntax

for i in range(number of loops): First instruction Second loop instruction

Page 42: Module 1: Programming Foundations of

42 | © 2021 True Digital Academy

What does program do?

def main(): move() for i in range(15): put_beeper() move()

Page 43: Module 1: Programming Foundations of

43 | © 2021 True Digital Academy

What does program do?

def main(): move() for i in range(15): put_beeper() move()

Page 44: Module 1: Programming Foundations of

44 | © 2021 True Digital Academy

What does program do?

def main(): move() for i in range(5): put_beeper() move()

Page 45: Module 1: Programming Foundations of

45 | © 2021 True Digital Academy

What does program do?

def main(): move() for i in range(5): put_beeper() move()

move()

put_beeper()move()put_beeper()move()put_beeper()move()put_beeper()move()put_beeper()move()

move()

put_beeper()put_beeper()put_beeper()put_beeper()put_beeper()move()move()move()move()move()

Page 46: Module 1: Programming Foundations of

46 | © 2021 True Digital Academy

What does program do?

def main(): move() for i in range(5): put_beeper() move()

Page 47: Module 1: Programming Foundations of

47 | © 2021 True Digital Academy

Quick Review: Control Flow (For loop)

● Uses a for loop when you know how many times the loop should run

● Executes a sequence of statements in each code block

For loop - Syntax

for i in range(number of loops): First loop command Second loop command

Page 48: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Control Flow: While loop

Page 49: Module 1: Programming Foundations of

49 | © 2021 True Digital Academy

Put Beepers inside except left and right corner

def main(): move() for i in range(6): put_beeper() move()

Page 50: Module 1: Programming Foundations of

50 | © 2021 True Digital Academy

While loop - repeat as long as a given condition is TRUE

● for loop - know how many times to repeat● while loop - repeat as long as a given condition is TRUE

Page 51: Module 1: Programming Foundations of

51 | © 2021 True Digital Academy

While loop demo

def main(): move_to_wall()

def move_to_wall(): while front_is_clear(): move()

Page 52: Module 1: Programming Foundations of

52 | © 2021 True Digital Academy

While loop syntax

def move_to_wall(): while front_is_clear(): move()

while loop - Syntax

while (condition that is True/False): First instruction Second instruction

For loop - Syntax

for i in range(number of loops): First instruction Second instruction

Page 53: Module 1: Programming Foundations of

53 | © 2021 True Digital Academy

“Boolean

True or False values

Page 54: Module 1: Programming Foundations of

54 | © 2021 True Digital Academy

Karel's Booleans

Test Opposite What it checks

front_is_clear() front_is_blocked() Is there no wall in front of Karel?

beepers_present() no_beepers_present() Is there a beeper where Karel is standing?

left_is_clear() left_is_blocked() Is there no wall to Karel’s left?

right_is_clear() right_is_blocked() Is there no wall to Karel’s right?

beepers_in_bag() no_beepers_in_bag() Is there a beeper in Karel bag?

facing_north() not_facing_north() Is Karel facing north?

facing_south() not_facing_south() Is Karel facing south?

facing_east() not_facing_east() Is Karel facing east?

facing_west() not_facing_west() Is Karel facing west?

Page 55: Module 1: Programming Foundations of

55 | © 2021 True Digital Academy

While loop syntax

def move_to_wall(): while front_is_clear(): move()

while loop - Syntax

while boolean: First instruction Second instruction

For loop - Syntax

for i in range(number of loops): First instruction Second instruction

Page 56: Module 1: Programming Foundations of

56 | © 2021 True Digital Academy

Put beepers inside except left and right corner

def main(): move() while front_is_clear(): put_beeper() move()

Page 57: Module 1: Programming Foundations of

57 | © 2021 True Digital Academy

Abstraction

def main(): move() while front_is_clear(): put_beeper() move() front_is

_clear?

put_beeper()

move()

end

true

false

move()

Page 58: Module 1: Programming Foundations of

58 | © 2021 True Digital Academy

“Abstraction

focusing on the important information only, ignoring irrelevant detail

Page 59: Module 1: Programming Foundations of

59 | © 2021 True Digital Academy

Quick Review: Control Flow (While loop)

● while loop - Syntax

instruction 1while boolean: instruction2 instruction 3instruction 4

True?

instruction 2

instruction 3

instruction4

true

false

instruction 1

Page 60: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Control Flow: If-Statement

Page 61: Module 1: Programming Foundations of

61 | © 2021 True Digital Academy

What happens if Karel is moved forward?

● Check before run

beeper_line2_crash.py

Page 62: Module 1: Programming Foundations of

62 | © 2021 True Digital Academy

if Diagram

def safe_move():if front_is_clear():

move() front_is_clear?

move()

end

true

false

beeper_line3_safemove.py

Page 63: Module 1: Programming Foundations of

63 | © 2021 True Digital Academy

while vs if

instruction 1while boolean: instruction 2 instruction 3instruction 4 True?

instruction 2

instruction 3

instruction 4

true

false

instruction 1

Page 64: Module 1: Programming Foundations of

64 | © 2021 True Digital Academy

while vs if

instruction 1while if boolean: instruction 2 instruction 3instruction 4 True?

instruction 2

instruction 3

instruction 4

true

false

instruction 1

Page 65: Module 1: Programming Foundations of

65 | © 2021 True Digital Academy

Invert beepers demo

Page 66: Module 1: Programming Foundations of

66 | © 2021 True Digital Academy

if-else

def invert_beeper(): if beepers_present(): pick_beeper() else: put_beeper() beepers_

present?

pick_beeper()

END

put_beeper()

true

false

START

Page 67: Module 1: Programming Foundations of

67 | © 2021 True Digital Academy

Quick Review: If-statement

● Control flow has three types: for, while, if● if choose to run code block inside? but not repeat● if-else choose to run which code block but not repeat

Page 68: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Boolean Logic

Page 69: Module 1: Programming Foundations of

69 | © 2021 True Digital Academy

Problem: Remove barrier (pick beepers)

Page 70: Module 1: Programming Foundations of

70 | © 2021 True Digital Academy

How to solve?

def main(): for i in range(4): remove_fence()

def remove_fence(): pass

Page 71: Module 1: Programming Foundations of

71 | © 2021 True Digital Academy

How to solve?

def main(): for i in range(4): remove_fence()

def remove_fence(): while left_is_blocked(): if beepers_present(): pick_beeper() move()

fence_karel.py

Page 72: Module 1: Programming Foundations of

72 | © 2021 True Digital Academy

OR operator

def main(): for i in range(4): remove_fence()

def remove_fence(): while left_is_blocked() or beepers_present(): if beepers_present(): pick_beeper() move() turn_left() move()

Page 73: Module 1: Programming Foundations of

73 | © 2021 True Digital Academy

boolean logic: OR operation

is_left_blocked() beepers_present()is_left_blocked() orbeepers_present()

True True True

True False True

False True True

False False False

Page 74: Module 1: Programming Foundations of

74 | © 2021 True Digital Academy

boolean logic: AND operation

is_left_blocked() beepers_present()is_left_blocked() andbeepers_present()

True True True

True False False

False True False

False False False

Page 75: Module 1: Programming Foundations of

75 | © 2021 True Digital Academy

Quick Review: Boolean Logic

● Booleans represent one of two values: True or False● Booleans can be used with boolean operators such as AND, OR● Use Booleans in programming to make comparisons and to determine the flow of

control in a given program.

Page 76: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Solving a Programming Problem

Page 77: Module 1: Programming Foundations of

77 | © 2021 True Digital Academy

How to do when face difficult problems

Page 78: Module 1: Programming Foundations of

78 | © 2021 True Digital Academy

● Put Beepers all over the mountains● There is a beeper at the foothill● Mountains come in many sizes.● Have any number of mountains● The world can be any size● Karel starts with lower left corner and end with

lower right corner

Problem: Climbing Mountains

Page 79: Module 1: Programming Foundations of

79 | © 2021 True Digital Academy

Top-down strategy (Top to bottom, Big to small)

1. Write pseudocode and plan all of the functions 2. Write and test each function

a. If it has loop, you will test code block inside the loop first3. Test overall program

Page 80: Module 1: Programming Foundations of

80 | © 2021 True Digital Academy

“Pseudocode

is an informal way of programming description that does not require any strict programming language syntax or underlying technology

Page 81: Module 1: Programming Foundations of

81 | © 2021 True Digital Academy

Pseudocode

while the front is clear Move until find foothill If find beeper

Jump across and put beeper

Page 82: Module 1: Programming Foundations of

82 | © 2021 True Digital Academy

Pseudocode

while the front is clear Move until find foothill If a beeper is found

Jump across and put beeper

def main(): while front_is_clear(): move_to_foothill() if beepers_present(): jump_across()

Page 83: Module 1: Programming Foundations of

83 | © 2021 True Digital Academy

Top-down strategy (Top to bottom, Big to small)

1. Write pseudocode and plan all of the functions 2. Write and test each function

a. If it has loop, you will test code block inside the loop first3. Test overall program

Page 84: Module 1: Programming Foundations of

84 | © 2021 True Digital Academy

Manage function in loop first

def main(): while front_is_clear(): move_to_foothill() if beepers_present(): jump_across()

def main(): move_to_foothill()

hurdle_karel.py

Page 85: Module 1: Programming Foundations of

85 | © 2021 True Digital Academy

Quick Review: Top-down strategy (Top to bottom, Big to small)

● Write pseudocode and plan all of the functions● Write and test each function

○ If it has loop, you will test code block inside the loop first● Test overall program

Page 86: Module 1: Programming Foundations of

86 | © 2021 True Digital Academy

Module Summary

We’ve learned:● Computational thinking

○ decomposition - breaking down a complex problem or system into smaller, more manageable parts

○ pattern recognition – looking for similarities among and within problems○ abstraction – focusing on the important information only, ignoring irrelevant

detail○ algorithms - developing a step-by-step solution to the problem, or the rules to

follow to solve the problem● Python’s control flow - if, for, while● Boolean logic● Top-down strategy

86 | © 2021 True Digital Academy

Page 87: Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

THANK YOU!

See you next time!

Page 88: Module 1: Programming Foundations of