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
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")
likes_books = False is_logged_in = False if not likes_books and not is_logged_in: print("User not logged in!")
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 zero7.0 -->
[1, 2, 3, 4, 5]
[1, "two", False, 4.5]
len([3, 4, "red", "car"])
items = ["carrot", "peas", "celery"]
items[0]
items[-1]
items[-2]
items = [] items
items = list() items
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"]}
#with curly bracket contact_information = {} contact_information
contact_information = {"name":"Alfredo","Last_name":"Deza"} contact_information
contact_information = dict() contact_information
data =[("name","Alfredo"),("Last_name","Deza")] dict(data)
dict(first="alfredo",lastname="deza")
contact_imformation = {"name":"Alfredo", "last_name" : "Deza", "height" :112.4, "age":49}
#explicit for key in contact_imformation.keys(): print(key)
for value in contact_imformation.values(): print(value)
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)
in <cell line: 2>()
for method in dir(tuple): if method.startswith('_'): continue print(method)
in <cell line: 2>()
unique = set()
unique.add("one") unique
unique.add("one") unique.add("one") unique.add("one") unique.add("two") unique
unique.pop() unique
items = ['a', '1', '23', 'b', '4', 'c', 'd'] numeric = [] for item in items: if item.isnumeric(): numeric.append(item) print(numeric)
inlined_numeric = [item for item in items if item.isnumeric()] inlined_numeric
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
numeric = [item for item in parent for parent in nested_items if item.isnumeric()] numeric
numeric = [ item for item in parent for parent in nested_items if item.isnumeric() ] numeric
contacts = { 'alfredo': '+3 678-677-0000', 'noah': '+3 707-777-9191' } contacts
contacts['noah]
contacts.keys()
contacts.values()
for key in contacts: print(key) for name, phone in contacts.items(): print("Key: {0}, Value: {1}".format(name, phone))
fruits = [] fruits
fruits.append("Orange") fruits.append("apple") fruits
fruits.insert(0, "melon") fruits
vegetables = ['cucumber','carrot'] fruits+ vegetables
shopping_list = fruits+vegetables shopping_list.append(["sugar","salt"]) shopping_list
shopping_list = fruits+vegetables shopping_list.extend(["sugar","salt"]) shopping_list
colors = ["red","yellow","green","blue"]
colors[0]
colors[:2]
colors[-3:]
colors[1:3]
colors.pop(100)
in <cell line: 3>()
popped_item = colors.pop(1) popped_item
colors
colors.remove("blue") colors
Retreiving Data contact_imformation = {}
in <cell line: 2>()
try: contact_imformation["height"] except KeyError: print("6ft")
result = contact_imformation.get("height") print("Height of contact is", result)
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)