DevNet Associate -Coding Basics
DevNet Associate -Coding Basics
Show Menu
Lab - Software Version Control / Software Development and Design / Coding Basics
3.3.11
with Git Development and
Software
3 Design
3.4 Coding Basics Coding Basics
Methods, Functions, Modules, and
3.4.1
Classes
3.4.1
Clean Code
3.4.2
Methods, Functions, Modules,
and Classes
3.4.3 Methods and Functions
3.4.4 Modules It may seem easy to throw code in a file and make it work.
But as project size and complexity grows, and as other
3.4.5 Classes developers (and stakeholders) get involved, disciplined
methods and best practices are needed to help
developers write better code and collaborate around it
3.4.6 Lab - Explore Python Classes more easily.
Clean Code
Understanding and Using
4
APIs
3.4.5 Classes
3.4.3
Show Menu The most powerful thing about methods and functions is
Lab - Software Version Control
3.3.11 that they can be written once and executed as many
with Git Development and
Software
3 times as you like. If used correctly, methods and functions
Design will simplify your code, and therefore reduce the potential
3.4 Coding Basics for bugs.
3.4.4 Modules
Arguments and Parameters
3.4.3 Methods and Functions The example above is passing this function a string, an
integer (or number), and an object containing a key and a
value.
3.4.4 Modules
Return Statements
3.4.5 Classes
Methods and functions perform tasks, and can also return
a value. In many languages, the return value is specified
3.4.6 Lab - Explore Python Classes
using the keyword return followed by a variable or
expression. This is called the return statement. When a
3.5 Code Review and Testing return statement is executed, the value of the return
statement is returned and any code below it gets skipped.
It is the job of the line of code calling the method or
3.6 Understanding Data Formats function to grab the value of the return, but it is not
mandatory.
Software Development and
3.7
Design Summary
Syntax of a function with a return statement in Python:
Infrastructure and
7 In the above example, the returned value would be the
Automation
string "argument1argument1argument1argument1",
Function Example
Show Menu
Lab - Software Version Control
3.3.11 Let's say your original code looks like this:
with Git Development and
Software
3 Design
# Print the circumference for circles with a
3.4 Coding Basics radius of 2, 5, and 7
radius1 = 2
Methods, Functions, Modules, and radius2 = 5
3.4.1
Classes radius3 = 7
# Formula for a circumference is c = pi *
3.4.2 Clean Code diameter
# Formula for a diameter is d = 2 * radius
pi = 3.14 # (Will hardcode pi in this
3.4.3 Methods and Functions
example)
circumference1 = pi * radius1 * 2
3.4.4 Modules print ("Circumference of a circle with a
radius of " + str(radius1) + " is " +
str(circumference1))
3.4.5 Classes
circumference2 = pi * radius2 * 2
print ("Circumference of a circle with a
3.4.6 Lab - Explore Python Classes radius of " + str(radius2) + " is " +
str(circumference2))
circumference3 = pi * radius3 * 2
3.5 Code Review and Testing
print ("Circumference of a circle with a
radius of " + str(radius3) + " is " +
3.6 Understanding Data Formats str(circumference3))
Software Development and As you can see, there is a lot of duplicate code here.
3.7
Design Summary
Modules
Show Menu
Lab - Software Version Control
3.3.11
with Git Development and
Software
3 Design Modules are a way to build independent and self-
contained chunks of code that can be reused. Developers
3.4 Coding Basics
typically use modules to divide a large project into smaller
parts. This way the code is easier to read and understand,
Methods, Functions, Modules, and and each module can be developed in parallel without
3.4.1
Classes
conflicts. A module is packaged as a single file. In
addition to being available for integration with other
3.4.2 Clean Code modules, it should work independently.
3.4.3 Methods and Functions A module consists of a set of functions and typically
contains an interface for other modules to integrate with.
It is essentially, a library, and cannot be instantiated.
3.4.4 Modules
Module example
3.4.5 Classes
Below is a module with a set of functions saved in a script
called circleModule.py . You will see this script again
3.4.6 Lab - Explore Python Classes later in the lab for this topic.
3.5 Code Review and Testing # Given a radius value, print the
circumference of a circle.
# Formula for a circumference is c = pi * 2
3.6 Understanding Data Formats
* radius
3.4.4 Modules
circle3 = Circle(7)
circle3.printCircumference()
3.4.5 Classes
3.4.5
3.4.2 Clean Code Note: Unlike other OOP languages, in Python, there is no
means of creating 'private' class variables or internal
methods. However, by convention, methods and variables
3.4.3 Methods and Functions
with a single preceding underscore ( _ ) are considered
private and not to be used or referenced outside of the
3.4.4 Modules class.
3.4.5 Classes
3.4.6
Infrastructure and
7
Automation
3.3 3.5
Version Control Sys… Code Review and T…