Module 1: Programming Foundations of

Post on 26-Dec-2021

4 views 0 download

Transcript of Module 1: Programming Foundations of

WELCOME TO GAGENERAL ASSEMBLY

Foundations of Programming

Module 1:

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

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.

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

WELCOME TO GAGENERAL ASSEMBLY

Foundations of Programming

Computational Thinking

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

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.

8 | © 2021 True Digital Academy

Karel in 2D world

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.)

10 | © 2021 True Digital Academy

Our First Program

11 | © 2021 True Digital Academy

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

Our First Program

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

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Function

14 | © 2021 True Digital Academy

Karel moves a beeper to a ledge

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()

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

17 | © 2021 True Digital Academy

“Function

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

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()

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()

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

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

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

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

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

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Decomposition

26 | © 2021 True Digital Academy

Karel repairs the road

27 | © 2021 True Digital Academy

Solution

def main(): move()

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

28 | © 2021 True Digital Academy

“Decomposition

breaking a large problem into smaller pieces

29 | © 2021 True Digital Academy

Decomposition

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

30 | © 2021 True Digital Academy

Decomposition

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

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

32 | © 2021 True Digital Academy

“Subroutine

subprogram that may be repeatedly called

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()

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()

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()

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()

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()

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.

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Control Flow: For loop

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.

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

42 | © 2021 True Digital Academy

What does program do?

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

43 | © 2021 True Digital Academy

What does program do?

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

44 | © 2021 True Digital Academy

What does program do?

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

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()

46 | © 2021 True Digital Academy

What does program do?

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

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

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Control Flow: While loop

49 | © 2021 True Digital Academy

Put Beepers inside except left and right corner

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

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

51 | © 2021 True Digital Academy

While loop demo

def main(): move_to_wall()

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

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

53 | © 2021 True Digital Academy

“Boolean

True or False values

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?

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

56 | © 2021 True Digital Academy

Put beepers inside except left and right corner

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

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()

58 | © 2021 True Digital Academy

“Abstraction

focusing on the important information only, ignoring irrelevant detail

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

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Control Flow: If-Statement

61 | © 2021 True Digital Academy

What happens if Karel is moved forward?

● Check before run

beeper_line2_crash.py

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

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

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

65 | © 2021 True Digital Academy

Invert beepers demo

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

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

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Boolean Logic

69 | © 2021 True Digital Academy

Problem: Remove barrier (pick beepers)

70 | © 2021 True Digital Academy

How to solve?

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

def remove_fence(): pass

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

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()

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

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

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.

WELCOME TO GAGENERAL ASSEMBLY

Computational Thinking:

Solving a Programming Problem

77 | © 2021 True Digital Academy

How to do when face difficult problems

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

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

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

81 | © 2021 True Digital Academy

Pseudocode

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

Jump across and put beeper

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()

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

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

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

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

WELCOME TO GAGENERAL ASSEMBLY

THANK YOU!

See you next time!