Skip to content

Instantly share code, notes, and snippets.

View ItsCosmas's full-sized avatar
🔨
Building

Cozy! ItsCosmas

🔨
Building
View GitHub Profile
@ItsCosmas
ItsCosmas / maps.py
Last active May 8, 2019 20:05
Maps
#MAPS
def addFive(x):
return x + 5
nums = [11,22,33,44,55]
result = list(map(addFive,nums))
print(result)
nums = [11,22,33,44,55]
result = list(map(lambda x:x+5,nums))
print(result)
@ItsCosmas
ItsCosmas / lambdas.py
Created May 7, 2019 10:13
Lambda Functions
# Functions on the go, without assigning them to a variable
# Anonymous functions
def square(x):
return x*x
print(square(2))
print((lambda x: x*x)(4))
@ItsCosmas
ItsCosmas / filter.py
Last active July 24, 2019 12:44
Filters
# FILTERS
nums = [11, 22, 33, 44, 55]
result = list(filter(lambda x: x % 5 != 0, nums))
print(result)
list = [12]
print("========= INSERT ============")
list.insert(0, 5)
print(list)
list.insert(1, 10)
print(list)
list.insert(2, 6)
print(list)
print("========= REMOVE ============")
ages = {"Cozy": 21, "Mark": 22, "Dan": 25}
primary = {
"red": [255, 0, 0],
"green": [0, 255, 0],
"blue": [0, 0, 255]
}
print(primary["red"])
print(ages["Cozy"])
# Only Immutable objects can be used as keys to dictionaries
# Dictionaries and Lists are mutable and cannot be used as keys in a dictionary
my_set = {"apple", "banana", "cherry", "strawberry", "grapes"}
# removes an element from the set
my_set.discard("banana")
print(my_set)
# Clear empties the set
my_set.clear()
words = ("spam", "eggs", "sausages")
print(words[0])
print(words.count("spam"))
print(words.index("eggs"))
# They are immutable
# FOR LOOP
words = ["hello", "world", "spam", "eggs"]
for word in words:
print(word)
# FOR LOOP BREAK
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
break
print(x)
@ItsCosmas
ItsCosmas / for_loop_continue.py
Created May 9, 2019 08:16
For Loop Continue
# FOR LOOP CONTINUE
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)