#!/usr/bin/env python # coding: utf-8 # # AddisCoder 2023: Week 2, Day 5 # ## Syntactic Sugar: List Comprehensions, Strings functions, Debugging # # Taught by Alex Krentsel (alex.krentsel@gmail.com) # # Python has a bunch of nice language features that let you do things more easily. # ## List Comprehensions # In[39]: # How do we know how to make a list of all the numbers from 1 to 20? # We can use a for loop! nums = [] for i in range(1,21): nums.append(i) print(nums) # In[ ]: # Python has some nice built-in features to do this more easily. Specifically, we can use list comprehensions. # List comprehensions are a way to create lists in Python. They are very useful and are used often in Python code. # Here is an example of a list comprehension that does the same thing as the for loop above. nums = [i for i in range(1,21)] # In[40]: # We can also use list comprehensions to create lists of strings. # Advanced example: we can use the chr() function to convert an integer to a character. # The ord() function converts a character to an integer. print(ord('a')) print(ord('z')) # In[42]: print(chr(97)) print(chr(98)) # In[ ]: # Here is an example of a list comprehension that creates a list of the first 10 letters of the alphabet. letters = [chr(i) for i in range(ord('a'),ord('k'))] # In[44]: # We can use range as usual to change the step size. countdown = [n for n in range(10,0,-1)] print(countdown) # In[49]: # We can also use if statements in list comprehensions lst = [x for x in range(10) if x%2 == 0] print(lst) # More examples to practice... # In[45]: # Making a list containing 10 2s? lst = [] for i in range(10): lst.append(2) print(lst) # In[46]: lst = [2 for i in range(10)] print(lst) # In[47]: # Multiple of 2 lst = [2*x for x in range(0,10)] lst # In[48]: # Make a new list modifying a different list lst2 = [l+1 for l in lst] lst2 # ## Dictionary Comprehensions # In[37]: # We can do the same thing for dictionaries powers_of_2 = {x:2**x for x in range(10)} even_powers_of_2 = {x:2**x for x in range(10) if x%2 == 0} # In[50]: # Here's a handy example: let's make a dictionary that maps from a character's code to the character itself. code2char = {i:chr(i) for i in range(ord('a'),ord('z')+1)} print(code2char) # ## Final Wrapup # # A few other topics we should talk about before we wrap up with standard Python stuff. # In[51]: # Ternary Operator # The ternary operator is a way to write if statements in one line. # The syntax is: if else # Example of a use of ternary: # We want to print "even" if a number is even and "odd" if a number is odd. def even_or_odd(n): return "even" if n % 2 == 0 else "odd" print(even_or_odd(7)) print(even_or_odd(42)) # ## Useful String Functions # In[ ]: # We can use the split() function to split a string into a list of strings. # We pass what "delimiter" we want to split on sentence = "This is a sentence made up of some words." words = sentence.split(" ") print(words) # In[54]: names_string = "Aku,Abraham,Binyam,Georg,Liya" name_list = names_string.split(",") print(name_list) # In[56]: # We can do the opposite too. # If we have a list, we can join the list together into a single string: print(words) print("-".join(words)) print(".".join(words)) print("ALEX".join(words)) # In[61]: # Printing using f-strings (formatted strings) # We can use f-strings to print strings with variables in them. # We put an f before the string and then put the variable in curly braces. name = "Alex" print(f"Hello, my name is {name}") greeting = "Hi" print(f"{greeting}, my name is {name}") # In[62]: # It doesn't have to be inside of a print statement. # We can use f-strings to create strings. name = "Alex" announcement = f"Hello, my name is {name}" print(announcement) # In[65]: # Going to uppercase or lowercase, sample_str = "My name is Alex" print(sample_str) print(sample_str.upper()) print(sample_str.lower()) # We can also use the title() function to capitalize the first letter of each word. print(sample_str.title()) # And many many more functions. # ## How to Debug your Code # # Let's look at an error together. #