Skip to content

Instantly share code, notes, and snippets.

View ShyamaSankar's full-sized avatar

Shyama Sankar Vellore ShyamaSankar

View GitHub Profile
@ShyamaSankar
ShyamaSankar / python_dictionaries.py
Created March 23, 2019 19:17
A cheat sheet for Python dictionaries
# 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 August 23, 2023 13:33
A cheat sheet for Python sets.
# 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 April 1, 2024 10:13
Cheat sheet for Python tuples
#### 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: ()
@ShyamaSankar
ShyamaSankar / python_lists.py
Last active October 15, 2021 17:04
Python lists
# 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 March 17, 2019 09:45
Matrix addition using nested list comprehension and nested for loops.
# 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 March 17, 2019 09:13
Flatten list of list using list comprehension
# 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 March 17, 2019 08:53
Nested for loop into a list comprehension
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 March 17, 2019 08:16
List comprehension with nested if conditions
# 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 March 17, 2019 06:33
Method within a list comprehension.
# 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 March 17, 2019 06:06
List comprehension with an if-elif-else ladder
# 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: