# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] slideshow={"slide_type": "slide"}
# # Beginning Programming in Python
#
# ### Lists/Strings
# #### CSE20 - Spring 2021
#
#
# Interactive Slides: [https://tinyurl.com/cse20-spr21-lists-strings](https://tinyurl.com/cse20-spr21-lists-strings)

# + [markdown] slideshow={"slide_type": "slide"}
# # Lists
#
# - Commonly in programming we want to keep a collection of values rather than a single value. 
# - In Python, one way this can be done is by using `list`s 
# - A `list` can store between 0 and 9,223,372,036,854,775,807 items(though a list that large would probably crash your computer).
# - A `list` can store different types in its list.
# - A `list` is a **mutable** collection, meaning we can change the contents of the list
# - Typical naming convention for `list` variables is to name them in the plural, i.e. `items`, `values`, or `cars`

# + [markdown] slideshow={"slide_type": "slide"}
# # Lists: Instantiation (Creation)
# - There are a few ways to instantiate a list. The way we'll go over today is using square brackets `[]`
# -

some_numbers = [1, 2, 3, 4]
some_letters = ["a", "b", "c"]
some_numbers_and_letters = [1, "b", 3]
empty_list = []

# + [markdown] slideshow={"slide_type": "slide"}
# # Lists: Access
# - To access/retrieve the values stored in a `list` variable we use square brackets `[]`
# - To retrieve a single value we use an index (starting from the left 0, or from the right with -1), i.e. `list_variable[0]`
# - To retrieve multiple items we use a `slice`. A `slice` is denoted using a colon `:`, and bounding indices can be placed on either side of the colon. Indices in `slice` notation are on a half closed interval where `list_variable[start:end]` operates on the interval `[start, end)`
# - The contents of a list can be changed by assigning a new value at a index. `list_variable[idx] = new_value`

# + [markdown] slideshow={"slide_type": "slide"}
# # Lists: Access
# -

some_values = [1, "b", 3, "d"]
print(some_values[0])
print(some_values[-1])
print(some_values[1:])
print(some_values[:2])
print(some_values[1:3])
print(some_values[:])

# + [markdown] slideshow={"slide_type": "slide"}
# # Lists: Updates
# -

some_values = [1, "b", 3, "d"]
print(some_values)
some_values[0] = "a"
print(some_values)
some_values[0:2] = [1, 2]
print(some_values)

# + [markdown] slideshow={"slide_type": "slide"}
# # `list` Methods
# - `list`'s are considered `object`s, we'll go over `object`s in more detail when we go over Object Oriented Programming (OOP).
# - For now you need to know that objects can have functions called `methods`, which can be "called" by using the `list_variable.method_name()` notation. 

# + [markdown] slideshow={"slide_type": "slide"}
# # `list` Methods: `append()`
# - `append()` adds a value to the end of the `list`
# -

some_values = []
some_values.append("Howdy")
print(some_values)
some_values.append("There")
print(some_values)
some_values.append("Friend")
print(some_values)

# + [markdown] slideshow={"slide_type": "slide"}
# # `list` Methods: `pop()`
# - `pop()` removes a value from the end of the `list`
# -

some_values = ["Howdy", "There", "Friend"]
print(some_values)
last_item = some_values.pop()
print("some_values: ", some_values)
print("last_item: ", last_item)

# + [markdown] slideshow={"slide_type": "slide"}
# # `list` Methods: `remove()`
# - `remove()` removes the first value in the `list` that matches the given argument
# -

some_values = ["Howdy", "There", "Friend"]
print(some_values)
some_values.remove("There")
print("some_values: ", some_values)

# + [markdown] slideshow={"slide_type": "slide"}
# # `list` Methods: `index()`
# - `index()` returns the "index" you would need to use to get the get the given argument.
# -

some_values = ["Howdy", "There", "Friend"]
print(some_values)
there_idx = some_values.index("Friend")
print("there_idx: ", there_idx)
print("some_values[there_idx]: ", some_values[there_idx])

# + [markdown] slideshow={"slide_type": "slide"}
# # `list` Methods: `count()`
# - `count()` returns the number of times a given argument occurs in a `list`
# -

some_values = ["Howdy", "There", "Friend"]
print("Howdy occurs", some_values.count("Howdy"), "time(s)")
some_values.append("Howdy")
print("Howdy occurs", some_values.count("Howdy"), "time(s)")
print(some_values)

# + [markdown] slideshow={"slide_type": "slide"}
# # `list` Methods: `reverse()`
# - `reverse()` reverses the order of the elements in the list 
# -

some_values = ["Howdy", "There", "Friend", "Hello"]
print("some_values: ", some_values)
some_values.reverse()
print("some_values: ", some_values)

# + [markdown] slideshow={"slide_type": "slide"}
# # `list` Methods: `extend()` or `+`
# - `+` like with strings will concatenate two lists together
# - `extend()` concatenates two lists, but does it "in-place". Its like using `+=` for concatenation.

# + slideshow={"slide_type": "-"}
some_values = ["Howdy", "There", "Friend"]
other_values = ["How", "Are", "You"]

concat = other_values + some_values
print(concat)
some_values.extend(other_values)
print(some_values)
some_values.extend(other_values)
print(some_values)

# + [markdown] slideshow={"slide_type": "slide"}
# # Built-in Functions That are Compatible With `list`s
# - `len()` will return the length(number of elements in) of the `list`
# - `max()` will return the maximum element in the list
# - `min()` will return the minimum element in the list
# - `sum()` will return the sum of all the elements in the list

# + [markdown] slideshow={"slide_type": "slide"}
# # Built-in Functions That are Compatible With `list`s

# + slideshow={"slide_type": "-"}
some_values = [1, 2, 3, 4, 5]
print(some_values)
print("There are", len(some_values), "values in the list")
print("The largest value is", max(some_values))
print("The smallest value is", min(some_values))
print("The sum of the values in the list is", sum(some_values))

# + [markdown] slideshow={"slide_type": "slide"}
# # Strings
#
# - Strings are like a list of characters but are different in a couple important ways:
#     - They are **immutable** (can't be changed)
#     - They don't support methods that imply mutability like `pop()`, `extend()`, `reverse()`, etc.
#     - Some helpful methods not apart of list include `.lower()` and `.upper()`
#     - `split()` can break a string into a list of strings, splitting the string based on the input argument
#     - More info in the string [documentation](https://docs.python.org/3/library/stdtypes.html#string-methods)

# + [markdown] slideshow={"slide_type": "slide"}
# # Strings
# -

class_name = "CSE20E40EABCjdfhsjkdfhkdjsfhskdjfhksjdhfkjlsdahf"
print(class_name[0])
print(class_name.index("E"))
print(class_name.count("2"))
print(class_name.lower())
print(class_name.split("h"))

# + [markdown] slideshow={"slide_type": "slide"}
# # Membership Operator `in` `not in`
#
# - You can test whether or not a `list` or string contains a value by using `in` and `not in` 

# +
some_numbers = [1, 2, 3]
contains_one = 4 in some_numbers
print(contains_one)

class_name = "CSE20"
contains_cse = "C20" in class_name
print(contains_cse)

# + [markdown] slideshow={"slide_type": "slide"}
# # What's Due Next?
#
# - zybooks Chapter 3 due April 18th 11:59 PM
# - Assignment 2 due April 25th 11:59 PM

StarcoderdataJupyterScriptsDedupFiltered