Skip to content

Instantly share code, notes, and snippets.

View ShyamaSankar's full-sized avatar

Shyama Sankar Vellore ShyamaSankar

View GitHub Profile
@ShyamaSankar
ShyamaSankar / list_comprehension_with_expression.py
Last active November 22, 2022 09:05
List comprehension to double numbers in a list
# 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:
@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_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_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: ()