Skip to content

Instantly share code, notes, and snippets.

View watxaut's full-sized avatar
🥐

Joan watxaut

🥐
  • Glovo
  • Barcelona
View GitHub Profile
@watxaut
watxaut / mutable_2.py
Created October 22, 2019 19:38
Python Tutorial: Mutable variables 2
# --> METHOD 1: The Pythonic way
import copy
list_copy = copy.deepcopy(whoops_list)
print(list_copy is whoops_list) # Spoiler, it's False
# --> METHOD 2: The 'I don't have time for this!' guy
# Don't be this guy
list_copy_2 = whoops_list[:]
print(list_copy_2 is whoops_list) # Spoiler, it's False
@watxaut
watxaut / mutable_1.py
Created October 22, 2019 19:29
Python tutorial: mutable_vars 1
# we define a list (mutable type)
whoops_list = ["I", "am", "really good", "at Python"]
# we equal this list to another variable
i_am_really_bad_at_python_list = whoops_list
# we append an item tho the SECOND list
i_am_really_bad_at_python_list.append("OR AM I????")
@watxaut
watxaut / sets.py
Last active October 22, 2019 19:21
Python Tutorial: Sets
# Sets are unordered lists of UNIQUE ITEMS, like dictionaries but without
# values, only keys.
set_numbers = {5, 3, 6, 7, 7, 7, 7}
print(set_numbers)
# they are useful when you need to know which items are in one set and not in the other
set_numbers_2 = {1, 2, 3, 4, 5}
# this will print the intersection which is another set = {3, 5}
print(set_numbers_2.intersection(set_numbers))
@watxaut
watxaut / dictionaries.py
Last active October 22, 2019 19:16
Python Tutorial: Dictionaries
# DISCLAIMER: any coincidence with reality is pure luck, no spoilers ahead
# having this dictionary of key: GoT character and value: a boolean answering if it's alive or not
d_got_is_alive = {"John Snow": True, "Tyrion Lannister": True, "Ned Stark": True}
# to retrieve an item, like lists but by the KEY
print(d_got_is_alive["Ned Stark"])
# example with for loop
# the method .keys() of a dictionary returns a list of the keys in the dictionary. WATXAUT! It might not be ordered!
for key in d_got_is_alive.keys():
@watxaut
watxaut / lists.py
Created October 22, 2019 19:10
Python Tutorial: Lists
list_numbers = [12, 33, 44, 55]
# lists of list are also possible! You can create matrix for example
jacobean_matrix = [
[3, 4, 5],
[1, 0, 4],
[0, 3, 1]
]
# to add things to lists (remember, mutable means mutable like it can change)
@watxaut
watxaut / while_loop.py
Created October 22, 2019 19:07
Python Tutorial: While loop
i = 0
# if you mess with the next line -> boom, infinite loop
while i < 10:
print(i)
# if you don't include this line -> boom, infinite loop
i = i + 1
@watxaut
watxaut / conditionals_example.py
Created October 22, 2019 18:39
Python Training: conditionals
# here we have one boolean
some_true_condition = True
# if the boolean is True, it will print, if not, it won't do anything
if some_true_condition == True:
print("Hey this is True")
if some_true_condition: # This is the same, but more Pythonic
print("Hey this still is True")
@watxaut
watxaut / immutable_variables.py
Last active October 22, 2019 18:28
Python Training: immutable variables
# Examples of immutable variables in Python
# ----> Numbers
integer_num = 4
float_num = 4.5
complex_num = 4j
# ----> Strings
string_sentence = "Hey I am immutable"
string_with_simple_quotes = 'I am immutable and I am still a string'