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 double the numbers in a list. | |
doubled_numbers = [] | |
for number in numbers: | |
doubled_numbers.append(number * 2) | |
# Rewrite using list comprehension. | |
# Syntax: |
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
# 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
#### 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: () |
OlderNewer