4️⃣

10.4 Practice

Practice

Print Even

Given the 2D list below, print every element at an even index of each inner list. Consider 0 to be even. Hint: A special message should appear if you did it right!
my_list = [ ["awesome", "hello", "job", "world"], ["you", "words", "got", "books"], ["it", "python", "right"], ["keep", "plant", "learning"], ["how", "school", "to"], ["code"], ]

Image Average

Here is the challenge problem for 2D loops:
Images are often represented as 3D arrays, where the rows and columns are the pixels in the image, and each pixel has an r, g, and b value
The interesting thing is that we can iterate over images. The challenge is, given an image, create a program that will return a different image where each pixel is the average of the pixels surrounding it in the original image
The neighbors of an image are all the pixels that surround it, 1 on each of its 4 adjacent sides, and 4 on the diagonals, for 8 in total.
Not every pixel has 8 neighbors though (think about why).
The code to grab an image from the internet and make it into an array is given to you. The code also displays the new image you create in the end.
 
💡
The image is 3D because each pixel has rgb values. To find the average value of all of a pixel's neighbors, you must change the average of the red value to the red value, blue to blue, etc. For example, if the neighbors ofa pixel with value [1, 2, 3] were [20, 30, 40] and [10, 120, 30], the new pixel that would replace the original one would be [15, 75, 35]
from PIL import Image import requests import numpy import matplotlib.pyplot as plt url = "https://images.dog.ceo/breeds/waterdog-spanish/20180723_185544.jpg" img = numpy.array(Image.open(requests.get(url, stream=True).raw)).tolist() newimg = img transpose = numpy.transpose(img) plt.imshow(img) plt.show() # write code to create newimg here plt.imshow(newimg) plt.show() plt.imshow(transpose) plt.show()

Address

Create a 2D list where each row represents a person and has 3 elements: their name, their age, and their address.
The entire list should have 4 such entries (4 people), so it will be a 4x3 list. Display the name and address of the 2nd person in the list. Then, display the entire list with the format: name (age): address

Random Grid

Create a 2D list with 4 rows and a randomly determined number of columns. The column number should be a random EVEN number between 2 and 16 (inclusive). All the even column numbers (including 0) should be filled with asterisks (*). The odd-numbered columns should be filled with underscores (_).
Display the grid at the end by printing out elements individually: don't use print(list). Assume that you don't know the size of the grid beforehand. In other words, if you wanted to display the 2D list without knowing the number of rows and columns in it, how would you code this?
 
For example, a 4x6 grid would display this:
* _ * _ * _ * _ * _ * _ * _ * _ * _ * _ * _ * _
💡
print('a') print('a') would display the 'a's with newlines, but print('a', end=' ') print('a') changes the end of the first 'a' from a newline to a space. The output is this: a a

Previous Section

Next Section

 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.