Thought leadership from the most innovative tech companies, all in one place.

5 Python Concepts That Will Advance Your Career

Using these concepts in your code will make you a seasoned Python programmer!

image

Python is a high-level, object-oriented programming language that has recently been picked up by a lot of students as well as professionals due to its versatility, dynamic nature, robustness, and also because it is easy to learn. Not only this, it is now the second most loved and preferred language after JavaScript and can be used in almost all technical fields.

Demand for Python developers is increasing and will keep increasing in the next few years. The following concepts are essential for any developer that wants to ride that wave in the future.

1. Understanding lists and dictionaries

This is a common misconception for new developers. Let’s say you create a list ‘x’ and then, assign this list to a new variable:

x = [9,8,7]
y = x

Now, try appending a new value in the y list and then print both lists:

y.append(6)
print(y)            # Prints [9,8,7,6]
print(x)            # Prints [9,8,7,6]

You must be wondering why does the new value has been appended to both lists! This happens because when assigning lists in Python, unless otherwise specified, the list is not copied. Instead, a new reference to this list is created i.e; y is just a reference to the list.

This means that operations in both variables will be reflected in the same list. To make a copy of the list, you need to use the .copy() method:

x = [9,8,7]
y = x.copy()
y.append(6)
print(y)           # Prints [9,8,7,6]
print(x)           # Prints [9,8,7]

2. Context managers

Context Managers are a great tool in python that help in resource management. They allow you to allocate and release resources when you want to. Context managers make sure that all aspects of a resource are handled properly. The most used and recognized example of a context manager is with the statement. with is mostly used to open and close a file.

file = open(‘data.txt’,’w’)
try:
  file.write(“Follow Me”)
except:
  file.close()

With the help of the context manager, you can do the task of opening a file in write mode and also closing it if something goes wrong in just one line precisely. The main advantage of using with is that it makes sure that our file will be closed at the end.

with open (‘data.txt’,’w’) as f:
  f.write(“Follow Me”)

Notice that we never called the f.close() method. The context manager handled it automatically for us, and it would try to do is as well, even if an exception was raised. There are many use cases that context managers can be used (i.e. aiohttp.ClientSession ) and of course, you can create your own.

3. Generators

Generators are a kind of function that returns an object that can be iterated over. It contains at least a yield statement. yield is a keyword in python that is used to return a value from a function without destroying its current state or reference to a local variable. A function with a yield keyword is called a generator. A generator generates an item only once when they ask for it. They are very memory efficient and take less space in the memory.

Example (Fibonacci Series Using Generators) —

def fib(limit):
  a,b = 0,1
  while a < limit:
      yield a
      a, b = b, a + b
for x in fib(10):
   print (x)

The difference between yield and return is that return terminates the function but yield only pauses the execution of the function and returns the value against it each time.

4. Type hinting

Type hinting enables you to write clean and self-explanatory code. The way you apply it is by “hinting” the type of a parameter and the return value of a function. For example, we want to validate the text input of a user is always an integer. To achieve that, we write a function that returns True or False based on our validations:

def validate_func(input):
 *...*

Now that you know what this function does, it is pretty easy to understand by looking at the definition. But, it would not be that easy if you were not given the description above. What is the type of input parameter? Where does it come from? Is it already an integer? What if it is not? Does the function return anything, or just raises an exception? These questions can be answered, by refactoring the code to this:

def validate_func(input: str) -> bool:
 *...*

Now, this function is easier to be interpreted, even by someone who reads this for the first time.

5. Logging

Logging is a process of capturing the flow of code as it executes. Logging helps in debugging the code easily. It is usually done in files so that we can retrieve them later. In python, we have a library logging that helps us to write logs onto a file. There are five levels of logging:

  1. Debug: Used for diagnosing the problem with detailed information.
  2. Info: Confirmation of success.
  3. Warning: when an unexpected situation occurs.
  4. Error: Due to a more serious problem than a warning.
  5. Critical: Critical error after which the program can’t run itself.

I will be writing a dedicated article on “Logging In Python” soon. Subscribe to get an email for when I will publish it.

Final Thoughts

Well, here are the Top 5 Python Concepts That Will Advance Your Career. The points described above, are only some of the Python insights experienced developers are keeping in mind.

I hope you find this article helpful and learned some new things. Share this awesome article with your Pythoneer Friends.😀

Till then see you in my next article…




Continue Learning