Intro to Python Programming with Mosh – Pt. 04

April 22, 2019

Intro to Python Programming

Tutorial – 04

Youtube – Learn Python 3 for Machine Learning & Web Development [2019]

By: Programming with Mosh
Progress

End Time: 1:41:55

Next Part of Tutorial: For Loops

NOTES

Weight Converter

This was a simple exercise to try and create a simple weight converter. It would take a user input weight and ask if it was in Kilograms or Pounds (lbs), then convert it to the other unit.

Their solution to this exercise provided a nice way to make strings less case sensitive. Since the unit input was just the first letter of the unit abbreviation (L for pounds and K for kilograms), this could make it completely non-case sensitive. You simply use the upper() method on the string you are comparing a value for before checking what it is. Then you only have to compare it to the capital case instance and either they will have already typed in the capital instance, or you will have converted the lower case to an upper case before comparing.

While Loops

These are used to execute blocks of code multiple times. The syntax for while loops is similar to C#. You just use the while key word with a condition, ending with a colon. Then the following indented code will be executed as long as the condition is met.

Guessing Game

This uses a while loop to build a simple guessing game.

This example went over “refactoring”, which is renaming a variable and all the instances of it in your code. This can be done with the keyboard shortcut: Shift + F6.

The Break command is used to immediately exit the program out of a while loop. This is useful when a varied condition is met that doesn’t need the loop to run again. This also prevents any Else counterparts of the while loop from executing.

While loops can also have Else counterparts. These will execute if the entire while loop is completed without the use of a Break.

Car Game

This was another exercise to do on your own. This time it was creating a “Car Game” that just said you started the car if you typed ‘start’ and stopped the car if you typed ‘stop’. I used a simple while loop to contain the game as a whole, with a game_started bool. The entire game ran in this while loop while game_started is true, and terminates when that is set to false. This gave me a way to quit the game when the user typed in quit.

Within the main game while loop, there were just a lot of if statements (with elif) to determine what to print based on what the player typed in. There was also an extra bool added which was car_started, which could be used to determine if the car was already started or stopped. This could be used to make sure the player couldn’t stop an already stopped car or start an already started one.

Intro to Python Programming with Mosh – Pt. 03

April 7, 2019

Intro to Python Programming

Tutorial – 03

Youtube – Learn Python 3 for Machine Learning & Web Development [2019]

By: Programming with Mosh
Progress

End Time: 1:16:25

Next Part of Tutorial: Project: Weight Converter

NOTES

Arithmetic Operations

In Python we have the same operations used in math, as well as a few extra ones. There is addition, subtraction, multiplication, and division.

Here are a few of the extra operators. By using //, it returns the integer of the division with no remainder. Using the modulus, %, it returns the remainder after performing the division. Finally, ** is exponent, so this will raise a number to the other values power.

Then there is the augmented assignment operator. This is done by placing the operator directly before the equal sign. For example:

x += 3
is the same as: x = x + 3

Operator Precedence

This is the math concept where certain operations are performed before other operations. Many of these in Python are the same as those in basic math.

Math Functions

This just covers so more basic math functions: round, abs

Round rounds your value to the nearest integer. Abs returns the absolute value of a number.

If you want to do very intense mathematical calculations, you will want to grab a math module to add to Python. This module would give you more built in functions to perform mathematical functions. This is done by typing:

import math

at the top of your Python code. You can then call functions from it in your code with math.FUNCTIONNAME.

if Statements

The syntax of if statements is a bit different from C#. The syntax is as follows:

if STATEMENT:
functionsToPerform()

A colon is used to end the if conditional, and the functions following it are determined just by the indentation. To end the statement, you just remove the indent.

To perform else if, you simply type elif in Python. Else remains the same as you type the full word.

Logical Operators

These are the AND, OR, and NOT operators. In Python, logical AND, OR, and NOT are done by simply typing the word “and”, “or”, or “not” in the place they are needed. They do not use symbols.

Comparison Operators

These are used to compare variables with values. These are operators such as greater than and less than. These are all the same as C#, including the equivalency operator that is ==. Not equal is also the same, which is !=.

Intro to Python Programming with Mosh – Pt. 02

Intro to Python Programming with Mosh – Pt. 02

March 14, 2019

Intro to Python Programming

Tutorial – 02

Youtube – Learn Python 3 for Machine Learning & Web Development [2019]

By: Programming with Mosh
Progress

End Time: 48:40

Next Part of Tutorial: Arithmetic Operations

NOTES

Strings

You can use single or double quotes for strings, but there is a difference for certain applications. For example, if you want an apostrophe in your string, you need to use double quotes to define the string. An example for the reverse is that you use single quotes to define the string if you want something to be in double quotes within the string.

Strings can also be defined with triple quotes. This allows you to create a multiline string.

You can call a character index of a string with brackets (i.e. string[0] will return the value of the first character in that string.). An interesting feature is that you can use negative indices to start at the end of the string and count backwards.

You can also call a chunk of characters with two values separated by a colon. If you don’t place a number before the colon, 0 will be assumed. If you don’t place a number after the colon, the length of the string is assumed. Using these together, you can perform a simple duplication of a string (i.e. another_string = string[:]).

The final check with this colon/index syntax was what would happen if you entered:
string[1:-1]
Where string was ‘Jennifer’. This returned ‘ennife’, which appears to indicate this syntax always returns values from left to right if possible, as well as showing that the beginning value is inclusive, while the final value is exclusive. To further test this, I tried string[3:1] and this did not return any value, which fit my thought on “left to right is possible”.

Formatted Strings

Formatted strings are used to make complex strings easier to visualize their output. Formatted strings are indicated with quotes starting with an f (i.e. f’text’). They then use curly brackets to create holes in the string where variables can be placed.

String Methods

The first basic method is len(). This returns the value of the number of characters in a string. This function is general purpose though, so it can count the number of objects in a list as well for example. The fact that it is general purpose is what makes it a function, as opposed to a method.

We looked into the find() method for strings. This takes an input of characters and returns the index of the first instance of those characters in the associated string. You can enter a single character or entire words to search for. Multiple characters will return the index of where those characters start.

The replace() method takes two inputs; the first input is what it searches for in the string, and the second input is what it replaces that with.

Next we used the “in” operator. Using the format of:
‘characters’ in string_variable
This returns a bool value of whether those characters are found within the string value indicated.

TERMINOLOGY

  • Method: in object oriented programming, a function that belongs to or is specific to some object

SUMMARY

The main difference between a function and a method is that a function is more general purpose, where as a method is something belonging to a specific type of object.

The string topics covered here gave me a lot of new information. I’m interested to see if there is any overlap with some of this in C# for Unity programming since there’s a lot of really helpful functions and methods covered. The formatted strings are especially nice to keep code nice and tidy instead of having those ugly string concatenation lines.

Intro to Python Programming with Mosh – Pt. 01

March 13, 2019

Intro to Python Programming

Tutorial – 01

Youtube – Learn Python 3 for Machine Learning & Web Development [2019]

By: Programming with Mosh
Progress

End Time: 29:35

Next Part of Tutorial: Strings

NOTES

This tutorial covers the complete basics of Python, starting with installing it, so it is as beginner friendly as you can get. The entire tutorial will use these basic foundational teachings to lead into creating projects to show how to use what you learn in a functional sense. These projects include a shopping site, an AI system for helping a user get music to match their preference, and a spreadsheet processing program.

Upon installation, it was important to “Add Python to PATH” for Windows (which I am using). I wasn’t sure if I did that since I had installed before, so I found that this references the Path in environment variables and just adds the path of your Python folder to this list. I added my Python path to this list in hopes that would resolve that issue. The tutorial says this is critical to follow it, so I will see if I get any errors.

Next we grabbed a code editor. The suggested one to use was PyCharm. There were no special installation features or plugins mentioned to download/install so I just installed the bare minimum.

In Python, you can define a string with single or double quotes. You can also multiply a string with a number to print that string that number of times (i.e. ‘*’ * 10 gives **********).

Standard variable types:

  • int: integer values
  • float: floating point values (those with decimal points)
  • string: words/text
  • bool: true/false
  • lists
  • objects

Next we finally got into receiving input from the user. This uses a Python function simply called Input(). The input from the user for this will also initially be created as a string variable, so make sure to convert this to int, float, etc. for your needs.

TERMINOLOGY

  • Expression: a piece of code that produces a value
  • Variable: allocation of memory for a specified value

SUMMARY

So far the basics seem very similar to what I’ve already learned in C++/C#, with the standard variable types and simple functions. I am speeding through these early parts, but don’t want to jump ahead since I want to make sure I don’t miss anything that does happen to differentiate from what I already know. This is also a good refresher for basic programming terminology and getting another view on the underlying basics of coding.

Complete Beginners Python Tutorial by Programming with Mosh

March 11, 2019

Complete Beginners Python Tutorial

Youtube – Complete Python Tutorial for Beginners (2019)

By: Programming with Mosh

I wanted to try using Unity’s ml-agent toolkit and its initial setup uses a lot of Python so I got interested in learning the basics to better navigate the toolkit and its full potential. This was one of the first tutorials I came across and its size and recency made it an attractive choice to cover a lot of ground that shouldn’t be outdated for any reason. Its size is also a bit of a drawback at over 6 hours, so I will most likely break this up and cover it in the upcoming weeks.

Unity ML Agent Setup in Windows

March 11, 2019

Setting Up Using ML Agents in Unity

Windows 10 Setup

Unity – Machine Learning
Unity – Windows Installation

I was interested in trying out Unity’s machine learning agents toolkit so I started attempting to get everything setup and installed today. I have no prior experience with Python, so that took a bit of getting used to.

I had some trouble setting up initially as I followed the link in the documentation for the Python download which led me to get Python 3.7, but Python 3.6 is what is used in the installation notes. When I went to install the TensorFlow component, I couldn’t get that to work in 3.7. Afterward I went back and got Python 3.6 and setup a 3.6 environment and got the same error initially, but then got it to work after actually activating the ml-agents environment (so I may have been able to solve the error in Python 3.7 with this change).

Otherwise the setup went rather smoothly. The excessive use of Python encouraged me to look into some tutorials to start learning the basics to handle this Unity ml-agent system better.

The Windows installation also included an extra section for getting into GPU training for the ML-Agents. This seems like it adds a lot of extra complications so I am going to stick with the basic setup for now, but I may come back to this once I get a better grasp of Python and the ml-agent toolkit.