Here are a few tips and tricks for Python beginners:
Use list comprehensions instead of for loops: List comprehensions are a concise way to create lists and are often faster than for loops.
They have the syntax: [expression for item in iterable if condition]. Example
original_list = [1, 2, 3, 4, 5] squared_list = [x**2 for x in original_list] print(squared_list)
output:
[1, 4, 9, 16, 25]
Use the "with" statement when working with files: The "with" statement is used when working with files and automatically takes care of closing the file after it's been used. This eliminates the need to explicitly call the close() method and can help prevent errors.
Example:
with open('example.txt', 'r') as file: contents = file.read() print(contents)
Once the code inside the
with
block is finished executing, the file will automatically be closed. This ensures that the file is closed even if an exception is raised inside thewith
block. The same can be used when writing to a file.Use the built-in function enumerate(): The enumerate() function is used to loop over a list while keeping track of the index of the current item. It returns both the index and the value of the current item.
Example:
fruits = ['apple', 'banana', 'orange'] for index, value in enumerate(fruits): print(f"{index}: {value}")
Use the ternary operator for simple if-else statements: The ternary operator allows you to write simple if-else statements in a single line of code. The syntax is: value_if_true if condition else value_if_false.
value_if_true if condition else value_if_false.
Example
x = 10 y = 20 bigger = x if x > y else y print(bigger)
Output:
20
A more complex example:
age = 20 status = "minor" if age < 18 else "adult" if age < 65 else "senior" print(status)
output:
adult
Get familiar with modules and packages: Python has a large number of modules and packages, many of which are designed to perform specific tasks. It is important to familiarize yourself with the available modules and packages and to use the ones that are appropriate for your task.
some of the modules for example (read more Python's Inbuilt Modules for beginner)
- random
- os
- DateTime and time
- json
Get into the habit of writing documentation: Good documentation is essential for any code you write. This is even more important for beginners, who are still learning the best practices for writing code.
Practice and practice, write more code, and get yourself involved in open-source projects that will help you to improve your skills.
Make use of Python Tutors and Mentors, who can guide and help you to overcome any issues you face while learning.
Get any function doc string in python shell buy adding ? at last and press enter.
Example:
sum?
output:
Signature: sum(iterable, /, start=0)
Docstring:
Return the sum of a 'start' value (default: 0) plus an iterable of numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.
Type: builtin_function_or_method
By implementing these tips and tricks, you can write more efficient and readable Python code, and also it will help you to improve your coding skills.
Do check out Python’s official documentation time to time. Do comment your favorite tricks.