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 an empty dictionary using {}. | |
employees = {} | |
print(employees) # Output: {} | |
print(type(employees)) # Output: <class 'dict'> | |
# Create a dictionary with items using {}. | |
employees = {1: 'Tom', 2: 'Macy', 3: 'Sam'} | |
print(employees) # Output: {1: 'Tom', 2: 'Macy', 3: 'Sam'} | |
# Create an empty dictionary using dict() constructor. |
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 an empty set using the constructor method. | |
numbers = set() | |
print(numbers) # Output: set() | |
# Note: {} creates a dictionary in Python. | |
print(type({})) # Output: <class 'dict'> | |
# set() constructor function takes an iterable as input. | |
numbers = set([1, 2]) | |
print(numbers) # Output: {1, 2} | |
string_set = set("hello") |
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
#### Tuple creation ##### | |
# Create a tuple, also called tuple packing. | |
numbers = 1, 2 | |
print(numbers) # Output: (1, 2) <- Note that it is represented with an enclosing paranthesis | |
# Create tuple with paranthesis. | |
numbers = (1, 2, 3) | |
print(numbers) # Output: (1, 2, 3) | |
# Create an empty tuple. | |
numbers = () | |
print(numbers) # Output: () |
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 an empty list using square brackets. | |
numbers = [] | |
print(numbers) # Output: [] | |
# Create an empty list using list(). | |
numbers = list() | |
print(numbers) # Output: [] | |
# Create a list of numbers. | |
numbers = [1, 2, 3] |
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
# Our list of lists. | |
matrix_1 = [[1,1,1], [2,2,2], [3,3,3]] | |
matrix_2 = [[4,4,4], [5,5,5], [6,6,6]] | |
# Matrix addition with for loop. | |
# Assuming that the matrices are of the same dimensions | |
matrix_sum = [] | |
for row in range(len(matrix_1)): | |
matrix_sum.append([]) | |
for column in range(len(matrix[0])): |
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
# Our list of lists. | |
matrix = [[1,2,3], [4,5,6], [7,8,9]] | |
# For loop to flatten the matrix. | |
flattened_list = [] | |
for each_list in matrix: | |
for number in each_list: | |
flattened_list.append(number) | |
# Rewrite using list comprehension. |
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
substrings_list = ["an", "the"] | |
strings_list = ["an apple", "the tree", "all trees", "an apple in the tree"] | |
# For loop to create a list of (substring, string) tuples from two lists. | |
# First is a list substrings and the second is a list of strings to be parsed. | |
tuple_list = [] | |
for string in strings_list: | |
for substring in substrings_list: | |
if substring in string: | |
tuple_list.append((substring, string)) |
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
# For loop to create a list with all even multiples of 5 (nothing but multiples of 10 :/). | |
filtered_list = [] | |
for number in range(1, 101): | |
if number % 2 == 0: | |
if number % 5 == 0: | |
filtered_list.append(number) | |
# Rewrite using list comprehension. | |
# Syntax: | |
# list_object = [expression_on_item for_item_in_iterable if_condition_1_on_item if_condition_2_on_item] |
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
# Original list of numbers. | |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
def modifier(number): | |
""" | |
Returns 0 if number is a multiple of 2, 1 if number is a multiple of 3 | |
and otherwise, the number itself. | |
""" | |
return 0 if number % 2 == 0 else 1 if number % 3 == 0 else number |
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
# Original list of numbers. | |
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
# For loop to create a new list where we substitute all multiples of 2 by 0, | |
# multiples of 3(which are not multiples of 2) by 1 and leave the rest as it is. | |
modified_numbers = [] | |
for number in numbers: | |
if number % 2 == 0: | |
modified_numbers.append(0) | |
elif number % 3 == 0: |
NewerOlder