# Chapter2.py
'''
"Bob"[0] "Mary"[1] "Joe"[2] "Mary"[3] -> data sample
Collection Classes elements/items are INdependent of each other
List [] indexed, change, dups, singular, !sorted
Sets {} singular, !sorted, !indexed, !dups, !change len(3)
{ "Bob", "Mary", "Joe" }
{ "Mary", "Joe", "Bob" }
{ "Joe", "Bob", "Mary" }
Excel unique()
Tuples () singular, immutable, positional id, dups, ordered
(4111111111111111, 12/25, 651, 32765)
Dictionary/Map { key:value } , key:!dup, value:dup, key:!change, value:change
{ fName: "Bob",
lName: "Bob" }
fieldNames = ['fName','lName','age'] -> keyNames unique
contacts.fname
customer.fname
{
fieldNames[0] = value[0], # fName: "Bob"
fieldNames[1] = value[1] # lName: "Smith"
}
'''
''' Level 3
Dependent data elements pandas data frame
Dimensional Array
1 dim -> Series
-> value
2 dim -> row/col -> value
3 dim -> pivot1 row/col -> value
4 dim -> p2,p1 row/col -> value
...
7 dim -> p5,p4,p3,p2,p1 row/col -> value
'''
# bit, byte, char, strings, lists, 1dim, ..., 7 dim
# Lists page 25-28
firstNames = ["Bob","Mary","Joe"]
print(firstNames)
firstNames.append("Mary")
print(firstNames)
firstNames[0] = "Robert"
print(firstNames)
# Loop thru the elements in a collection
## for x in ys
## x is the local variable in the scope of for
## ys is the data set
## for "each element/item" in "data set": do something
print("Contact Information")
print("===================")
print("List of Current Contacts")
row = 1
for firstName in firstNames:
# template variable
print(f"Contact ID: {row} Contact First Name: {firstName}")
# parameterization
# print("Contact ID:", row, "Contact First Name:", firstName)
# join operator
# print("Contact ID: " + str(row) + " Contact First Name: " + firstName)
row += 1 # structural
# for keyword
## indexes and gets the len list len(4)
## create a range(0:3)
## control i and loops increment
## built in break out of range
# The data set for this prompt is firstNames, and the type is a list. Ask the user what contact they want to update. Assign the id to an index by subtracting 1 from the int() input from the user. Then ask the user what the new contact name will be. Reprint the list.
updateID = int( input("What contact do you want to change? "))
# test or validate the input [1:4]
i = updateID - 1
firstNames[i] = input("What is the updated name? ")
print("New Contact List")
print(firstNames)
# coders -> programmers -> developers -> senior dev -> engineer -> senior e
# Production aka Real World
lastNames = [] # When needing a list to append, it is a best practice to create the list object in memory first, then use it.
lastNames.append("Smith")
lastNames.append("Jones")
lastNames.append("Johnson")
print(lastNames) # ['Smith', 'Jones', 'Johnson']
# 3 ways we handle object definition in memory
'''
objName -> visible object in memory
_objName -> hidden object in memory
__objName__ -> attr of an object in memory
ccinfo() -> _ccinfo[] -> ccinfo()
user -> _hidden -> objMemory
'''
# Tuples page 30
# There is no need to create an empty tuple first, as a tuple can not be mutated. The tuple can be created when the data is passed.
print("=== Credit Card Information ===")
print("===============================")
# Ask the user to enter their credit card informaion, using ccNum, ccExp, ccCode and ccZip
print("=== Please provide your credit card info for your purchase ===")
# Credit Card Number
## Store a string
## Validate as 16 digit number [0-9] must start with [4-9]
## use re module to test the user input page 89 Level 2
ccNum = input('Enter you credit card number: ')
# Expiration Date
ccExp = input('Enter Expiration Date (mm/yyyy): ')
# Security Code
ccCode = input('Enter your Security Code: ')
# Zip Code
## Use 5 digit format, not 5-4 format
ccZip = input('Enter your zip code (12345): ')
# Create a tuple with the user-supplied inputs
ccInfo = (ccNum, ccExp, ccCode, ccZip)
# Pass the tuple thru the cc api for processing, if approved, print approved", if declined, print declined. And if declined ask the user for new ccInfo
# call the api/function page 38
# get the result
result = "declined" # api passes back to our program "approved" or "declined"
if result == "approved":
print(ccInfo)
print("Amount approved by Merchant")
else:
print("Credit Card Declined. Please confirm your credit card information.")
reviewCcNum = "CC Number: " + ccInfo[0] # variable
print(reviewCcNum)
reviewCcExp = "CC Exp: " + ccInfo[1] # join
print(reviewCcExp)
print("CC Code:", ccInfo[2]) # parameterization -> don't add variable
reviewCcZip = f"Zip Code: {ccInfo[3]}" # template variable view
print(reviewCcZip)
# loop back to tuple inputs and tuple object
# Sets page 31
## Don't want duplicate product info in our cart
## performance for controller class from the set
## use a nested tuple for the product data, so that another object can NOT mutate our cart data
'''
{
(sku, desc, price),
(sku, desc, price),
(sku, desc, price)
} # close set
'''
products = {
# (int sku, string desc, float price)
( 1111, "Mens Running Shoe", 129.95 ),
( 1112, "Womens Hat", 65.25),
( 1113, "Childrens Socks", 14.56)
} # products
print("All product data in the products set")
print("====================================")
print(products)
# {(1112, 'Womens Hat', 65.25), (1111, 'Mens Running Shoe', 129.95), (1113, 'Childrens Socks', 14.56)}
print("====================================")
# Access each individual tuple in the set
print("=== Product Data ===")
print("====================")
for productTuple in products:
# architecture for productTuple (int, string, float)
# sku int, desc string, price float
# data model for the products = local variable for loop
productSKU, productName, productPrice = productTuple
# variable/property = param passed
# create a list from the tuple so we can use the int and the float as strings
list_productTuple = list(productTuple)
# format the productSKU int to view (ignore the data type)
# string_productSKU = f""
string_productSKU = "Product SKU: {}".format(productSKU)
print(string_productSKU)
# productName is already a string. nothing to do here
print("Product Name: " + productName)
# productPrice is a float, convert to string using str()
print("Product Price: $" + str(productPrice))
# option 2 to format BOTH int and float to string
string_productInfo = "SKU: {} Price: ${}".format(productSKU, productPrice)
string_productSKU_productPrice = "SKU: {} Price: ${}".format(productSKU, productPrice)
print(string_productSKU_productPrice)
# type_el1_el2
# type_elInfo -> more than 2 elements use
# EOF