Skip to content

SiMi723/Mlop_Week_One

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Mlop_Week_One

name = "Alfredo" last_name = "Deza"

height = 1000 distance = 1.33 date = "Tuesday"

height = "10000" height

name = "James" name

print(name, last_name)

#watch out with strings #full_name = name +last_name full_name = f"{name} {last_name}" full_name

new_name = name print(new_name) name = "Alfredo" print(new_name) print(name)

#it adds separation when using commas print(name, last_name, "is your instructor", "today.")

#strings can be assigned with single, double, and triple quotes full_name = 'Alfredo Deza ' full_name = "Alfredo Deza " full_name = """Alfredo Deza ""

summary = """Triple quotes are useful when you have a 'or a "within a string""" print(summary)

summary = 'use single quotes when u habe a double " in your string' print(summary) print("And use double quote when u have ' in your string")

name = " Alfredo " result = "this result is brought to u by" + name print(result)

result = f"{name} is teaching f strings!" print(result)

type(15)

14/2

7/0

7 + "14

2 7+ "14"

result = 3/2 print(result) type(result)

type(14.3)

311/99

type(True)

first_result = bool(1) second_result = bool(0) print(first_result, second_result)

none type(None)

condition = True if condition: print("the condition was met")

condition = False if condition: print("Was the condition met?")

groceries = [] if groceries: print("We have some groceries") invites = ["john", "George"] if invites: print("We have some invites!")

properties = 0 if properties: print("We have properties!") parents = 2 if parents: print("WE have parents!")

if properties == 0: print("We have no properties!") if parents>1: print("We have more than 1 parents") if parents>1: print("We have more than 1 parents")

if properties: print("We have properties!") else: print("We don't have properties!")

if properties: print("We have properties")

elif parents: print("We don't have any properties, but we have parents")

name = None if not name: print("Didn't get a name")

name = "Alfredo" last_name = None if not name: print("No name!") elif not last_name: print("No last name either!")

has_kids = True married = True if has_kids and married: print("This person is married and has kids")

not can be used but can get harder to read

likes_books = False is_logged_in = False if not likes_books and not is_logged_in: print("User not logged in!")

raise RuntimeError("this is a problm!")

RuntimeError Traceback (most recent call last) in <cell line: 1>() ----> 1 raise RuntimeError("this is a problm!") RuntimeError: this is a problm!

try:

result = 14/0 except ZeroDivisionError : #do some other intense operation result = 14/2 print(result)

try:

result = 14/0 result + "100" except ( ZeroDivisionError, TypeError) :

result = 14/2 print(result)

try:

result = 14/0 except ZeroDivisionError as error:

print(f"got an error --> {error}") result = 14/2 print(result)

division by zero

7.0 -->

[1, 2, 3, 4, 5]

[1, "two", False, 4.5]

use built-in to count items in a list

len([3, 4, "red", "car"])

items = ["carrot", "peas", "celery"]

items[0]

items[-1]

items[-2]

items = [] items

items = list() items

data can be pre-seeded

colors = ["red","blue","brown"] colors

for color in colors: print(color)

for color in ["yellow", "red"]: print(color)

numbers = [2, 3, 4, 12, 5, 3, 4] low_numbers = [n for n in numbers if n>6] low_numbers

{}

{"key":"value"}

{"key": True}

{"name":"Alfredo", "name":"Alfredo"}

{"items":["lumber","concerete","nails"]}

{[1,2]:False}

#with curly bracket contact_information = {} contact_information

contact_information = {"name":"Alfredo","Last_name":"Deza"} contact_information

with the dict built-in

contact_information = dict() contact_information

data =[("name","Alfredo"),("Last_name","Deza")] dict(data)

with dict() and keyword arguement

dict(first="alfredo",lastname="deza")

contact_imformation = {"name":"Alfredo", "last_name" : "Deza", "height" :112.4, "age":49}

for key in contact_imformation:

print(key)

#explicit for key in contact_imformation.keys(): print(key)

retrieve only values

for value in contact_imformation.values(): print(value)

retrieve both keys and values

for key, value in contact_imformation.items(): print(f"{key}-->{value}") contact_imformation.items()

Alfredo Deza 112.4 --> 49 --> --> -->

ro_items = ('first','second','third') print(ro_items[-1]) print("first item in the tuple is:%s"%ro_items.index('first')) for item in ro_items: print(item)

ro_items[9]

ro_items.index('fifth')

in <cell line: 2>()

find out what methods are available in a tuple

for method in dir(tuple): if method.startswith('_'): continue print(method)

tuples are immutable

ro_items.append('a')

in <cell line: 2>()

unique = set()

unique.add("one") unique

adding more items as long as they are unique

unique.add("one") unique.add("one") unique.add("one") unique.add("two") unique

you can pop items in like list but it takes no arguement

unique.pop() unique

items = ['a', '1', '23', 'b', '4', 'c', 'd'] numeric = [] for item in items: if item.isnumeric(): numeric.append(item) print(numeric)

notice the if condition at the end, is this more readable? or less?

inlined_numeric = [item for item in items if item.isnumeric()] inlined_numeric

doubly nested items are usually targetted for list comprehensions

items = ['a', '1', '23', 'b', '4', 'c', 'd'] nested_items = [items, items] nested_items

numeric = [] for parent in nested_items: for item in parent: if item.isnumeric(): numeric.append(item) numeric

and now with list comprehensions

numeric = [item for item in parent for parent in nested_items if item.isnumeric()] numeric

this can improve readability

numeric = [ item for item in parent for parent in nested_items if item.isnumeric() ] numeric

dictionaries are mappings, usually referred to as key/value mappings

contacts = { 'alfredo': '+3 678-677-0000', 'noah': '+3 707-777-9191' } contacts

contacts['noah]

you can get keys as list-like objects

contacts.keys()

or you can get the values as well

contacts.values()

looping over dictionaries default to .keys() and you can loop over both keys and values

for key in contacts: print(key) for name, phone in contacts.items(): print("Key: {0}, Value: {1}".format(name, phone))

define an empty list of fruits

fruits = [] fruits

appending items will insert them by the end

fruits.append("Orange") fruits.append("apple") fruits

similar to append u can insert but that require an index position

fruits.insert(0, "melon") fruits

you can add one list to another

vegetables = ['cucumber','carrot'] fruits+ vegetables

watch out when appending a list to an existing list

shopping_list = fruits+vegetables shopping_list.append(["sugar","salt"]) shopping_list

you can extend a list instead(similar to adding)

shopping_list = fruits+vegetables shopping_list.extend(["sugar","salt"]) shopping_list

colors = ["red","yellow","green","blue"]

by index

colors[0]

slicing for the first three items

colors[:2]

slicing for the last three items

colors[-3:]

slicing for a range

colors[1:3]

but the index must exist

colors.pop(100)

in <cell line: 3>()

use an existing index to pop properly

popped_item = colors.pop(1) popped_item

popping alters the list however

colors

colors.remove("blue") colors

Retreiving Data contact_imformation = {}

normal retreival[and possible exception]

contact_imformation["height"]

in <cell line: 2>()

using a try/except block

try: contact_imformation["height"] except KeyError: print("6ft")

using .get()

result = contact_imformation.get("height") print("Height of contact is", result)

falling back when there is no key

result = contact_imformation.get("height", "5 ft 9 in" ) print("Height of contact is", result)

contact_imformation["age"] = 31 print("Age is", conatct_imformation.pop("age")) print(contact_imformation)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published