Code Icon

PYTHON LISTS, FUNCTIONS

Links


    # LISTS ARRAYS
    friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby", 2, True] # we can mix
    print(friends)
    print(friends[0])
    print(friends[-1]) # last element of the list
    print(friends[1:8]) # Shows Elements from 1 to 7
    friends[0] = "Mike"


    # LIST FUNCTIONS
    Characteristics: Mutable
    lucky_numbers = [3, 7, 6, 9, 8. 23]
    friends = ["Kevin", "Karen", "Jim", "Oscar", "Toby"] 

    friends.extend(lucky_numbers) # appends a new list inside a list
    friends.append("Mike") # add at the end
    friends.insert(1, "Mike") # insert a value at a specific place, doesn't delete others, moves them 1 position
    friends.remove("Jim")
    del frinends[1]
    friends.clear() # clears the list, removes all Elements
    friends.pop() # removes last element of a List
    friends.index("Oscar") # gives the index if exists
    frinds.count("Jim") # tells how many times the value is in the List
    friends.sort() #sorts list in alphabetical order
    friends.reverse() # reverses the order
    friends2 = friends.copy() # copies the list 

    friends_new = friends[1:4] Slices and takes elements from 1 to 3
    friends_new = friends[:4] Slices and takes elements from beginning to 3


    for friend in friends:
        if friend == "Sarah":
            print("Success")


    # 2D LISTS

    number_grid = [
        [1,2,3],
        [4,5,6],
        [7,8,9],
        [0]
    ]

    print(number_grid[0][0])

    # nested for LOOP
    for row in number_grid:
        for col in row:
            print(col)


    # TUPLES they are inmutable, no changes, updates, modifications, delete
    coordinates = (4, 5)
    States = ("Delaware", "Pennsylvania", "New Jersey")
    print(coordinates[0])


    # DICTIONARIES
    monthConversions = {
        "Jan": "January",
        "Feb": "February"
        "Mar": "March"
    }
    print(monthConversions["Jan"])
    print(monthConversions.get["Jan"])


    
    monthConversions["Apr"] = "April" // Adding items
    del monthConversions["Apr"]

    for value in monthConversions.values():
    for value in monthConversions.keys():
    for key, value in monthConversions.items(): //gets key and value, name is customizable
 

    # DICTIONARIES objects
    customers = [
        {
            "id": "1232",
            "name": "John"
            "last_name": "Sey"
        },
        {
            "id": "1233",
            "name": "Jack"
            "last_name": "Bay"
        },
    ]
    

    indexes
    customers[0]
    len(customers) // know the length
    new_dictionary = {
    "id": "1236",
    "name": "Sun"
    "last_name": "Shao"
    },
    customers.append(new_dictionary) 
 

    # DICTIONARIES + LISTS
    customers = [
        {
            "id": "1232",
            "name": "John"
            "last_name": "Sey",
            "discounts": ["standard", "volume", "loyalty"]
        },
    ]

    if "loyalty" in customers["discounts"]:
    

    indexes
    customers[0]
    len(customers) // know the length
    new_dictionary = {
    "id": "1236",
    "name": "Sun"
    "last_name": "Shao"
    },
    customers.append(new_dictionary) 
 

    # DICTIONARIES
    customers = {
        "id01":{
            "phone": "1232",
            "name": "John"
            "last_name": "Sey"
            },
        "id02":{
            "phone": "1233",
            "name": "Jack"
            "last_name": "Bay"
            },
    }

    print(customers[2]["address"])


    # FUNCTIONS need to be indented correctly. Named in lowercase with underscore if more than one name
    def say_hi(name, age):
        print("Hello you" + name + ", you are " + age)
    
    sayhi("Alberto", 30)


    # FUNCTIONS WITH RETURN (after the return you can't add anything)

    def cube(num):
        return num*num*num
    
    print(cube(3))