More README Work
This commit is contained in:
parent
05392f945b
commit
0f49a82db7
@ -1,3 +1,4 @@
|
||||
# 4 - If/Else and Logical Operators - Challenge
|
||||
Build a program that requests the user's age, and return one of the following:
|
||||
- <= 18 - You're a youngin'
|
||||
- > 18 AND <= 35 - The early adult years
|
||||
|
@ -12,26 +12,31 @@ else:
|
||||
|
||||
We can check different things to make different choices, neat. But what do we do if we need to ask a bunch of related questions in our program? That's were elif comes in.
|
||||
|
||||
```
|
||||
if (some condition):
|
||||
(DO STUFF IF CONDITION IS TRUE)
|
||||
elif (some other condition):
|
||||
(DO STUFF IF SOME OTHER CONDITION IS TRUE)
|
||||
else:
|
||||
(DO STUFF IF ALL CONDITIONS (QUESTIONS) ARE FALSE)
|
||||
```
|
||||
|
||||
elif allows use to specify another condition and some code to go with it to add another question to ask if the questions that came before it were false. This gives us more options to determine what we're going to do next.
|
||||
|
||||
## Do I need to use if/elif/else all the time?
|
||||
The smallest form of if is this
|
||||
|
||||
```
|
||||
if (some condition):
|
||||
(DO STUFF IF CONDITION IS TRUE)
|
||||
```
|
||||
|
||||
You don't need else, you don't need elif, unless you want them and it makes sense for what you're trying to do in your program
|
||||
|
||||
## Does the indentation matter?
|
||||
YES! Python is ULTRA PICKY about indentation. In general, when you see a colon (:) in Python, that means that some portion of the code underneath it should be indented one tab more than the current level of indentation. Here's a very bland example (ignore the for loop for the moment, we'll talk about that next lesson)
|
||||
|
||||
```
|
||||
for x in range(10):
|
||||
if x == 5 or x == 7:
|
||||
print("Potato")
|
||||
@ -39,6 +44,7 @@ for x in range(10):
|
||||
print("Grape")
|
||||
else:
|
||||
print("You get nothing")
|
||||
```
|
||||
|
||||
The identation determines something called scope. Scope is essentially defining blocks of code that "live in the same space". Scope can be a complicated topic, so we're going to gloss over it. [W3Schools](https://www.w3schools.com/python/python_scope.asp) does a really good job explaining scope relatively quickly. You can even try the code for yourself right on the webpage!
|
||||
|
||||
@ -57,7 +63,7 @@ Sometimes conditions are as simple as, is this less than that, or is this equal
|
||||
|
||||
AND will result in a True value only when both the left and the right side of the AND are True.
|
||||
|
||||
Example: intValue < 100 and intValue > 50 is only True when intValue is any number between 51 and 99
|
||||
Example: `intValue < 100 and intValue > 50` is only True when intValue is any number between 51 and 99
|
||||
|
||||
The Truth Table for AND looks like this
|
||||
<table>
|
||||
@ -86,4 +92,56 @@ The Truth Table for AND looks like this
|
||||
<td>False</td>
|
||||
<td>False</td>
|
||||
</tr>
|
||||
</table>
|
||||
</table>
|
||||
|
||||
OR will result in a True value so long as at least one side (the left, the right, or both) is True, OR is only False when both sides are False
|
||||
|
||||
Example: `intValue < 0 or intValue > 100` is True when intValue is less than 0 (any negative number) OR intValue is a value of 101 or greater
|
||||
|
||||
The Truth Table for OR looks like this
|
||||
<table>
|
||||
<tr>
|
||||
<th>Left Side</th>
|
||||
<th>Right Side</th>
|
||||
<th>Result</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>True</td>
|
||||
<td>True</td>
|
||||
<td>True</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>True</td>
|
||||
<td>False</td>
|
||||
<td>True</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>False</td>
|
||||
<td>True</td>
|
||||
<td>True</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>False</td>
|
||||
<td>False</td>
|
||||
<td>False</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
One other Logical Operator that doesn't really combine two values is NOT. NOT flips whatever the resulting value is, True becomes False, False becomes True.
|
||||
|
||||
Example: `not intValue < 100` is True when intValue has a value of 100 or greater
|
||||
|
||||
NOT can be a little weird to think about, adding negation to logic can make things a little fuzzy to think about. An actual Python Programming course would cover this more than I can here, but there is one thing to keep in mind when working with logical operations. Like PEMDAS from math, logical operators also have an order of operations, see the formatted list below
|
||||
|
||||
- Parenthesis
|
||||
- NOT
|
||||
- AND
|
||||
- OR
|
||||
|
||||
Parenthesis is at the top, whatever you see in parenthesis will happen first, NOT is evaluated next, then AND, and finally OR. That means if you have something like this:
|
||||
|
||||
`someValue or someOtherValue and thisValue`
|
||||
|
||||
The AND of someOtherValue and ThisValue will happen first, because AND has higher precedence, the result of that will then be combined with OR only after the AND operation has completed. If you wanted OR to come first, you'd have to write it something like this
|
||||
|
||||
`(someValue or someOtherValue) and thisValue`
|
@ -1 +1,2 @@
|
||||
# 5 - Random Numbers and For Loops - Challenge
|
||||
Write a program that requests an integer from the user, and prints that many random numbers AND prints hte sum and average of those numbers.
|
@ -0,0 +1,34 @@
|
||||
# Random Numbers and For Loops
|
||||
|
||||
## What is a For Loop, and why do I care?
|
||||
There are times where you're going to want to repeat a task or section of code in your program a certain number of times. For Loops take care of this for us. We create a for loop structure like this
|
||||
|
||||
```
|
||||
for indexVariable in range(someNumber):
|
||||
DO SOME STUFF
|
||||
```
|
||||
|
||||
The indexVariable element stores the current loop iteration number, or index. The range() function creates a sequence of numbers that we can use from 0 to someNumber-1. i.e. If you said range(5), you'd get a list of numbers 0, 1, 2, 3, 4.
|
||||
|
||||
## Can you tell me more about range?
|
||||
There are different types of "ranges" but range() is pretty common to see when first starting out. Range isn't limited to just creating 0..N-1 ranges, you can do more stuff with it.
|
||||
|
||||
Lets say you wanted the range to start with 1 instead of 0. You can say range(1, 6), and this will give you a list of numbers 1, 2, 3, 4, 5.
|
||||
|
||||
Lets say you wanted to count by 2's instead of by 1. You can say range(0, 5, 2), and this will give you a list of numbers, 0, 2, and 4.
|
||||
|
||||
You can even count backwards. range(4, -1, -1) will give you a list of numbers 4, 3, 2, 1 and 0.
|
||||
|
||||
You'll have to decide what goes into range() when you are writing code for whatever it is your doing.
|
||||
|
||||
## Random numbers, why do I need those?
|
||||
For Robot Programming, you probably don't. It's pretty rare for a use case to show up where a robot needs to do something based on random input, as it generally isn't very safe to do something like that.
|
||||
|
||||
Random makes for easy examples when working with For Loops, which is why it's included here, and in general programming, it's seen a lot of different places, cryptography and securing communications and data especially. For these examples, it just makes for a nice talking point for how loops can be used to repeat a process several times over.
|
||||
|
||||
## Why do we "import random"?
|
||||
Not everything is available to you all at the same time in Python. Some stuff is in seperate packages called "Modules" that extend the functionality of the base language. In order to work with the extra stuff in the Modules we want to use, we have to "announce" with an import statement that we want to use the module.
|
||||
|
||||
Some modules (like random) come with Python can don't need to be installed, other modules, like RobotPy and its assoicated pieces, need to be installed, usually through the command line utility pip, before they can be added in your program.
|
||||
|
||||
On top of importing separate modules, you can import additional code that exists within your own project. We'll see some of this further down the line in other lessons, but for now, just know that importing becomes much more important as your programs (whether they be for robots or not) become larger and more complex.
|
@ -1 +1,2 @@
|
||||
# 6 - While Loops, Break/Continue, and Loop Translation - Challenge
|
||||
Write a program that generates a random number, and repeatedly asks the user to guess the number, telling them if they're too low, too high, or if they guess correctly. The program should end once they guess the correct number.
|
@ -0,0 +1,63 @@
|
||||
# 6 - While Loops, Break/Continue, and Loop Translation
|
||||
|
||||
## I just learned about For Loops, what the heck is a While Loop?
|
||||
For Loops are great when you "know" the number of times you need to repeat something in Python (or any language). You have a finite number of steps that need to be repeated, so you do those things a certain number of times and then you're done.
|
||||
|
||||
While loops, on the other hand, repeat indefinitely until something is no longer true. A While Loop could go on "forever" if you let it (although generally this is a bad thing, more on that later).
|
||||
|
||||
While Loops are structured like this:
|
||||
|
||||
```
|
||||
while someConditionIsTrue:
|
||||
DO STUFF
|
||||
```
|
||||
|
||||
Each time you run through the While Loop, the condition at the top is re-checked to see if it's still true, if it is, the code runs again, and again, and again, until that condition at the top is False, at which point, the while loop exits, and the code is no longer repeated.
|
||||
|
||||
## What are we breaking or continuing?
|
||||
Break and Continue are keywords that allow us to change the behavior of loops in our programs under certain circumstances we define, usually with if statements.
|
||||
|
||||
Continue can be used to jump back to the top of the loop. For For Loops, this means going back to the top, incrementing our index value (as defined by our call to range()) and then making sure that we still numbers to get through. For example, if you had a list of numbers 0, 1, 2, 3, 4 and the following code:
|
||||
|
||||
```
|
||||
for x in range(5):
|
||||
if x == 3:
|
||||
continue
|
||||
print(x)
|
||||
```
|
||||
|
||||
The above code would print the following:
|
||||
- 0
|
||||
- 1
|
||||
- 2
|
||||
- 4
|
||||
|
||||
Three never prints, because once we hit that continue statement, we go right back to the top and onto the next number in the list of numbers that was generated by range(5)
|
||||
|
||||
Break on the other hand, when you hit a break statement, the loop ends, immediately, regardless of whether or not there were more things to do. Using the same list 0, 1, 2, 3, 4, and slightly different code:
|
||||
|
||||
```
|
||||
for x in range(5):
|
||||
if x == 3:
|
||||
break
|
||||
print(x)
|
||||
```
|
||||
|
||||
We would get
|
||||
- 0
|
||||
- 1
|
||||
- 2
|
||||
|
||||
Continue and break can also be used with While Loops, it all depends on what you're trying to accomplish.
|
||||
|
||||
## What is Loop Translation?
|
||||
Loop Translation is the idea that, a For Loop can always be written as a While Loop, and a While Loop can always be written as a For Loop. Loops in general do the same things once you break them down into there component parts and start running them on a CPU, so from that frame of reference it's easy to see how Loop Translation could be possible.
|
||||
|
||||
In general though, it's not good to "use the wrong type of loop" if you can help it. For loops are great when you know how many times you want to do something, but are terrible if the number of times you need to do something will change while the loop is running. While loops, on the other hand are great at doing things indefinitely, until some condition is no longer met, but the code needed to make a while loop do a certain number of things a certain number of times can be really ugly.
|
||||
|
||||
You as the programmer will need to decide when to use one loop or the other. This comes with experience, and for the most part is easy to pick up once you've made the decision a few times.
|
||||
|
||||
## I've seen two loops now, are there other types?
|
||||
Yes, sort of. Most other types of loops are based on For and While, the most common forms in my opinion are what I've shown you in this lesson and the last one.
|
||||
|
||||
There is a form of looping that can be done using something called Recursion. This however, goes way beyond the scope of these lessons, and so it isn't discussed here.
|
@ -1,3 +1,4 @@
|
||||
# 7 - Classes and Objects - Challenge
|
||||
Write a class called Ball with the following
|
||||
- Characteristics
|
||||
- diameter (float)
|
||||
|
@ -0,0 +1,63 @@
|
||||
# 7 - Classes and Objects
|
||||
|
||||
## Are you just throwing out words now, what are Classes and Objects?
|
||||
We are now getting into a powerful concept in programming in general that can be a little difficult to comprehend at first. The concept is called Object Oriented Programming, or OOP. Essentially what you're doing, is creating representations of a thing, and that things characteristics and behaviors, in code. The thing might be a real world object, it might not be, but to start out with, we're going to look at it from the context of creating real world objects in code, because that'll make the most sense. On top of that, a lot of the time spent Robot Programming is creating and manipulating representations of real world things in your code.
|
||||
|
||||
Classes are templates, blueprints used to define what an object should have for characteristics (how big it is, how tall, how short, how fast, does it have teeth and if so how many, etc.) and behaviors (can it bite, does it dance, can it jump up and down, etc.). You can't have an Object without a class that comes before it, the class gives you the framework to build an Object.
|
||||
|
||||
When making a class, the characteristics should be relevant to the Object you're wanting to represent, and the behaviors it needs to convey. Trees don't have teeth, so your class for a Tree shouldn't have anything referencing teeth, of the tree biting something. Likewise, a dog doesn't have leaves, so no need to write a behavior for the dog to make it photosynthesize light into sugar.
|
||||
|
||||
Objects are created from classes. You fill in the characteristics you defined in the Class with values, keeping with the tree example, you might have values for the height of the tree, the diameter of the tree, the type of tree. All of those things are characteristics of a specific tree. You could create one tree that's 5 meters tall, .5 meters around, and is an Oak tree, while creating a completely different tree that's 2 meters tall, .15 meters around, and is a Maple tree. You can create many representations of the same type of Object from a single class.
|
||||
|
||||
Now, representing trees in code, not so useful for our purposes. But once we get into Robot Programming, you'll have classes that create Objects that represent Motor Controllers, Sensors, the Driver's Station, Cameras, all kinds of stuff, all real world objects represented by structures in code.
|
||||
|
||||
## What does a class definition look like?
|
||||
Let's start off with this small class and talk about how it looks
|
||||
|
||||
```
|
||||
class SomeClass():
|
||||
def __init__(self, someParameter, someOtherParameter):
|
||||
self.instanceVariable1 = someParameter
|
||||
self.instanceVariable2 = someParameter
|
||||
|
||||
def doSomething(self):
|
||||
print(self.instanceVariable1)
|
||||
print(self.instanceVariable2)
|
||||
|
||||
def doSomethingElse(self):
|
||||
return self.instanceVariable1 + self.instanceVariable2
|
||||
```
|
||||
|
||||
Above, we start off by saying class <i>ClassName</i>(): this just defines what our class name is going to be so we can reference it later.
|
||||
|
||||
Next, we have def __init__(self), this is our constructor. It's a special method (a set of code that can be called by name) that is used to build Objects based on the class we create. The names inside the parenthesis are called parameters, these are special variables that are used to pass information into methods from outside the class. Classes are meant to encapsulate the information they contain and hide it away from prying eyes unless it's accessed in some way through a behavior (a method). So parameters allow us to hand information in that wouldn't otherwise be there.
|
||||
|
||||
The <i>self</i> parameter is important when building classes, because it stores our characteristics (also called instance variables) that we to contain inside of our created objects. You'll notice that in all methods in our class, not just __init__, self is a parameter. This is how the other methods (behaviors) in our resulting objects will be able to access the instance variables we set up in __init__ for later use.
|
||||
|
||||
The other methods (behaviors) <i>doSomething</i> and <i>doSomethingElse</i> are the actions that our created objects will be able to take. You get to define how this behaviors work, and you can have as many or as few as are necessary to make your representation work the way you want it to.
|
||||
|
||||
Notice for a moment in <i>doSomethingElse</i> there is a <i>return</i> statement. Return makes it so that the behavior produces a value, you can take that value, much like you would from a method like input() and store it in a variable. We'll see this when we look at creating an object down below.
|
||||
|
||||
## So how do I create an Object from a Class?
|
||||
Well, lets assume for the moment that we defined SomeClass in a file called OtherClasses.py, using that information, lets look at the code below:
|
||||
|
||||
```
|
||||
import OtherClasses
|
||||
|
||||
thisIsSomeClass = OtherClasses.SomeClass(1, 2)
|
||||
thisIsSomeClassAgain = OtherClasses.SomeClass(3, 4)
|
||||
|
||||
print(thisIsSomeClass.doSomethingElse())
|
||||
print(thisIsSomeClassAgain.doSomethingElse())
|
||||
```
|
||||
|
||||
First, we import the OtherClasses file by name, this makes SomeClass available to us to use. Then, we create two variables, thisIsSomeClass and thisIsSomeClassAgain, and create two different representations of SomeClass, the first using the values 1 and 2, the second using 3 and 4.
|
||||
|
||||
By doing this, we've created two Objects from the same class that will produce different results when asking each separate object to do something.
|
||||
|
||||
The first print statement deals with thisIsSomeClass, which has 1 and 2 for values. We saw above that doSomethingElse returns the sum of the instance variables we created, those instance variables have the values we pass in as parameters when we create an Object using SomeClass, in this case, the values 1 and 2. So when doSomethingElse runs, it will return, in this case, the value of 1 + 2, which is three. So the line print(thisIsSomeClass.doSomethingElse()) will print 3 to the console.
|
||||
|
||||
The next line is similar but not the same, because now we're dealing with the Object stored in thisIsSomeClassAgain, that Object was created using 3 and 4, we call doSomethingElse for that Object too, which adds 3 + 4, to get 7. So the line print(thisIsSomeClassAgain.doSomethingElse()) will print 7 to the console.
|
||||
|
||||
## This feels like a lot of information, is this a lot of information?
|
||||
It is, Classes and Objects can do so much, even more than what has been demonstrated in this lesson. It's a hard concept to convey quickly. For Robot Programming, at least at the beginner to low intermediate levels, an in depth understanding of Classes and Objects isn't super important, but it's good to know they exist, because once you decide to start making more complex robot programs, it may make sense to move certain aspects of your code into a class/object type of structure. On top of that, certain styles of Robot Programming require you to create many different classes (and subsequently, many different objects) in order for your robot to work. Take this early introduction to Classes and Objects into memory and hold onto it, even if you couldn't quite make this setup yourself for your own real world object if you wanted to, again, it's still good to have a general idea of what they are, because they will show up in your robot code, even if you don't realize in the moment that's what you're looking at.
|
@ -0,0 +1,13 @@
|
||||
# 8 - Basic Robot Electronics
|
||||
|
||||
## I want to be a programmer, not an electrician, why is this here?
|
||||
To be a good programmer (not just robot programmer) you need to have an understanding of what hardware your software has to interact with. Not every system is the same, and so you can't write your programs the same way for every problem you're trying to solve (although it would be nice if it were that way).
|
||||
|
||||
Not knowing which Motor Controller is which, not knowing which sensor is which, not knowing what any electronic component is on the robot will set you up for failure if you want to write code that actually works. So I built in some basic information to help give you a general idea of what everything is and how it's important or could be important to your overall robot function.
|
||||
|
||||
Now, am I saying that you need to get your hands in and start wiring the robot, no of course not. I did, but that doesn't mean the dual path of electronics and programming is right for you. But it will serve you well to know at least roughly what everything is that way when someone walks up to you and says "the climber motors are using REV Spark MAXs on CAN IDs 3 and 4 and we need to use the built in encoders to know where are climbing hooks are at all times" you won't just look back at them with a confused expression on your face.
|
||||
|
||||
This concept of "knowing just enough" applies to everything you'll do as a programmer. You don't write a AAA game without first understanding what engine you're using, what hardware that engine works on, what the target performance is. You don't write a database application without first understanding what the underlying database is going to be, what resources does it have, do any other applications interact with it. You must know the system your coding for, or you won't be able to write effective and efficient code.
|
||||
|
||||
## Ok I get it, but, this is just a PDF slideshow...
|
||||
That I can also fix, while there may be some Java oriented comments in the video, [this video on Youtube](https://www.youtube.com/watch?v=RI5EFQlbrYI) is an hour long discussion from the original Java Programming Training I conducted in 2022. We talk about everything in the slideshow, step by step. Again, it's just a basic overview, so if you have questions, you can direct them to me, or Google is your friend. Don't be in the dark on something when it comes time for you to start writing code if you can help it at all, it'll save you from the terrible feeling of needing to scramble to learn something in a time crunch.
|
Loading…
Reference in New Issue
Block a user