How to improve your Python skills to expert level

Original article was published by Sunil Thakur on Artificial Intelligence on Medium


How to improve your Python skills to expert level

Well to improve your skills in any domain you need to put in a lot of deliberate practice and language skills are no different.

But I will show you few tips so that you can instantly improve your coding skills and style if practiced in your regular coding. Before this I would like to highlight few pointers which is really essential to an expert coder and this comes from number of years of software coding experience I have.

  1. Readability is the key: If you want your code to be reviewed properly and faster please use right name conventions. This also makes easy for you also in case you revisits the same code after few months.
  2. Concise code : This can a bit controversial as depends on person, but I feel that lesser code means less line and hence less chance of bugs but balance between readability and conciseness.
  3. Time matters : Time complexity is of utmost importance if you want to be an expert programmer. So makes sure to check the default time-complexities of the data structures while in-built libraries.

Lets move to the fun part of real code:

1. enumerate : This function helps to iterate.

Arguments : iterable — a sequence, an iterator, or objects that supports iteration.

e.g. look at the variable names, string formatting using f-Strings, enumerate for getting index and element together all in action here.

stocks = ['AAPL', 'TLSA', 'FB', 'NVDA']
for index, stock in enumerate(stocks):
print(f'{index} : {stock}')
Output:
0 : AAPL
1 : TLSA
2 : FB
3 : NVDA

2. join

task_list = ['Task1', 'Task2', 'Task3', 'Task4', 'Task5',]
final_list = ''
for task in task_list:
final_list += task
final_list
Output:
'Task1Task2Task3Task4Task5'

Lets use .join method: This provides two major advantages

The first is the performance . Join method makes the entire copy in one pass

when you provide hundreds or thousands of strings. You get both space and time complexity improvements over the normal concatenation method.

final_list = ''.join(task_list)
final_list
Output:
'Task1Task2Task3Task4Task5'

Another advantage: This helps to provide format connecting string or logic

final_list = '-->'.join(task_list)
final_list
Output:
'Task1-->Task2-->Task3-->Task4-->Task5'

3. List Comprehension:

Normal coding way:

def isNumOdd(num):
return num % 2
numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
new_list = []
for number in numbers_list:
if isNumOdd(number):
new_list.append(number)
new_list
Output:
[1, 3, 5, 7, 9]

Lets look at a concise way of doing this in 1-liner

new_list = [item for item in numbers_list if isNumOdd(item)]
new_list
Output:
[1, 3, 5, 7, 9]

4. Use *args and **kwargs in Python 3:

*args is used to unpack variable numbers of arguments and thus used as a parameter to send a non-keyworded variable-length argument list to functions.

def multiply_numbers(*args):
result = 1
for num in args:
result *= num
return result
multiply(2, 3)
multiply(2, 3, 4)
multiply(2, 3, 4, 5)
multiply(2, 3, 4, 5, 6)
Output:
6
24
120
720

**kwargs is used to pass a keyworded, variable-length argument dictionary to a function. It provides us with flexibility to use keyword arguments in our code

def print_names(**kwargs):
for key, value in kwargs.items():
print("The value of {} is {}".format(key, value))
print_values(first_name="Sam", second_name="Tom")Output:
The value of first_name is Sam
The value of second_name is Tom

5. Lambda Functions:

Lambda functions finds there use where a small functionality is needed.

It can take one to multiple parameters with syntax

lambda parameters: expression

#List of students along with grads
grades = [{'name': 'A', 'grade': 70}, {'name': 'B', 'grade': 80}, {'name': 'C', 'grade': 90}]
# Sort using a lambda
sorted(grades, key=lambda i: i['grade'])
output:
[{'name': 'A', 'grade': 70},
{'name': 'B', 'grade': 80},
{'name': 'C', 'grade': 90}]

Now lets sort by name

#List of students along with grads
grades = [{'name': 'Test_Name', 'grade': 70}, {'name': 'Best_Name', 'grade': 80}, {'name': 'Nest_Name', 'grade': 90}]
# Sort using a lambda
sorted(grades, key=lambda i: i['name'])
output:
[{'name': 'Best_Name', 'grade': 80},
{'name': 'Nest_Name', 'grade': 90},
{'name': 'Test_Name', 'grade': 70}]

For more information please follow the link below for more in-depth documentation and details on python coding style guide.

PEP 8 — Style Guide for Python Code

https://www.python.org/dev/peps/pep-0008/

Thanks for reading this article I hope its helpful to you all ! If you enjoyed this article and found it helpful please leave some claps to show your appreciation.

Keep up the learning.