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

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/Limpapat/Python101/blob/main/basic_python_part1.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="cF3ug9_ldwkm"
# # Python 101 : Basic Python Programming [Part 1]
# by <NAME>
#
# 22/08/2021
#
# [datacamp] Cheat sheets http://datacamp-community-prod.s3.amazonaws.com/0eff0330-e87d-4c34-88d5-73e80cb955f2

# + [markdown] id="GFXXoNlpz16a"
# ## Variables
#
#
# ```
# variable = expression
# ```
#
# Variable type
# ```
# number --> int float
# string
# boolean
# ```
#
# Keywords based on Python version 3.7
# ```
# False      await      else       import     pass
# None       break      except     in         raise
# True       class      finally    is         return
# and        continue   for        lambda     try
# as         def        from       nonlocal   while
# assert     del        global     not        with
# async      elif       if         or         yield
# ```
#

# + id="tMD8rjsHWON5" colab={"base_uri": "https://localhost:8080/"} outputId="3e31f429-265e-4915-87cb-e171900e2c7d"
# Int
a = 1
a

# + id="4bovsaOLw5Gr" colab={"base_uri": "https://localhost:8080/"} outputId="588cd8ea-f36e-4b2e-9b8a-f412e9d57e7a"
# This is comment
a = 1
(float(a) - 1)*99

# + id="91G6iFPEWnc5" colab={"base_uri": "https://localhost:8080/"} outputId="dae5261b-e3d9-4b46-d34d-7ef3cb03b53b"
# Float
float(a)

# + id="reCYBrjSW-WZ" colab={"base_uri": "https://localhost:8080/", "height": 58} outputId="31540699-70e0-425e-e0ad-97fdd39e2018"
# String
str(a)

# + id="TopGFHnV_oqA" colab={"base_uri": "https://localhost:8080/", "height": 58} outputId="0b0c8461-2f65-410b-905c-e42c829d694d"
str(a) + "1"

# + id="w8Zv3DPAZUt3" colab={"base_uri": "https://localhost:8080/"} outputId="30e69959-c53d-421f-b919-c2bdd97ef00c"
bool(a)

# + id="Zn5_2PblJ7DJ" colab={"base_uri": "https://localhost:8080/"} outputId="dff2c86a-935c-4977-82ad-ec0327980f72"
bool(None)

# + id="gUCKSUJmZeNS" colab={"base_uri": "https://localhost:8080/"} outputId="94745146-d916-4d26-8810-aeef3aa4f68f"
type(a)

# + id="myq5TcAWe7ZP" colab={"base_uri": "https://localhost:8080/", "height": 58} outputId="690e281c-740c-42ad-fd6a-a02ab75766bc"
b = "Python!"
b

# + [markdown] id="v7qySGBL4t47"
# ### Basic operators
# * Arithmetic Operators
# ```
#   + - * / % ** //
# ```
# * Comparison (Relational) Operators
# ```
# == != <> > < >= <=
# ```
# * Assignment Operators
# ```
# = += -= *= /= %= **= //=
# ```
# * Logical Operators
# ```
# and or not
# ```
# * Bitwise Operators
# ```
# & | ^ ~ << >>
# ```
# * Membership Operators
# ```
# in, not in
# ```
# * Identity Operators
# ```
# is, is not
# ```

# + [markdown] id="_jGLwZ_5a5GA"
# ## Collections

# + [markdown] id="3ZyIaJomrYPJ"
# ### String

# + id="PwkD2_FxrXjm"
sentence = " Hello Corgi. I am hungry. "

# + id="iHraaCCo3wOZ" colab={"base_uri": "https://localhost:8080/"} outputId="71df636a-118c-4e5c-db44-c1f3510e4119"
len(sentence)

# + id="OfSs6-_Xr7Nn" colab={"base_uri": "https://localhost:8080/", "height": 58} outputId="9365bd1c-ea1c-47f0-df0f-ed81733681fd"
sentence[1]

# + id="9-o4xocQ5she" colab={"base_uri": "https://localhost:8080/", "height": 58} outputId="ffe11393-3c74-4f1b-ba44-3182ace99652"
sentence[1:12]

# + id="nxSxmQcyrmQQ" colab={"base_uri": "https://localhost:8080/", "height": 58} outputId="38a5d6ae-7680-47da-f9dd-cd1eccd64f62"
sentence.strip()

# + id="-a6IbCxw3zA8" colab={"base_uri": "https://localhost:8080/"} outputId="1b98ce90-710b-4504-cd47-aa9251e55172"
len(sentence.strip())

# + id="r1I9hlv6r4FB" colab={"base_uri": "https://localhost:8080/"} outputId="5b0f987e-ebea-401d-ebc7-9a1e82e9799e"
sentence.strip().split(' ')

# + id="BdxmLMEuqv2j" colab={"base_uri": "https://localhost:8080/", "height": 58} outputId="90544c1a-56e5-4d28-9893-f2f201d42b82"
ss = ' '.join(sentence.strip().split(' '))
ss

# + [markdown] id="4SeTYcMBqLjD"
# ### List
#
# ```
# list = [...]
# ```

# + id="4YcpeyeUbGca" colab={"base_uri": "https://localhost:8080/"} outputId="1eb79913-62a7-4f18-c5cb-f74f835818a5"
l = [b, a, a - 1, a]
l

# + id="h5tpLduMfpIY" colab={"base_uri": "https://localhost:8080/"} outputId="fab3ea82-9565-4a58-ee1d-883dc58fe951"
len(l)

# + id="P_7Esctef3G5" colab={"base_uri": "https://localhost:8080/"} outputId="80f27882-2a8a-4de7-b924-a26d6dad2875"
l[3] = 0
l

# + id="VyVzFzgLCmGr" colab={"base_uri": "https://localhost:8080/", "height": 199} outputId="579b7328-f227-4132-c18a-abd950a1a78a"
# NameError
l[4] = ":)"

# + id="jreK8tLJCvj5" colab={"base_uri": "https://localhost:8080/"} outputId="00c41864-5a44-4c97-aa2c-c005984580ff"
l

# + id="omSAISZ5CweV" colab={"base_uri": "https://localhost:8080/"} outputId="51e84ae4-cfc4-43bd-d189-ca9101f9348a"
l.append(":)")
l

# + id="SzfHN-iiC6Ag" colab={"base_uri": "https://localhost:8080/"} outputId="42f962f0-6165-4373-f521-4c0abeee7928"
len(l)

# + id="MIQd0KpXC8DE" colab={"base_uri": "https://localhost:8080/"} outputId="7ffc42a1-d440-4fb9-c8dc-eaa838ba2cda"
l.pop() #list.pop(index)
l

# + id="DqMoGEKxJGn9" colab={"base_uri": "https://localhost:8080/"} outputId="1201d830-ea94-49e6-c9f4-4f863dd4f753"
l.pop(3)
l

# + [markdown] id="DtYagBS_qQFy"
# ### Tuple
#
# ```
# tuple = (...)
# ```

# + id="ueDnLunycWaY" colab={"base_uri": "https://localhost:8080/"} outputId="74fc95cf-57ff-4d05-b27b-2180bf411d72"
t = (0, 1, 2, 3, 4)
t

# + id="au0wM8XCgn_B" colab={"base_uri": "https://localhost:8080/"} outputId="4ac1e4cf-3cff-4a02-c5e9-d9a21d33819e"
len(t)

# + id="_rkBkHYUguGs" colab={"base_uri": "https://localhost:8080/"} outputId="088f89e1-bfc0-403e-c0a0-7d9343a11a26"
t[0:3]

# + id="n3myc9P4hCBD" colab={"base_uri": "https://localhost:8080/"} outputId="333f34e5-9933-4d21-cd45-2486029642d8"
t[-1]

# + id="S0LgakJNhKZE" colab={"base_uri": "https://localhost:8080/"} outputId="d14b2f26-b664-4509-b4a5-017de3f3bd98"
t[-3:]

# + id="vj_zrSjyhZBo" colab={"base_uri": "https://localhost:8080/", "height": 199} outputId="535ec580-aed7-4269-c057-f5a1a7b5ded6"
# TypeError: Cannot edit tuple.
t[4] = 5

# + [markdown] id="69kUPKHLJb__"
# ### Set
#
# ```
# set = {...}
# ```

# + id="IzN2IwqtJehQ" colab={"base_uri": "https://localhost:8080/"} outputId="6197bc64-f7d0-405d-deee-0b3d89f1fbeb"
s = {1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 5, 5}
s

# + id="uI6B8gzXK5ge" colab={"base_uri": "https://localhost:8080/"} outputId="485af2fa-5354-4fb8-8a70-5913ee5aaa5f"
len(s)

# + id="1Ct0_roAK8ea" colab={"base_uri": "https://localhost:8080/"} outputId="3524552e-7d83-4e59-ca77-b56a2f16df67"
0 in s

# + id="acEJe37MK8x-" colab={"base_uri": "https://localhost:8080/"} outputId="0a6c27b1-3d94-49ba-b11f-2463f2fce906"
1 not in s

# + id="bmvtDGRTJqB0"
s1 = {5, 6, 6, 7}

# + id="E_8aIn4YLIBS" colab={"base_uri": "https://localhost:8080/"} outputId="f4a061eb-4584-4942-b87b-bcfd8e305776"
print('s = ', s)
print('s1 = ', s1)
print('s is a subset of s1 : ', s <= s1)
print('s union s1 = ', s | s1)
print('s intersect s1 = ', s & s1)
print('s difference s1', s - s1)
print('s symetric difference s1 = ', s ^ s1)

# + [markdown] id="5ReonijCBhrj"
# ### Dictionary
#
# ```
# dict = {key : value}
# ```

# + id="QvXx0QchiTts" colab={"base_uri": "https://localhost:8080/"} outputId="b0d3115a-a5cc-4aad-8482-5a1251355aea"
grade = {
    "A" : 80,
    "B" : 70,
    "C" : 60,
    "D" : 50
}
grade

# + id="kK1D_Izjq8Kj" colab={"base_uri": "https://localhost:8080/"} outputId="ec5c635e-aad3-4263-a81d-275dce0f4df0"
grade["A"]

# + id="4hSmxQg8rAOa" colab={"base_uri": "https://localhost:8080/"} outputId="27183a1d-4797-4ab5-98ba-d69fd9c82b0e"
grade["B+"] = 75
grade

# + id="TAXxt9Z2hgwz" colab={"base_uri": "https://localhost:8080/"} outputId="74b04c85-2e97-472f-dedb-602a1c3de5a7"
d = {}
d["s001"] = "Dog"
d["s002"] = "Cat"
d

# + id="pJdFriSPBuDy" colab={"base_uri": "https://localhost:8080/", "height": 58} outputId="61c04a36-7a01-472b-d8eb-2e905e3aefe1"
d["s001"]

# + [markdown] id="1wovioh3a9f-"
# ## Print
#
# Print in more detail: https://realpython.com/python-print/

# + id="CHKZusOdbCmf"
# ?print

# + colab={"base_uri": "https://localhost:8080/"} id="IgrLkFIwdBQO" outputId="c751a3df-c17e-4ce2-f89c-5ae2e34dd88c"
name = 'Limpapat'
age = 16
gpa = 10/3
print('Hi! \nMy name is', name, ', \nI\'m', age, 'years old, \nMy GPA:', gpa, '.')

# + colab={"base_uri": "https://localhost:8080/"} id="miS2LmBugN43" outputId="ddac43c6-b10e-445d-a373-7f495dcf03b0"
print('Hi! \nMy name is %s, \nI\'m %d years old, \nMy GPA: %.2f' %(name, age, gpa))

# + colab={"base_uri": "https://localhost:8080/"} id="HkhfL-Lafdgs" outputId="0c0fef4f-d3ff-4244-efd6-fb002c63cdcb"
print('Hi! \nMy name is {}, \nI\'m {} years old, \nMy GPA: {}.'.format(name, age, gpa))

# + colab={"base_uri": "https://localhost:8080/"} id="JDLGP_zkd8oy" outputId="727f7923-11cd-4053-ab66-3ebda959f43b"
print(f'Hi! \nMy name is {name}, \nI\'m {age} years old, \nMy GPA: {gpa}.')

StarcoderdataJupyterScriptsDedupFiltered