# Chapter 7 Pythonic # Python -> required way to code "syntax" # Pythonic -> readability, framework, resource allocation # Variable Declaration -> readability, resource allocation print("==== Variable Declaration Example ====") # Python syntax a = 7 # EOL python interpreter b = 3 c = 4 z = a + b + c print(z) # 14 # Pythonic a1, b1, c1 = 7, 3, 4 z1 = a1 + b1 + c1 print(z1) # String Interpolation -> framework print("==== String Interpolation Example ====") # join operator vs template variables # join operator is data type dependent "all types have to be friends" # Don't talk to strangers only talk to friends # https://en.wikipedia.org/wiki/Law_of_Demeter # join operator framework works at the object level # Python firstName = "Bob" lastName = "Smith" fullName = firstName + " " + lastName # string + string + string print(fullName) # Pythonic # if we only need to get the value, use template variables # template variables do not care about data type # template variables framework works at the value level name, age = "Bob Smith", 21 # string, int contactInfo = name + " " + str(age) # Python object level str() contactInfo1 = f"{name} {age}" # Pythonic value level print(contactInfo) print(contactInfo1) # List Comprehension -> framework, resource allocation print("==== List Comprehension Example ====") # dataset for our example data = ['Bob','Beth','Joe','Mary'] # List # Python syntax using range() function # len() and range() are called manually after the in keyword # the index id and the data value need to be called as 2 seperate variables for x in range(0, len(data)): # x as a local variable is the index id, NOT the value print(f"Index Id: {x} Value: {data[x]}") # x = 0 data[0]='Bob' print("=======") # Pythonic # the in ys is handling all the len() and range() for us for x in data: print(x) # x is the value that we are looping thru # EAFP vs LBYL Principles print("==== EAFP vs LBYL Principles Example ====") # control variable y = 'Bob' # Python LBYL -> Look Before You Leap # if else statements print("=== if statement ===") if y == 'Bob': # Fail at the if, IF the data type is assumed correct # return True print("Bob is the value tested") else: # return false print("The value tested is not equal to the control") # Pythonic EAFP -> Easier to Ask for Forgiveness than Permission # try except handlers "finally" optional BUT highly recommended try: if y == 'Bob': # return True print("Bob is the value tested") else: # return false print("The value tested is not equal to the control") except ValueError: print("Something about the value error") except TypeError: print("Something about the type error.") # if else test for data validation -> Python # Regular Expressions -> Pythonic # pattern matching -> match a defined string pattern # regex101.com # regexlib.com # [a-zA-Z0-9] # (555) 555-5555 # [(][1-9]\d{2}[)] \d{3}-\d{4} # Ask the user to enter phone number phone_number = input("Enter your phone number ex. (xxx) xxx-xxxx: ") # re is a standard module import re # define the pattern to test pattern_phone = r"[(][1-9]\d{2}[)] \d{3}-\d{4}" # variable to hold the true or false match = re.search(pattern_phone, phone_number) # return true or false if match: print("Valid phone number.") else: print("Invalid phone number") # EOF