Posts tagged with #python

Python Errors: All in One Place

Every Python developer encounters errors, especially when starting out. Instead of being frustrated by them, treat errors as clues that help you understand how Python works.

1. SyntaxError: EOL while scanning string literal

EOL stands for End Of Line. Python reads your string from the opening quote and expects a matching closing quote on the same line. If it reaches the end of the line without finding one, it throws this error.

# ❌ Broken — closing quote missing
print("Hello, Parthi)

# ✅ Fixed
print("Hello, Parthi")

2. IndentationError: unexpected indent

Python uses whitespace to define structure, no curly braces. Every block (inside an if, a for, a function) must be indented consistently. An unexpected indent means a line has leading spaces where Python expected none.

# ❌ Broken — indented for no reason
name = "Parthi"
    print(name)

# ✅ Fixed — back at the base level
name = "Parthi"
print(name)

# ✅ Also valid — indented correctly inside a block
if name == "Parthi":
    print("Welcome!")

3. NameError: name '…' is not defined

Python evaluates names at runtime. If you reference a variable that hasn't been assigned yet or you've misspelled it, NameError occurs.

# ❌ Broken — used before assignment
print(total)
total = 100

# ❌ Broken — typo (underscore vs no underscore)
username = "parthi"
print(user_name)

# ✅ Fixed
total = 100
print(total)

username = "parthi"
print(username)

Errors happen to everyone; Keep going.

Basic Python Concepts in One Place

Printing Output

The print() function is used to display output on the screen.

print("Hello, World!")

Strings of Characters

A string is a sequence of characters enclosed in single or double quotes. Strings can contain letters, numbers, spaces, and symbols.

print("I am learning Python")

String Concatenation

Concatenation means joining strings together.

first_name = "Parthi"
last_name = "Ban"

full_name = first_name + last_name
print(full_name)

You can also add spaces manually.

full_name = first_name + " " + last_name
print(full_name)

Input Function

The input() function allows users to enter data.

name = input("Enter your name: ")

print("Hello, " + name)

Commenting

Comments are notes in your code that Python ignores.

Single-line Comment

# This is a comment
print("Hello")

Multi-line Comment

"""
This is a multi-line comment.
Used to explain code.
"""

Variables

Variables are used to store data.

name = "Parthi"
age = 25

Variable Naming

Python variable names should be meaningful and follow certain rules.

  • Valid Variable Names
name = "Parthi"
user_age = 25
total_marks = 450
  • Invalid Variable Names
2name = "Parthi"
user-age = 25
class = "Python"

len() Function

The len() function returns the length of a string.

name = "Python"

print(len(name))

Conclusion

These concepts form the foundation of Python programming. Understanding printing, strings, variables, user input, comments, string operations, and naming conventions will make it easier to learn functions, loops, conditions, and other advanced Python topics later.

Happy Coding!