Getting Started With Python

Introduction

Why learn Python?

Python is an incredibly useful high-level programming language. The syntax is simple and easy-to-use and can be learned quickly by anyone who can read and write English. Additionally, Python is an object-oriented programming language (this will be briefly explained later in this section).

Examples of Python applications

Simple Calculator

Python can be used to create simple applications to make the user's tasks easier. For example, it is very quick an easy to build a simple calculator that can be used for small calculations.

Machine Learning

Python is an instrumental language used in machine learning. Machine learning is a subset of artificial intelligence where developers must only write a small portion of code and the machine can use this code to determine optimal functionally autonomously.

Autonomous Vehicles

Many auto companies and Silicon Valley giants are currently working to create the first viable fully-autonomous vehicle. Most, if not all, of these companies are using Python in some facet for the development and operation of these vehicles.

Python Essentials

Comments

Python supports the use of "comments" that allow the developer to include notes about the functionality of their code without them being executed as code. Comments are extremely important because they allow a future developer who may be working on your code to better understand your thought process and intentions.

Single-Line Comments

Single-line comments are comments that only exist on one line. These comments are preceded by a single '#' and a space:

    # I am setting x equal to 0

    x = 0


Multi-Line Comments

Multi-line comments are comments that exist on multiple lines. These comments begin with (""")(three quotes) and end with (""")(three quotes):

    """ 

    This is a 

    multi-line comment

    """

    x = 0 

Variables

Usage

Python variables are used for storing information in your program. Variables can be used in input, output, calculations, or saved for future use.

In Python, variables do not need to be designated a "data-type", meaning, a variable can be assigned anything from a letter, to a number, to a special character without the developer needing to change the variable's type. 

Rules

  • A variable's name must start with one letter or an underscore (_).
  • A variable cannot start with a number.
  • A variable's name can only contain alpha-numeric characters and underscores.

Examples

     x = 1

     y = 2

     name = 'Ethan'

Input

When coding, the functionality of our program frequently depends on information that must be input by the user. To do this, we can use the input() function. Usage of the input() function requires 2 components: 

  1. An assignment variable: this is where the input information will be stored in our program.
  2. A prompt: this is what lets the user know what information we are asking for.

Here is a simple usage of input() asking the user to enter a number:

    entered_number = input("Please enter a number")

In this example, "entered_number" represents the variable where the data will be stored and the string in the quotes is the prompt that will ask the user for information.

Output

To output information to the user, we use the print() function. The print function allows us to output text or numeric information to the user. For example, if we wanted to tell the user that they should spin around 3 times, we could use this line of code:

    print "You should spin around 3 times"

This line of text would be output to the users screen.

Conditional statements

IF

The 'if' statement is used to check if a condition is true and perform some function if it is. The 'if' statement is often used to verify that a variable is equal to a certain value and print a statement to the user if it is (see Example 1). If an if statement is not true, the code will not execute and move on to the next piece of code. 

ELIF

The 'elif' statement is similar to an 'if' statement, but it must follow an 'if' statement. The 'elif' statement is used to check a second condition if the first condition was false. In the example below, you can see that, if the 'if' statement from Example 1 was false, our 'elif' statement would check an additional condition.

ELSE

The 'else' statement is used as a catch-all if the preceding 'if' and 'elif' statements were all false. If none of the earlier conditionals were true, the code in the 'else' statement will execute no matter what. As you can see in Example 3, if 'x' does not equal '1' or '2', we use the 'else' statement to let the user know this fact.

Example 1:

if x == 1:

     print "X equals 1"

Example 2:

if x == 1:

     print "X equals 1"

elif x == 2:

     print "X equals 2"

Example 3:

if x == 1:

     print "X equals 1"

elif x == 2:

     print "X equals 2"

else:

    print "X does not equal 1 or 2"

Conditional Comparators

When using conditional statements, it is important to use the correct symbols. 

To check if two values are equal, use '==':

   if x == 1:

To check if two values are NOT equal, use '!=':

    if x != 1:

To check if a number is greater than another number, use '>':

    if x > 1:


Our First Program

Integrated Development Environment (IDE)

An Integrated Development Environment (IDE) is the software that developers use to write programs. This software can be very difficult to install and configure correctly so, for our first program, we will be using an online IDE that is pre-configured for our example. Click HERE and open our IDE in a new window. You should see something like this:


Our first program

Our first program will be used to find if a number is the factor of another number. It will take 2 values as input, run the necessary calculations, and let the user know the result. The goal of this program is to use the Python essentials you learned in the last section.

Good luck!

Developer's information

Every program you write should include your name, e-mail address, and the date in a multi-line comment at the top of your program. This is called a "program header".

User input

In order to run our calculations, we need input from the user. We can do this using the input() function. 

For example: 

    our_input_variable = input("Enter the value")

The value to the left of the '=' is our input variable. This is where the user-entered information will be stored. The string in the quotes is the prompt to the user. This is what we use to request the information from the user.

Verifying our factor

Basic math tells us that if a number (let's say num1) is a factor of another number (let's say num2), the remainder of num1 divided by num2 must be 0. In Python, we can determine the remainder of a division operation using the % (modulo) operator.

For example: 10 % 2 = 0, 10 % 5 = 0, and 10 % 4 = 2 

We can store the result of this operation in a new variable. We will name this variable remainder.

IF statement

Now that we've completed our modulo calculation, our 'remainder' value will be 0 or some other number. If remainder is equal to 0, we know that factor is, indeed, a factor of number. To properly output this information to the user, we will use an if statement with a print function.

First, our if statement will check if remainder is equal to 0 using the '==' comparator. Is this evaluates to true, the print function will be run.

Our print function will print, for example, "2 IS a factor of 10." To make this statement as explicit as possible, we want to include both our factor and number values in our output. To do this, we can simply use the str() function.

When printing a variable, the str() function must always be used to convert the variable to readable text. This readable text can now be included in the output by using the + sign (as seen below) to append to the output string.

ELSE statement

The final lines of code in our program are to let the user know that the their sq_root value is NOT the square root of their number. We do this using an else statement. If the prior 'if' statement evaluates to false, the else statement will execute.

If our IF statement evaluates to false, however, we know that factor is NOT a factor of number. In this case, we can use an ELSE statement to output our final print function to say, for example, "3 is NOT a factor of 10." Our print function will be nearly identical to the previous one with the exception of changing "IS" to "is NOT".

Our program is now complete. In simple programs like this, there is no further code necessary to halt execution, it will stop on its own.

Executing code

To execute our program, simply click the execute button at the upper left-hand corner of our IDE. In the green command line below, you will be prompted to enter the user input. Type a number and press enter. Next you will be prompted for a factor. Type another number and press enter. The program will let you know whether your number is a factor or not.

What have we learned?

The modulo operator results in the:

  • Quotient
  • Sum
  • Difference
  • Remainder

True or False

  • A multi-line comment is preceded by '#'.
  • The 'IF' statement should always be the first statement in a conditional block.
  • You should NEVER include any personal information in your code.

In a conditional statement, I want to verify that two values (x and y) are different. I should use:

  • ==
  • !=