# Chapter 7 Pythonic page 82 # Indentation process with the colon: sub ops def indentation(): # Non Pythonic with 4 spaces print("The indentation was with 4 space bars") # Pythonic print("Use the tab key for indentation") print("---- Variable Declaration -----") # Variable Declaration # Non Pythonic x = 7 y = 3 # Pythonic x,y = 7,3 # Data Sets data = ["Philip", "Bob", "Mary"] print("---- List Comprehension -----") # List Comprehension # Non Pythonic using the range() for x in range(0, len(data)): # dec start and stop print(x) # local var in scope is index # NOT value # If I want the value, use name[x] print(data[x]) # Pythonic for x in ys for x in data: print(x) # Prints the value of x, NOT the index print("---- EAFP and LBYL concepts -----") # EAFP > Easier to Ask for Forgiveness than Permission # LBYL > Look Before You Leap # control variable z = "Bob" # Non Pythonic was LBYL using the if keyword if z == "Bob": print("Do something if true") else: print("Bob does not exist") # Pythonic EAFP using try except finally handling Exception Exit Code 0 try: if z == "Bob": print("Do something if true") except Exception as err: print(err) finally: print("End of program") print("--- Context and Connection Managers ----") # Context and Connection Managers filename = "myfile.txt" # Non Pythonic uses a variable to open a file, close file manually txtfile = open(filename, 'r') # assigns a var name and then opens file content = txtfile.read() print(content) txtfile.close() # manually close the file # Pythonic with as keyword still the open() with open(filename, 'r') as txtfile: content = txtfile.read() print(content) # no need to close() handled but the with keyword scope print("---- List Slicing ----") # List Slicing "Phil@1234.com" ['P','h','i','l','@',...] # use data list from above # Non Pythonic # create the sublist to hold data sub_data = [] for i in range(0, len(data)): sub_data.append(data[i]) print(sub_data) # 3 returns # Pythonic def func1(): # f lenofdata = len(data) # s = f sub_data1 = data[0: lenofdata] # s s[s:s] print(sub_data1) # return f 1 return func1() # Functional and Pythonic print(" ---- Regular Expressions ---- ") #RegEx page 89 # regex101.com pattern testing # regexlib.com pattern examples # Regular Expressions are Pythonic # Python syntax .ToString() # Pythonic phonenumber = input("Enter your phone number ex: (555) 555-1212: ") import re # Standar Module Regular Expressions phone_pattern = r"((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}" match = re.search(phone_pattern,phonenumber) # True or False if match: print("Valid phone number: ", phonenumber) else: print("You entered an invalid phone number. Please enter as (###) ###-####") print(" ---- Using Enumerate with Data Set ---- ") # Accessing both index and value of a set # data list from above # Python for i in range(len(data)): # parameterization # mismatched data types i int value strings print("Param Method > Index ID: ", i, "Name:", data[i]) # string join + REQUIRE implicit type matches print("Join Method >Index ID: " + str(i) + " Name: " + data[i]) # f to use the format method print(f"Format Method> Index ID: {i} Name: {data[i]}") # staying in the string print("===========") # Pythonic using enumerate() with format method for i, v in enumerate(data): #index, value of index # f to use the format method print(f"Format Method> Index ID: {i} Name: {v}") # staying in the string