Repository navigation
CommonPrintErr
Welcome, Python learners! This guide will help you identify and avoid common mistakes that beginners often make when starting to code in Python. By understanding these pitfalls, you'll be able to write cleaner, more efficient code and troubleshoot issues more effectively.
print "Hello, World!"print("Hello, World!")In Python 3, print() is a function and requires parentheses. Always remember to enclose your arguments in parentheses when calling functions.
print("She said "hello" and left.')print("She said \"hello\" and left.")Ensure that you use matching quotes (either single or double) to enclose strings. Use escape characters (\) when you need to include quotes within a string.
if True:
print("This is indented incorrectly")if True:
print("This is indented correctly")Python uses indentation to define code blocks. Consistent indentation is crucial for your code to run correctly.
Print("This won't work")print("This will work")Python is case-sensitive. Built-in functions like print() should always be in lowercase.
print("Open parenthesis"print("Closed parenthesis")Always ensure that you close all opened parentheses in your code.
print("Hello" + "World") # Outputs: HelloWorldprint("Hello", "World") # Outputs: Hello World
print("Hello" + " " + "World") # Outputs: Hello WorldUse commas to separate items in print() statements if you want automatic spacing. Use + to concatenate strings without spaces.
print("It's not correct")print("It\'s correct")
print("Use \\ to print a backslash")Use backslashes to escape special characters in strings. Double backslashes are needed to print an actual backslash.
-
Read Error Messages: Error messages are your friends. They often point directly to the line and nature of the problem.
-
Use a Good IDE: Many integrated development environments (IDEs) can catch these errors before you run your code.
-
Practice Consistently: The more you code, the more familiar you'll become with Python's syntax and common pitfalls.
-
Review Your Code: After writing, take a moment to review your code for these common mistakes.
-
Learn from Mistakes: When you encounter an error, take time to understand why it occurred and how to prevent it in the future.
Remember, making mistakes is a natural part of the learning process. By being aware of these common errors, you'll be better equipped to write clean, efficient Python code. Happy coding!