Python and SQL: Objects in Programming
Tags: python, sql, webdev, beginners
Published: 2024-04-17 17:28:57

I am now in my 9 week of coding bootcamp, which means by next week I will be done 2/3 of my program. Up until now, I have been learning front-end development, or everything we see on websites and their respective backend. For the past 2 1/2 weeks, I have been learning the complete backend with the coding Languages Python and SQL(Search Query Language). Let's delve into it:

**Python Syntax**
Although the logic can be similar, Python syntax is different than Javascript. For example, here's how you write a function in Javascript:
```
function addNumbers(){
    return 9+10
}
```
here's how to write the same function in Python:

```
def add_number():
    return 9+10
```
a function in python is written with `def`, and we also use what developers call "Snake Case" naming where there is an underscore in_between_each_word. "Camel Case" is a naming convention where you capitalizeEachWord except the first one. Indentation is also very important in python.

**Python Classes**
This is one of the most important concepts we studied this phase: `Classes`. Imagine if we are a coder and want to create multiple car companies. They all contain the same info: Name, how many models they have produced, how many they have stopped producing, and the description. We would manually write out a Bunch of objects that would have the info like down below:

```
Ford = {
    "name": "Ford",
    "models produced": 68,
    "models discontinued": 287,
    "description": "They make F-150s"
}
Ferrari = {
    "name": "Ferrari",
    "models produced": 20,
    "models discontinued": 88,
    "description": "They make beautiful red sport cars"
}
```
and then we can access them like so:
```
Ferrari['name']
=> Ferrari
Ford['description']
=> They make F-150s
```
but that would be so tiring to type out for every. Single. Company. Especially if you are doing over 100 of them; it would take a long time to type out. That is where Classes come in: We can create the same type of object so that the data types are consistent across the board. For example, this is what a company class would look like:

```
class Company:
    def __init__(self, name, models_produced, models_discontinued, description):
        self.name = name
        self.models_produced = models_produced
        self.models_discontinued = models_discontinued
        self.description = description
```
and here's how you would create an instance of that class:

```
ferrari = Company("Ferrari", 12, 42, "love this company")
```
so when you try to pass in less or more than 3 things into the company class, it will throw an error. I can also cover getter and setter functions to verify if the information typed in were numbers or letters, but I won't get into that.

**SQL**
What I actually want to get to is `relationships`. In real life they are complicated, and in Python they are complicated. There are three types: `One to One`, `One to Many`, and `Many to Many`. An example of a One to One relationship would be like a dog that owns a single toy: the toy has one dog and the dog has one toy. Another example of a One to Many relationship would be that same dog having many toys: one dog can have many toys, but each toy can only have one dog. The final concept is a many to many relationship; think of a dog daycare. Many dogs share many toys, so a toy can have many dogs, and a dog can have many toys. Simple, right? The main functionality of SQL is inserting data into `tables`. Similarly to a spreadsheet, these tables have rows, columns and cells. one instance of a dataset is a row, the the type and name of the data is the column, and the whole dataset is the table itself. We use SQL to create multiple tables and establish relationships with each other, read, create, update and delete data from these tables. Due to the shortness of this article, I will not go into the syntax of SQL but it has been very fun to learn and very simple

With only 2 more phases to go, I can't wait to continue to learn more, even with the headaches of Python. See you guys in the next one!