How Do You Build a Flask Application to Return the Price of a Cryptocurrency?

Problem scenario
You want to write a Flask program to display the high and low prices of a given cryptocurrency for a given day. You want the output to include the volume too. How do you do this?

Solution
Prerequisite

This assumes that you have installed Flask and pandas.

Procedures
Run this program.

How Do You Use Boto and CloudFormation to Delete a Stack?

Problem scenario
You want to delete a CloudFormation Stack with Boto. What do you do?

Solution
Run a program like this (but replace “continualstack” with the name of the stack you deployed):

import boto3
cloudf = boto3.client(‘cloudformation’, region_name=’us-west-1′, aws_access_key_id=’AKIAMNOPQRST’, aws_secret_access_key=’FOOBAR1234/ZYXWV987654′)
json_stack=open(‘lampstack.json’, ‘r’).read()
cloudf.delete_stack(StackName=’continualstack’) …

How Do You Troubleshoot the Python Errors “NameError: name ‘copy’ is not defined” or “object has no attribute ‘copy'”?

Problem scenario
You are running a Python program. But you get “NameError: name ‘copy’ is not defined” or “object has no attribute ‘copy'”. What should you do?

Solution
Use “import copy” at the top of the program.

How Do You Write a Python Program to Find if The “Read more” Preview on a Web Page is Visible for Every Web Page Previewed?

Problem scenario
Sometimes the “Read more” or “Continue reading” hyperlink in the page preview of your website (e.g., when you search for a word), is not visible. You know that some of your web page previews on your website don’t have the “Read more” hyperlink at the end. This makes the preview appear to have the entire web page. You need to identify the postings that don’t have the “Read more.” How do you use Python to find the pages that have previews that have no “Read more” hyperlink?

How Do You Write a Python Function to Determine if There Are Three or More Unique Characters in a String?

Problem scenario
You want a function to tell you if there are three unique characters in a string. For example “ababababa” would have fewer than three unique characters. But “abc” would have three or more unique characters. How do you write a function that does this that solves in linear time?

Solution

variable_to_test = “aaaaaaaaaaaabbbbbb”

def two_unique(s):
flag = “There are only two unique characters in the string!”
dict_a = {}
for string_portion in s:
if string_portion in dict_a:
pass
else:
dict_a[string_portion] = 1
key_counter = 0
for key in dict_a.keys():
key_counter = key_counter + 1; …

Why Are Python List Operations/Methods Unlike Python String Operations/Methods?

Problem scenario
List operations such as .sort() will change previous copies of the list.

String operations such as .replace(“old_pattern”, “new_pattern”, count), will not change the variable or its copies; these operations will return a string after the replace operation has run on the original string.

Why is this behavior happening?

Solution
In-place changes and mutability are factors.

How Do You Troubleshoot “SyntaxError: invalid syntax” in Python?

Problem scenario
You are running a Python script. You keep getting “SyntaxError: invalid syntax” and a line number. You can find nothing wrong with the syntax on the specified line number. What do you do?

Possible solution #1
Look at the line with code, as opposed to blank line(s), above it. Is there a missing quote, brace, bracket or parentheses?

Possible solution #2
Are you trying to use an “if” statement on one line?

Why in Python Does a Variable Copy Get Changed when You Change Its Source?

Problem scenario
You have a list in Python that you copy into another variable. You change the original list and the copied version changes. Why does the copied version get copied and what can you do to overcome this?

Solution
There is something called a “deep” copy and a “shallow” copy. This program will illustrate both the problem and the solution:

list_a = [3, …

How Do You Use the .copy() Feature in Python?

Problem scenario
You want to use the copy feature in Python. What do you do?

Solution
To see the different types of copying, use a mutable object such as a list. Here is an example:

import copy

var1 = [1, 2, 3, 4]
shallow_copy = var1
deep_copy = copy.deepcopy(var1)
var1.append(999999999999)

print()
print(shallow_copy)
print(” — Shallow copy above and deep copy below —.”)
print(deep_copy)

For most (or perhaps all) intents and purposes,