TOP 5 THINGS IN PYTHON I WISH I KNEW SOONER

Junior Developer Journal
3 min readJan 31, 2024

--

These are things I wish i knew sooner with my multiple years of experience using python.

Lets start right off with the first tip. It is the most useful thing if you need to quickly do a Y/N question.

First Tip

The first tip is useful for asking Y/N questions instead of having strict rules.
What beginners do:

ans = input('Do you want to read another blog? ')
if ans == 'Yes':
print('hey')
else:
print('no')

What we want to do is make it so the audience dont need to follow the rules of typing exactly ‘Yes’. They wouldn’t even be able to type ‘yes’.
What you should do:

yes = ['YES', 'YUP', 'Y']
ans = input('Do you want to read another blog? ')
ans = ans.upper()
if ans in yes:
print('hey')
else:
print('no')

Second Tip

The second tip involves using list comprehensions to create concise and readable lists.

What beginners often do:

squares = []
for num in range(10):
squares.append(num**2)

This helps you compact your text instead of writing all of this code and be more efficient
What you should do:

# Using list comprehension
squares = [num**2 for num in range(10)]

List comprehensions offer a more compact and efficient way to create lists.

Third Tip

The third tip encourages the use of the with statement when working with external resources.

What beginners often do:

file = open('example.txt', 'r')
content = file.read()
file.close()

This helps you to write this into a function style with an indent to make it more clean
What you should do:

# Using with statement
with open('example.txt', 'r') as file:
content = file.read()
# file is automatically closed when exiting the block

The with statement ensures proper resource management.

Fourth Tip

The fourth tip introduces the get() method for dictionaries to handle key retrieval with a default value.

What beginners often do:

if 'key' in my_dict:
value = my_dict['key']
else:
value = default_value

Find a key in a dictionary any time quickly instead of writing a 4 lines 4:1
What you should do:

# Using get() method
value = my_dict.get('key', default_value)

The get() method provides a more concise way to retrieve dictionary values with a fallback.

Fifth Tip

The fifth tip focuses on the importance of meaningful variable names and code comments to enhance code readability and maintainability.

What beginners often do:

# Poor variable naming
x = 10
y = 5
result = x + y

To change the style of compact text, this is about code readability, never have random name ( e.g x, y, A,B )
What you should do:

# Better variable naming and comments
num1 = 10
num2 = 5
sum_result = num1 + num2

Choosing descriptive variable names and adding comments where necessary can significantly improve the clarity of your code. This practice helps both you and others understand the purpose and functionality of different parts of your script.

Jump Up — my game

Hello everyone. I would like to inform you about my game Jump Up. This is my first ever game that i have made and would make my world if you download it. The link is down below as an itch.io website. Please note that as this game is in early versions (1.0.2) this game will not be available on windows, linux, ios, and some more. The only platform this game is available on at this moment in time is: Macbook. Thanks for reading this and please give my game a go and leave some feedback. Thank you everyone, JDJ out.
Get Jump Up ↑ by Junior Dev Journal: Here (Not a straight download link)

Links

Subscribe to JDJ: SUBSCRIBE!
Latest Blog: Here

AD:

Best Trading Platform: Its True!
Get Jump Up ↑ by Junior Dev Journal: Here (Not a straight download link)

--

--

Junior Developer Journal
Junior Developer Journal

Written by Junior Developer Journal

Hello! I am a developer! I program with Python and yeah! Thats it! Hope you enjoy my DevLogs.

Responses (1)