# Chapter7.py # Pythonic -> readability, framework, resource allocation # Variable Declaration -> readability, resource allocation print("===== Variable Declaration Example =====") # Python Syntax a = 7 b = 4 # Output Python Syntax c = a * b print(c) # Pythonic a,b = 7,4 # Output Pythonic c = a * b print(c) print("===== End of Variable Declaration Example =====") # List Comprehension print("===== List Comprehension Example =====") ## data set data = ['Bob', 'Joe', 'Mary'] ## Python syntax using the range() ## len() and range() manually defined with in keyword for x in range(0, len(data)): # x is the index not the value, if you do want the value, you need to call the obj[x] print(f'Index: {x} Value: {data[x]}') # data[0] Bob, data[1] Joe, data[2] Mary ''' # Ask the user what contact details to print row = input("Record to print: ") print(f"First Name: {data[int(row)]} Last Name: ") ''' print("---------") ## Pythonic using the in keyword with data set ## the in keyword handles the len() AND the range() for x in data: # x is the value not the index print(x) # x Bob,Joe,Mary print("===== End of List Comprehension Example =====") print("===== EAFP and LBYL Concepts =====") # control variable z = 'Bob' # Python LBYL -> Look Before You Leap Principle # if statements print("===== LBYL Concept =====") if z == 'Bob': # return True print("Do something with the true") else: # return False print("Bob does not exist.") # Pythonic EAFP -> Easier to Ask for Forgiveness than Permission Principle # try except handlers print("===== EAFP Concept =====") try: if z == 'Bob': x = 9 y = 0 z = x / y # divide by zero error Exception -> except print("stuff here...") # do cleanup here, it wont run with Exit Code 1 except Exception as err: print(err) # finally is optional, but highly recommend finally: # all the time try or except print("finally -> Do all our cleanup here...") # GC, close db conn, close() .txt, security checks, write log files # final processes print("===== End of EAFP and LBYL Concepts =====") print("===== Context and Connection Management =====") # IO reference filename = 'process_logging.txt' # Python Syntax -> Manually close IO's, commonly because you DO NOT use a keyword to open a connection (ex: with keyword) # varname = open(io) txtfile = open(filename, 'r') content = txtfile.read() print(content) txtfile.close() print("----") # Pythonic -> utilize opening IO's with keywords. Connections automatically close when you get an exit code # with open(io) as namespace with open(filename, 'r') as f: content = f.read() print(content) # no need to close() handled by the with keyword print("===== End of Context and Connection Management =====") print("===== Regular Expressions are Pythonic =====") # Python syntax .contains() # regex101.com # pattern matching or data validation # Ask a user to enter there phone number phone_number = input("Enter your phone number : ") import re # Standard Library Python # variable to hold our pattern (555) 555-1212 pattern_phone = r"[(][1-9]\d{2}[)] \d{3}-\d{4}" # regexlib.com # variable to search and return boolean match = re.search(pattern_phone, phone_number) # boolean True or False if match: print("Valid phone number") else: print("invalid phone number. please enter as (###) ###-####")