This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> # Create a list for subsequent operations | |
>>> numbers = [1, 2, 3, 4, 5, 6] | |
>>> | |
>>> # Typical way to create a list consisting of squares | |
>>> squares0 = [] | |
>>> for number in numbers: | |
... squares0.append(number*number) | |
... | |
>>> # List comprehensions | |
>>> squares1 = [number*number for number in numbers] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> # Create a list of students based on their arrival sequence | |
>>> students = ['John', 'Aaron', 'Jennifer', 'Ashley'] | |
>>> | |
>>> # Lengthy way | |
>>> for index in range(len(students)): | |
... student = students[index] | |
... print(f"Arrival # {index+1}: {student}") | |
... | |
Arrival # 1: John | |
Arrival # 2: Aaron |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> # Dictionary comprehension | |
>>> squares_dict = {number: number*number for number in numbers} | |
>>> squares_dict | |
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36} | |
>>> | |
>>> # Set comprehension | |
>>> numbers_dups = [1, 2, 3, 4, 3, 2, 1] | |
>>> squares_set = {number*number for number in numbers_dups} | |
>>> squares_set | |
{16, 1, 4, 9} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> # Use a custom class | |
>>> class Student0: | |
... def __init__(self, name, gender, student_id): | |
... self.name = name | |
... self.gender = gender | |
... self.student_id = student_id | |
... | |
>>> s0 = Student0('John', 'M', 2020001) | |
>>> f"Name: {s0.name}; Gender: {s0.gender}; ID #: {s0.student_id}" | |
'Name: John; Gender: M; ID #: 2020001' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This Google Sheets script keeps data in the specified column sorted any time | |
* the data changes. | |
* | |
* After much research, there wasn't an easy way to automatically keep a column | |
* sorted in Google Sheets, and creating a second sheet to act as a "view" to | |
* my primary one in order to achieve that was not an option. Instead, I | |
* created a script that watches for when a cell is edited and triggers | |
* an auto sort. | |
* |