Problem Solving With Python: Program 2

The Problem Statement:

Program 1 showed us how to iterate over a range of numbers with the use of a for loop and the range function. Suppose instead of a range of number we have a bunch of numbers that are contained in data structure called a list. Given a list of numbers, how do we iterate over it using a for loop?

The task, just like program 1, will be to write a Python program that displays a list of numbers from 4 to 9. Next to each number, the program will also display its square. But unlike program one where we used the range function to generate the range of numbers along with their square, we instead will use a list of number and iterate over it in three different ways.

The following is the expected approximate output of program 2. The output is repeated four times. Note that for the last example we will iterate over the number in reverse order:

    4 16
    5 25
    6 36
    7 49
    8 64
    9 81
    --END---
    4 16
    5 25
    6 36
    7 49
    8 64
    9 81
    --END---
    4 16
    5 25
    6 36
    7 49
    8 64
    9 81
    --END---
    9 81
    8 64
    7 49
    6 36
    5 25
    4 16
    --END---
    

The Solution:
    

def program_2():

    """LOOP 1"""
    for number in 4, 5, 6, 7, 8, 9:
        print(number, number * number)
    print("--END---")

    """LOOP 2"""
    for number in [4, 5, 6, 7, 8, 9]:
        print(number, number * number)
    print("--END---")

    """LOOP 3"""
    list_of_numbers = [4, 5, 6, 7, 8, 9]
    for number in list_of_numbers:
        print(number, number * number)
    print("--END---")

    """LOOP 4"""
    reverse_list_of_numbers = list_of_numbers[::-1]
    for number in reverse_list_of_numbers:
        print(number, number * number)
    print("--END---")


if __name__ == '__main__':
    program_2()



LOOP 1:

The range of values are from 4 to 9. The comma is used to separate the numbers so that the for ... in loop can distinguish between the unique numbers and consume them.

LOOP 2:

The list of values is explicitly defined by the usage of the opening and closing brackets. That is, the use of [ (opening bracket) and ] (closing bracket)

LOOP 3:

We can also assign the list of values to a variable called list_of_numbers and then use the variable in the for loop. The use of the variable will allow us to reuse the list of number in the subsequent, and last, for loop.

LOOP 4:

We reverse the list of numbers with the use of the so called slice operator. The slice operator is a shorthand notation that is used when we want it iterate over a list in a custom order. There are many ways to use the slice operator. We show only one example in this tutorial. The slice operator, [::-1], iterates over the list_of_number in a reverse order; returning a new list of numbers but in reverse order. This reverse list of numbers is captured by the reverse_list_of_numbers variables, which on the next line is used by the for loop. More on the slice operator in a future tutorial.

Page with links to the entire series can be found here

Are you having any issues running or understanding the program? Please, add a comment explaining where you are stuck or where the tutorial is not clear so we can improve it.

Problem Solving With Python: Program 1

The problem statement:

Program 0 provided a solution to the problem statement but it is an inefficient way to salve it. The solution required us to perform repetitious work. For that problem we only had to display the numbers from 4 to 9 along with its square. Now imagine if the problem required us to display numbers, along with their square, from 4 to 79. Our solution with be unnecessarily long and take an unacceptable amount of time to complete.

The task, just like program 0, is to write a Python program that displays a list of numbers from 4 to 9. Next to each number, the program will also display its square. But unlike program zero we are expected to write program 1 using less code such that we minimize as much as possible the repetitious work.

The following is the expected approximate output of program 1:

    4 16
    5 25
    6 36
    7 49
    8 64
    9 81

The Solution:

def program_1():
    for number in range(4, 10):
        print(number, number * number)


if __name__ == '__main__':
    program_1()

Program 1 introduces a powerful programming feature called a for loop, that allowed us to write a concise solution to this problem.

Why is it called a for loop?
It's called this because for each member in a given list of items, the code indented under a for loop will be looped over and executed a number of times that is equal to the number of items in the given list. In our solution, the number of members in the given list of items is 10 - 4 = 6. Hence, our for loop executed our indented code, or nested code, exactly six times.

What is this range(..) function?
This range function creates an object that produces a sequence of integers from the start value of 4 to the stop value of 10 minus 1. That is, given the start value of 4 and the stop value of 10 the following sequence of integers is generated for the for loop to consume. 4, 5, 6, 7, 8 and 9. Note that the last value of 10 is not included in the sequence. If for example we wanted modify the code to start at 2 and end at 10 we would call the range function as follows. 11 - 2 = 9 for loops would be executed. 

range(2, 11)

 Page with links to the entire series can be found here

Are you having any issues running or understanding the program? Please, add a comment explaining where you are stuck or where the tutorial is not clear so we can improve it.

Problem Solving With Python: Program 0

The problem statement:

The task is to write a Python program that displays a list of numbers from 4 to 9. Next to each number, the program will also display its square. The square of a number is computed by multiplying it with itself like so. 4 x 4 = 16. Here, 16 is the square of 4.

The following is the expected approximate output of program 0:

    4 16
    5 25
    6 36
    7 49
    8 64
    9 81

The Solution:

def program_0():
    number = 4
    print(number, number * number)

    number = 5
    print(number, number * number)

    number = 6
    print(number, number * number)

    number = 7
    print(number, number * number)

    number = 8
    print(number, number * number)

    number = 9
    print(number, number * number)


if __name__ == '__main__':
    program_0()

How do I setup?

Follow the steps at this link to get started. When following the instructions assume that your machine does not already have Python installed so that you follow the instructions for downloading Python and making sure you install version 3.10. Ignore the details of the program in this example and instead focus on running the program in their example.

How do I run the program?

Running the code. If you followed the steps from the setup section than you should now how to create a file and run a Python program in Python. In your python project create a module (file) called program_0.py. Type the code in this file as shown and run the program. If your setup was successful then the Python interpreter should have run your program that generates some output within PyCharm.

What is a code interpreter?

The Python Interpreter is a program that reads and execute Python code. In order for the interpreter to be able to do its job, the program that we write must follow a set of rules. Code indentation is one of these rules. Violation if this or any programming rules will result in a program that does not execute or, even worse, a program that executes incorrectly.

What is a variable?

A variable is a name for a physical location within the computer that can reference either a single piece of data like a number or a character or data structures that can hold multiple pieces of data. In our program we define the variable number that holds a single number. As the name implies, a variable can be changes by our program as it is running. Our program first defines this variable number by assigning the number 4 to it. Our program then changes the value that our variable is referencing with the use of the assignment operator.

Why use a variable at all? 

Variables will allow us to write code that is concise, descriptive and reusable. The importance of these ideas will become more evident as we write programs with increasing complexity.

What is an assignment operator?

The assignment operator, denoted by =, is used to assign a value to variables. The following is a general form of an Assignment Statement.

variable_name = expression


What are Python Keywords? 

Python keywords are reserved words that should not be used except for its explicitly defined usage. The keywords used in this program are the following.

  1. def is used to define the start of a function.
  2. if is used to test a condition. In our case it is used to test if the variable __name__ is equal to the string __main__. If it is then the next indented lines are executed by the interpreter. In our case, our function, program_0() is executed, resulting in an output that is generated by our program.

       
What is a function? 

A function is a building block of a program; they are used to define code that is reusable and can be called anywhere within your program. Note how we first write our display logic in def program_0()... then further down we call it. Suppose we wanted to display the same values again immediately below the first program output. This can be accomplished in one of two ways. We can either modify our program to display the values twice or simply call our program immediately following the first call like so.

if __name__ == '__main__':
    program_0()
    program_0()

        
Hence, code reuse!

What is a built-in function?

Python is shipped with many standard built in functions. Our program the built-in print(...) function to display our values to the monitor. We can pass any number of variables to the print function with the use of a comma. The print function will display the variables passed to it by inserting a single space between them. The print function will also add to the end a newline character so that the display system will print the next values on a new line.

Page with links to the entire series can be found here

Are you having any issues running or understanding the program? Please, add a comment explaining where you are stuck or where the tutorial is not clear so we can improve it.

Obtain a Student Visa to Study in the USA (PART 2)

Part Two:

This is our second post in our obtaining a student visa to study in the USA series. Your can read our first post here.

What is the Form I-20 and How to Receive it?


All F-1 visa category (and M-1) students that study in the USA need a Form I-20, "Certificate of Eligibility for Nonimmigrant Student Status". Once accepted into a Student and Exchange Visitor Program, international students will receive a Form I-20 from their designated school official (DSO). This will be one of the important Forms that you as the student should keep that safe as you will need it throughout your international student life.

Process to Obtain an I-20:
1:
  1. Receive an I-20 application from the School via Email.
  2. Complete, Print, and Sign the application. 
  3. Recent Bank Statements from you or your sponsors showing that you are financially able to pay your school tuition, fees and life expenses for the years you plan on attending the school.
  4. Send a copy of your valid passport along with completed and signed I-20 application, and financial documents to designated school office (DSO) at the school you will be attending via Email.

Obtain a Student Visa to Study in the USA


Are you interested in knowing how to obtain a student visa in order to study at a university in the USA? If so then read about the steps I took in obtaining a student visa in order to study at the one of the largest public universities in America, The City University of New York (CUNY).

This post is a bit unusual in the sense that we typically don't cover non-tech related stuff on this blog but given my experience in this area I thought that this might benefit our readers. So, in this and upcoming posts I will walk you through the step by step process of applying for a student visa, entering into the USA, and give you some pointers on what to do during and after your studies are complete and you've earned a degree.

How to Setup Angular CLI

Angular is one of the third top front-end frameworks in 2020. Angular is a TypeScript-based open-source web application framework led by the Angular Team at Google.

In this tutorial we are going to show you a step by step guide to setting up your environment for Angular development and a simple hello-world application.

Wikileaks Vault 7: CIA Tips for Git Workflow

Wikileaks has begun dumping a large number of files on the CIA's hacking tools. The dump is called Vault 7. It is a goldmine, not only for information about the CIA's activities, but also for information on things like how to set up a development environment or properly use Git in your everyday programming workflow. Here are a couple highlights from some cursory searches of the document dump:

CIA Git Tutorials

CIA Vim Tutorials

CIA Setting Up a Development Environment