Skip to content

Instantly share code, notes, and snippets.

@steele-ntwrk
Last active September 18, 2021 07:04
Show Gist options
  • Select an option

  • Save steele-ntwrk/b5e0875bec5e3f93bd000d5c98ba9587 to your computer and use it in GitHub Desktop.

Select an option

Save steele-ntwrk/b5e0875bec5e3f93bd000d5c98ba9587 to your computer and use it in GitHub Desktop.
Control flow notes

Devnet - Programming Basic Notes

Control Flow

Control flow aims to provide a structure/order to programming assists with keeping two key principles

  1. DRY - Don't Repeat Yourself
  2. KISS - Keep It Simple Stupid

Loops and Iteration

Loops allw for code to be excuted multiple times in succession

  • Commonly used for user interation portions of a program are based inside of a loop.
  • Loops can be nested and conditionals can be nested as well

Caution dont get stuck in a INFINITE loop

  • Create a break in your loop definition.
  • Define breakout

Functions and Methods

Functions

Functions allow for a block of code to be executed on demand during run time.

Key Comptents of a function:

  • "def" defines a function
  • Proceeding txt/symb is it's name
  • Variables can be passed in to a function by using "()"

Example of function:

def revivePlayer(p):  
    if p.health > 0:  
    print(p + "has health, they dont need to be revived")  

Example of function being invoked:

revivePlayer(player1)  

Methods

Methods are similar to functions and are just defined/nested under Classes. Classes can be used to create individual objects.

When a method is envoked it will reference the object instance they are being envoked from.

Key Compotents of Method:

  • Same as Functions but need a class defined.

Example of Method:

class Player:
        Health = 10
        Revives = 3

        def revive(self):
            if self.health > 0:
                print("Player has health, they do no need to be revived")
                return
            elif self.revives < 0:
                print("This player is out of revives, sorry :(")
                return

            self.revives = self.revives - 1
            self.health = 10

Example of method being invoked:

player1.revive()
player2.revive()

Object-Oriented Programming (OOP)

Object-Oriented Programming is an architectural style supported by many programming languages. The key to the OOP architect is is the use of 'Objects', this supports key progamming principles DRY and KISS

Key Benifits of Object-Oriented Programming:

  • Obejects can be created, deleted and manipulated in isolation of each other.
  • Maintainable, code that can easily fixed or updated over time.
  • Debuggable, code that can easily be ioslated for testing/fixing
  • Testable, enables ability to testable
  • Ledgible, needs to be able to be supported by other humans.

Class State Behaviour

Classes are like blueprints for objects and define what charcteristics objects will have, these blueprints or Classes can then be used to substantiate an Object.

Classes are made up of two compontents, state and behavior.

State are charcteristics/conditions/variables of an object.

Behavior are functions that can execute within a class. A common function is "init" which is commonly referred to as a constructor, it's used to create an object/state and its variables.

Example of Converting code to Classes and implementing KISS DRY.

circle1Id = 1
circle1radius = 10
circle1colout = "Blue"
circle1display = True

circle2Id = 1
circle2radius = 8
circle2colout = "Yellow"
circle2display = True

circle3Id = 1
circle3radius = 17
circle3colout = "Red"
circle3display = False

circle1circumfrence = str(3.141 * 2 * circle1radius)
circle2circumfrence = str(3.141 * 2 * circle2radius)
circle3circumfrence = str(3.141 * 2 * circle3radius)

print("Circle 1 circumfrence is: " + circle1circumfrence)
print("Circle 2 circumfrence is: " + circle1circumfrence)
print("Circle 3 circumfrence is: " + circle1circumfrence)

Converted to Class

class Circle:
    Id = 0
    radius = 0
    colour = ""
    display = True
# ^^^^ State

    def __init__(self,id,r,c,d):
        self.id = id
        self.radius = r
        self.colour = c
        self.display = d

    def circumfrence(self):
        return 3.141 * 2 * self.radius
# ^^^^ Behavior

circle1 = Circle(1,10,"Blue",True)
circle2 = Circle(2,82,"Yelow",True)
circle3 = Circle(3,24,"Red",False)

print("Circle 1 circumfrence is: " + circle1.circumfrence())
print("Circle 2 circumfrence is: " + circle2.circumfrence())
print("Circle 3 circumfrence is: " + circle3.circumfrence())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment