Skip to content

Instantly share code, notes, and snippets.

Avatar

Shyama Sankar Vellore ShyamaSankar

View GitHub Profile
@ShyamaSankar
ShyamaSankar / python_dictionaries.py
Created Mar 23, 2019
A cheat sheet for Python dictionaries
View python_dictionaries.py
# 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.
@ShyamaSankar
ShyamaSankar / python_sets.py
Last active Feb 7, 2022
A cheat sheet for Python sets.
View python_sets.py
# 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")
@ShyamaSankar
ShyamaSankar / python_tuples.py
Last active Jul 15, 2022
Cheat sheet for Python tuples
View python_tuples.py
#### 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: ()
View python_lists.py
# 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]
@ShyamaSankar
ShyamaSankar / nested_list_comprehension.py
Created Mar 17, 2019
Matrix addition using nested list comprehension and nested for loops.
View nested_list_comprehension.py
# 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])):
@ShyamaSankar
ShyamaSankar / list_comprehension_flatten_list_of_lists.py
Created Mar 17, 2019
Flatten list of list using list comprehension
View list_comprehension_flatten_list_of_lists.py
# 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.
@ShyamaSankar
ShyamaSankar / list_comprehension_nested_for_loop.py
Created Mar 17, 2019
Nested for loop into a list comprehension
View list_comprehension_nested_for_loop.py
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))
@ShyamaSankar
ShyamaSankar / list_comprehension_with_nested_if.py
Created Mar 17, 2019
List comprehension with nested if conditions
View list_comprehension_with_nested_if.py
# 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]
@ShyamaSankar
ShyamaSankar / list_comprehension_with_method.py
Last active Mar 17, 2019
Method within a list comprehension.
View list_comprehension_with_method.py
# 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
@ShyamaSankar
ShyamaSankar / list_comprehension_with_if_elif_else_ladder.py
Created Mar 17, 2019
List comprehension with an if-elif-else ladder
View list_comprehension_with_if_elif_else_ladder.py
# 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: