DEV Community

Cover image for Mastering python the right way
Marriane Akeyo
Marriane Akeyo

Posted on

Mastering python the right way

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.
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

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)
Enter fullscreen mode Exit fullscreen mode
  • Selection best for decision making eg if else and nested if statements.
day = rainy
if day == rainy:
   print("Carry an umbrella")
Enter fullscreen mode Exit fullscreen mode
  • 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])
Enter fullscreen mode Exit fullscreen mode

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 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())
Enter fullscreen mode Exit fullscreen mode

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())

Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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,FastApi and Django, 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.

Top comments (0)