Tips to write better code in python

Tips to write better code in python

Every beginner aspires to become a pro programmer. To become better at writing python code practice is what you need but more than practice you should learn to read other developer code.

Like books and experience makes a man wiser you can become a professional in programming by learning from other programmers and reading their code, adapting their strategy to solve a problem.

One more way to get better at programming is to listen to talks on the development process by programmers. You can find these podcasts on Spotify, Amazon, google podcast any other podcast hosting platform.

If you are new to listening to books and podcasts you can try amazon audible free for 30 days and see if it works for you and you gain some knowledge.

If you want to have list of good python & programming podcasts drop a comment below.

Coming back to how to get better at programming, understanding code written by others, you can gain a lot

  • You can find new concepts
  • Get an idea of what to use where.
  • Become better at writing clean code
  • Build an attitude of learning from others.
  • Networking by suggesting.

You can explore infinite code committed on Github, It will take time to get started but if you are truly passionate about coding you will search for the answers and become proficient at programming.

Below are some of the tips which I have compiled to boost your python programming skills and write better code.

Use f-strings

Embed expression inside strings using minimal syntax.

Eg:

name="Electroica"
print(f'Hello {name}')

Chaining of the Comparison operator

Instead of writing

if a<b and b<c:

chain them

if a < b < c:
	
if a <=b > c < d is not f

List comprehension

Define list and content at the same time

#check even numbers in a list
bag = [ 1,2,3,4,5,6,7,8]
even= [i for i in bag if i%2==0]

Enumerate

Access index of elements in a list

coding_in = ["Python", "java", "C","C++"]
for index, value in enumerate(coding_in):
	print(index,value)

#result
0 Python
1 java
2 C
3 C++

Virtual Environments

No more dependencies and version clashes errors while working on multiple projects

python -m venv projectbeta

Multiple Inputs

Split the string

#Name of four friends
f1,f2,f3,f4 = input("Enter Four Friends name").split()

Use lambda functions

Anonymous function with multiple arguments but single expression.

#sum of n numbers
get_sum = lambda a : sum(range(0,a+1))
print(get_sum(100))

Thank you for reading, Happy Learning, drop your suggestion in the comments.

Feel free to follow us on Youtube, Linked In , Instagram

Loading comments...
Mastodon