Training Videos

Summer Training

  1. video

  2. video

  3. video

Jay Weekly Calls

  1. Video did not record, slides
  2. video, slides
  3. video, slides
  4. video, slides

Solution Templates

Basic Python

Single Solution

median

headers:
    use-dict-pop: Use `dict.pop()`
---
# How to pop a key from a dictionary in Python
Popping a key from a dictionary removes it from the dictionary and returns its value . For example, popping `"a"` from `{"a": 1, "b": 2}` changes the dictionary to `{"b": 2}` and returns `1`.

## Use [`dict.pop()`](kite-sym:builtins.dict.pop) to pop a key from a dictionary {#use-dict-pop}
Call  [`dict.pop(k, d)`](kite-sym:builtins.dict.pop) with the desired key as `k` and a default return value as `d` to pop a key from a dictionary. If the key is not found in the dictionary, the default return value is returned instead.
```python
a_dictionary = {"a": 1, "b": 2}
output = a_dictionary.pop("a", 0)
print(output)
print(a_dictionary)
```
```python
a_dictionary = {"a": 1, "b": 2}
output = a_dictionary.pop("c", 0)
print(output)
```

Multi Part Solution

multiply list

headers:
    use-zip: Use `zip()`
---
# How to multiply two lists in Python
Multiplication between two lists multiplies each element from one list by the element at the same position in the other list. For example, multiplying `[1, 2, 3]` and `[4, 5, 6]` results in `[4, 10, 18]`.

## Use [`zip()`](kite-sym:builtins.zip) to multiply two lists {#use-zip}
Pass both lists into [`zip(*iterables)`](kite-sym:builtins.zip) to get a list of tuples that pair elements with the same position from both lists. Use a for loop to multiply these elements together and append them to a new list.
```python
list1 = [1, 2, 3]
list2 = [4, 5, 6]
products = []
for num1, num2 in zip(list1, list2):
    products.append(num1 * num2)
print(products)
```
Use a list comprehension for a more compact implementation.
```python
list1 = [1, 2, 3]
list2 = [4, 5, 6]
products = [a * b for a, b in zip(list1, list2)]
print(products)
```

## Other solutions

Use [`numpy.array()`](kite-sym:numpy.array) to multiply two lists
- This solution is more efficient for large datasets. However, it requires importing the
NumPy library
- **Further reading:** See documentation for [numpy.array()](https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html)

> **Further reading:**
> List comprehensions provide a concise way to create lists. You can read more about list comprehensions [here](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions).

Multi Solution

set

headers:
    add-single: Add a single item
    add-multiple: Add multiple items
---
# How to add items to a set in Python

A set is a collection of distinct items. Adding an item to a set that doesn't contain the item updates the set to contain the item. For example, adding `3` to `{1, 2}` results in `{1, 2, 3}`. If the set already contains the item, nothing is added.

## Add a single item to a set {#add-single}

Call [`set.add(element)`](kite-sym:builtins.set.add) to add an element to a set.

```python
items = {1, 2}
items.add(3)
print(items)
```

## Add multiple items to a set {#add-multiple}

Call [`set.update(iterable)`](kite-sym:builtins.set.update) to add all elements from a list or other iterable to a set.

```python
items = set()
items.update([2, 3, 4])
print(items)
```

> **Warning:**
> Sets can be defined as `{1, 2}`, but if `{}` is used to define an empty set, a `dict` will be made, resulting in a possible error or unexpected behavior when trying to use it.

NumPy

Basic Answer

numpy array

headers:
    use-numpy-as-array: Use `np.asarray()`
---
# How to convert a list to a NumPy array in Python

A NumPy [array](https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html) is an optimized data structure to efficiently perform calculations on data but is less flexible than a native [list](kite-sym:builtins.list).

## Use [`np.asarray()`](kite-sym:numpy.asarray) to convert a list into an array {#use-numpy-as-array}

Call [`np.asarray(a)`](kite-sym:numpy.asarray) with the list as `a`.

```python
KITE.start_prelude()
import numpy as np
KITE.stop_prelude()
an_array = np.asarray([1, 2, 3])
print(an_array)
```

Annotations

Basic Answer

Annotations Example

Warning The Annotation syntax supports backticks and kite-sym links. However, these are currently nonfunctional in the preview.

headers:
    use-a-datetime: Use a `datetime`
---
# How to convert a string date to timestamp in Python
A timestamp measures the number of seconds that have passed since January 1st, 1970. For example, converting the string date `"25/01/2000"` returns the timestamp `948758400`.

## Use a datetime to convert a string date to timestamp {#use-a-datetime}
Use [`datetime.datetime.strptime(date_string, format)`](kite-sym:datetime.datetime.strptime) with `format` as `"%d/%m/%Y"` to convert `date_string` to a [`datetime`](kite-sym:datetime.datetime). Use [`datetime.datetime.timetuple()`](kite-sym:datetime.datetime.timetuple) to return a tuple from the datetime. Pass the tuple into [`calendar.timegm(tuple)`](kite-sym:calendar.timegm) to convert the tuple to a timestamp.


```python
KITE.start_prelude()
import calendar
import datetime
KITE.stop_prelude()
string_date = "25/01/2000"
# Convert to `datetime`
a_datetime = datetime.datetime.strptime(string_date, "%d/%m/%Y")
# Convert to tuple
datetime_tuple = a_datetime.timetuple()
# Convert to `timestamp`
timestamp = calendar.timegm(datetime_tuple)
print(timestamp)
```