Featured Post

15 Python Tips : How to Write Code Effectively

Image
 Here are some Python tips to keep in mind that will help you write clean, efficient, and bug-free code.     Python Tips for Effective Coding 1. Code Readability and PEP 8  Always aim for clean and readable code by following PEP 8 guidelines.  Use meaningful variable names, avoid excessively long lines (stick to 79 characters), and organize imports properly. 2. Use List Comprehensions List comprehensions are concise and often faster than regular for-loops. Example: squares = [x**2 for x in range(10)] instead of creating an empty list and appending each square value. 3. Take Advantage of Python’s Built-in Libraries  Libraries like itertools, collections, math, and datetime provide powerful functions and data structures that can simplify your code.   For example, collections.Counter can quickly count elements in a list, and itertools.chain can flatten nested lists. 4. Use enumerate Instead of Range     When you need both the index ...

Python Syntax Errors Cheat Sheet

Here's is the Python syntax errors cheat sheet. In Python, you can avoid errors, if you know syntax rules. These are missing semicolons, adding extra commas, and extra spaces. Further Python is case sensitive. So using the wrong identifier also will give error.

Python syntax cheat sheet


Python Syntax Errors Cheat Sheet

Indentation is unique to Python. You cannot find strict indentation in any other programming language. You need to focus on the three areas while writing a Python program. To avoid errors you need to learn indentation rules.

  • Indentation or Syntax Errors
  • Exceptions
  • Handling Exceptions

1: Indentation

If you do not follow the proper order, you will get an error. The details of one block shroud follow in one vertical line.

The sub-block should be inside of that. In if loop, the IF, ELIF, and ELSE should have the same indentation. Not only, but the statement inside of them should also have the same indentation.

Best Examples

Understand these examples a good material on indentation for you.

2: Exceptions


Python raises an exception, when it encounters an error. These are called system errors.

Python has many built-in exceptions. One best example is ZeroDivisionError

More exceptions all list in one place.

3: Handling Exceptions


To handle exception you need two blocks which are Try and Except. Try Block is the block where you can give your code. Except is the Block where your exception message comes here.

Python Syntax Example:

try:
d = int(20)
f = str
g = d + f
except TypeError:
  print('This is TypeError Exception')
else :
  print('No exception happened')

Comments

Popular posts from this blog

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)