Skip to content

Instantly share code, notes, and snippets.

View Ovicron's full-sized avatar
🐜

Ovicron

🐜
View GitHub Profile
import re
lyrics = '''7 Swans a Swimming
6 Geese a Laying
5 Golden Rings
4 Calling Birds
3 French Hens
2 Turtle Doves'''
xmas_regex = re.compile(r'(\d+\s\w+)')
import re
# Using Regex re.compile to define what to search for ( use parenthesis to group stuff )
search_phone_num = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
# Can use findall method or search method
mo = search_phone_num.search(r' My phone number is 343-943-4582 please call me! 394-393-2222 TEL')
import pprint # Imports pretty print
letter_counter = input("Enter text that you want to count: ") # Letters inside here are counted (user input)
count = {} # Empty dictionary that contains the counted letters
for i in letter_counter.upper(): # for item in letter_counter input and upper method makes sure so its all upper case.
count.setdefault(i, 0) # we use "setdefault" which is a dictionary method on "count" to add non-existing key.
# Guess the number game
import random
name = input("Hello, what is your name? ")
print(f"Hello {name}, I am thinking of a number between 1 - 10. Can you get it?")
secret_Num = random.randint(1, 10)
for guess_Count in range(1, 6):
# Using length function to find last element on a list along with IF/ELIF statements.
def larger_list(lst1, lst2):
if len(lst1) > len(lst2):
return lst1[-1:]
elif len(lst2) > len(lst1):
return lst2[-1:]
elif len(lst1) == len(lst2) and len(lst2) == len(lst1):
toppings = ["pepperoni", "pineapple", "cheese", "sausage", "olives", "anchovies", "mushrooms"]
prices = [2, 6, 1, 3, 2, 7, 2]
num_pizzas = len(toppings)
print("We sell " + str(num_pizzas) + " different kind of pizzas!")
pizzas = list(zip(prices, toppings))
@Ovicron
Ovicron / working_with_lists.py
Created April 25, 2020 17:06
len/count/sort/indexing
inventory = ['twin bed', 'twin bed', 'headboard', 'queen bed', 'king bed', 'dresser', 'dresser', 'table', 'table', 'nightstand', 'nightstand', 'king bed', 'king bed', 'twin bed', 'twin bed', 'sheets', 'sheets', 'pillow', 'pillow']
inventory_len = len(inventory) # Number of elements inside a list (number or strings)
first = inventory[0] # Index 0 means the first item in list
last = inventory[-1] # Negative means counts from last item in list
inventory_2_6 = inventory[2:6] # Index of 2 UP-TO 6 but doesn't include 6
@Ovicron
Ovicron / list_and_ranges_lesson.py
Created April 25, 2020 00:49
list/ranges/zip/append
first_names = ["Ainsley", "Ben", "Chani", "Depak"] # Used with ZIP
age = [] # Empty List
age.append(42) # Appending/Adding to empty list
all_ages = [32, 41, 29, 42] # Used with ZIP
name_and_age = zip(first_names, all_ages) # Using "ZIP" to pair/combine other lists.
@Ovicron
Ovicron / movie_rating_function.py
Created April 24, 2020 19:55
shows me inbetween comparison
def movie_review(rating):
if rating <= 5:
return "Avoid at all costs!"
elif 5 < rating < 9: # This shows us an in between comparison
return "This one was fun."
elif rating >= 9:
return "Outstanding!"
@Ovicron
Ovicron / try_and_except.py
Created April 23, 2020 21:32
try/except statements.
# try and except statements (Make errors more readable or hide them)
def raises_value_error():
try:
# Find errors with code in between "try" and "except" statements
raise 0 / 0