Mastering python the right way
Tags: wecoded, python, theycoded
Published: 2022-03-04 22:47:51

>Python is a high level programming language  with alot of benefits  to beginners ,intermidiate and also experts in programming.It is also one of the functional programming languages .For more information about it's syntax and getting started with python , check out my article [here](https://dev.to/marrie/introduction-to-modern-python-48hh).
Today however, we are going to focus on the tricks in understanding and mastering this language the right way.
We can further break down our mastery into the following:

**Basic syntax**
The basic syntax of any language never change.It is therefore important start by understanding the foundations that make up the language.They include the simple operations,datatypes ,comparisons,functions and so on as described [here](https://dev.to/marrie/introduction-to-modern-python-48hh)

**Control Structures**
This describes the order in which we excecute our code.This is ussually made possible using loops and function calls.They are classified into three main group

- Sequencial-which is the default mode.As the name suggests,excecution happens in a sequence such that the later line of code depends on the former one.

```
a=4
b=5
c=5%4
print(c)
```

- Selection best for decision making eg if else and nested if statements.

```
day = rainy
if day == rainy:
   print("Carry an umbrella")
```

- Repetition used to excecute code more than once.For example while loops and  for loops

```
numbers=[1,2,3,4,5]
for i in numbers:
  print(numbers[i])
```


**Data Structures**
_List_ for example,store multiple items in a single variable.
_Tuples_ are simillar to lists, except that they cannot be changed and use parantesis ().
_Dictionaries_,store data in form of key,value pairs which map abitrary keys to their values.A _set_ on the other hand is an unordered collection of items and every set element stored os unique and must be immutable.
You can visit this [link](https://dev.to/marrie/introduction-to-modern-python-48hh) for perfect code examples on the same.

---
Mastering when to use what data structure is also very important aspect.

It is therefore important to know that:

- Dictionaries are used when you need a logical association between a key and value pair,when you need a fast look up for your data based on a custom key or when your data is being constantly modified since they are mutable.

- Lists when dealing with data that does not deal with random access.

- Sets when the elements are unique in nature.

- Tuples when your data cannot change.

**Indentation and whitespaces**
Python uses a maximum of 4 whitespaces for indentation and a minimum of one, however ,the number of spaces must be uniform in a block of code.The first line of code cannot be indented.Indentation begins from the second line if need be.This reduces the lines of code and also increases readability.

---

**Decorators**
They aid in modifying functions or extend their functionality using other functions.We use the @ symbol followed by the decorator_name.   

```
def meal_decorator(function):
       def cook():
          return "preparing something nice"
   @meal_decorator
   def eat():
      return "Lets dive in"
   print (eat())
```

**Python Classes**
A class is a blueprint of an object and in turn we can classify an object as an instance of a class.
It contains attributes and methods as shown below. 

```
class Person:
     name = "John" #attribute

     def skate(self,wheels):
        return f'{name}, skates using {wheels}' 
     
     #constructor
     def __init__(self,age):
       self.age = age
#instantiate
Student = Person(25)

print(Student.skate())
  
```

It is important to master that a class can also inherit from another class without modifying it.The class thus becomes a child class of the parent class and has all the characteristics of the parent class.
In this case we shall just add the parent name to the new class as follows 


```
class Banker[Person]
```

 **Encapsulation**
This involves hiding the complexity of the code from the user .
**Polymorphisim**
Involves using a common interface for many forms.

**Frameworks**
Lastly we shall look at frameworks .Python frameworks help hide the complexity of the code into something simple that can be understood and used by many people.Some common python frameworks include [FlaskAPI](https://flask.palletsprojects.com/en/2.0.x/),[FastApi](https://fastapi.tiangolo.com/) and [Django](https://docs.djangoproject.com/en/4.0/), just to mention a few. Each framework has its own strength and weaknesses, depending on the task at hand.Having a better understanding of this also enables one to master python the right way.

I hope this gives you an incite  of the modern python and ts mastery.Thanks for reading.