Skip to content

Instantly share code, notes, and snippets.

@nichotined
Last active August 28, 2019 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nichotined/b36ae1273c144a735cf2ffc7950ff3e3 to your computer and use it in GitHub Desktop.
Save nichotined/b36ae1273c144a735cf2ffc7950ff3e3 to your computer and use it in GitHub Desktop.

Programming Class

Compound Data Type

List

List is a sorted array to store multiple data. It can also store multiple types of data.

fruits = ["banana", "apple", "orange", "kiwi"]
numbers = [1, 2, 3, 4, 5, 6, 7]
animals = [{"Dog", 4}, {"Gorilla", 2}]
data = ["name", "full_name", 19]

You can access an item in a list by referring to its index just like accessing String

fruits = ["banana", "apple", "orange", "kiwi"]
print(fruits[0])
print(fruits[1])
print(fruits[0::2])

You can modify the value from a list by referring to its item index

fruits = ["banana", "apple", "orange", "kiwi"]
fruits[0] = "pineapple"
print(fruits)

You can add value to a list by using append function and it will be added in the last index

fruits = ["banana", "apple", "orange", "kiwi"]
fruits.append("pineapple")
print(fruits)

To add value to a list in the specific index, you can use insert function

fruits = ["banana", "apple", "orange", "kiwi"]
fruits.insert(1, "pineapple")
print(fruits)

There are several ways for you to remove an item from a list. You can use remove function, pop function or del function.

fruits = ["banana", "apple", "orange", "kiwi", "pineapple"]
fruits.remove("banana")
print(fruits)

del fruits[0]
print(fruits)

fruits.pop()
print(fruits)

You can empty a list using clear function

fruits = ["banana", "apple", "orange", "kiwi", "pineapple"]
fruits.clear()
print(fruits)

You can completely delete a list using del

fruits = ["banana", "apple", "orange", "kiwi", "pineapple"]
del fruits
print(fruits)    # NameError

You can also get a length from a list using len function

fruits = ["banana", "apple", "orange", "kiwi", "pineapple"]
length = len(fruits)
print(length)

Dictionary

Dictionary is a collection of items which is unordered, indexed, and changeable. It has a key and value pair.

nicholas = {"name": "Nicholas Frederick", "age": 22}
micro_services = {"services":
    [
        {"name": "spider"}, {"name": "vertices"}, {"name": "gandalf"}
    ]
}
qa_members = {
    "Platform": [
        {"name": "Rizky"}, {"name": "Amri"}
    ],
    "Mercury": [
        {"name": "Vicky"}, {"name": "Haira"}, {"name": "Bagus"}
    ]
}

You can access an item in a dictionary using its key

qa_members = {
    "Platform": [
        {"name": "Rizky"}, {"name": "Amri"}
    ],
    "Mercury": [
        {"name": "Vicky"}, {"name": "Haira"}, {"name": "Bagus"}
    ]
}
print(qa_members.get("Platform"))
print(qa_members["Mercury"])

You can add an item to a dictionary by assigning value to a key name

person = {"name": "nicholas"}
person["age"] = 20
print(person)

You can change the value of a specific item by referring to its key name

person = {"name": "nicholas", "age": 20}
person["age"] = 22
print(person)

Note:

Using get function will not throw an error if the key does not exist otherwise using the subscript operator will throw a KeyError

Set

Set is an unordered and unindexed collection of data.

fruits = {"apple", "banana", "orange", "kiwi"}

You cannot access items in a set by referring to an index since sets are unordered the items has no index but, you can check whether an item in a set.

fruits = {"apple", "banana", "orange", "kiwi"}
print("banana" in fruits)

Tuple

A tuple is an immutable data type. Once you created it, you cannot add or remove the data.

cities_in_jawa = ("Jakarta", "Bandung", "Bogor", "Surabaya", "Semarang")

You can access a Tuple item by the item index starting from 0.

cities_in_jawa = ("Jakarta", "Bandung", "Bogor", "Surabaya", "Semarang")
print(cities_in_jawa[0])
print(cities_in_jawa[3])

Assignment

  1. Write a Python script that will take arguments from the command line.
    Your program's behavior should match the example below

    $ python3 list.py hello world my name is maverick
    ['hello', 'world', 'my', 'name', 'is', 'maverick']
    
    $ python3 list.py hello world my name is maverick
    ['hello', 'world', 'my', 'name', 'is', 'maverick']
    
  2. Write a Python script that will ask for some inputs for name, age, and gender.
    Your program's behavior should match the example below

    $ python3 profile.py
    Please enter your name: Nicholas
    Please enter your age: 22
    Please enter your gender: Male
    {'name': 'Nicholas', 'age': 22, 'gender': 'Male'}
    Character in name is 8
    You are a BOY!
    
    $ python3 profile.py
    Please enter your name: Catherine
    Please enter your age: 24
    Please enter your gender: Female
    {'name': 'Catherine', 'age': 22, 'gender': 'Female'}
    Character in name is 9
    You are a GIRL!
    
  3. Consider the following code

    data1 = (1, 2, 3)
    data2 = (1, 2, 3)
    print(data1 == data2)
    print(data1 is data2)

    What are the differences between data1 and data2?
    Why is returns False when == returns True?
    Explain the answer using your own words!

  4. Write a python script that has a list of tuples that each contain the name and number of automated TestRail cases of every member of QA.

  5. Given a dictionary

    {
     "age": 27,
     "eyeColor": "green",
     "name": "Burks Santos",
     "gender": "male",
     "company": "COMBOGENE",
     "email": "burkssantos@combogene.com",
     "address": "159 Linden Street, Detroit, Georgia, 8388",
     "latitude": "-68.759098",
     "longitude": "-142.239765",
     "friends": [
         {
             "id": 0,
             "name": "Kelly Dejesus",
             "gender": "male"
         },
         {
             "id": 1,
             "name": "Le Holden",
             "gender": "male"
         },
         {
             "id": 2,
             "name": "Kline Ray",
             "gender": "male"
         }]
    }

    Write a python program that will ask for a key of above dictionary and print the value of it.
    This program will endlessly ask for a key!
    Your program's behavior should match the example below

    $ python3 data.py
    Give me one of below key, and I will return you the value.
    age, eyeColor, name, gender, company, email, address, latitude, longitude, friends
    Please enter a key: age
    Value of key age is 27
    Please enter a key: email
    Value of key email is burkssantos@combogene.com
    Please enter a key: message
    Value of key message is out of my control
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment