Summary of Basic Python Knowledge

Quick Guide to Python Basics: From Zero to Proficiency

Target Audience: Beginners with no background | Career switchers to programming | Developers needing a review

1. Overview of Python Features

Interpreted Language: Code runs directly without compilationDynamic Typing: Variables do not require type declaration, automatically inferredSimplified Syntax: Indentation replaces braces <span>{}</span>, making code more elegantCross-Platform: Runs on Windows/macOS/LinuxStrong Ecosystem: Supports web scraping, data analysis, AI, web development, etc.

2. 7 Essential Basic Syntaxes

1. Variables and Data Types

name = "Alice"       # String (str)
age = 25             # Integer (int)
price = 19.99        # Float (float)
is_student = True    # Boolean (bool)

Key Points:

  • Variable names use <span>snake_case</span> (e.g., <span>user_age</span>)
  • Use <span>type()</span> to check variable types

2. String Operations

text = "Hello Python"
print(text[0])       # Output: H (index starts at 0)
print(text[-1])      # Output: n (negative index means from the end)
print(text[2:5])     # Output: llo (slicing)
print(text.upper())  # Convert to uppercase: HELLO PYTHON

3. Lists and Dictionaries

List (list): An ordered mutable collection

fruits = ["apple", "banana", "orange"]
fruits.append("pear")   # Add element
print(fruits[1])        # Output: banana

Dictionary (dict): Stores key-value pairs

user = {"name": "Bob", "age": 30}
print(user["name"])     # Output: Bob
user["age"] = 31        # Modify value

4. Conditional Statements (if-else)

score = 85
if score >= 90:
    print("Excellent")
elif score >= 60:
    print("Pass")
else:
    print("Fail")

5. Loops (for/while)

# for loop to iterate over a list
for fruit in fruits:
    print(fruit)

# while loop
count = 0
while count < 3:
    print(count)
    count += 1

6. Function Definition (def)

def greet(name):
    """Function to greet"""
    return f"Hello, {name}!"

print(greet("Alice"))  # Output: Hello, Alice!

7. File Read/Write

# Write to file
with open("test.txt", "w") as f:
    f.write("Hello World")

# Read from file
with open("test.txt", "r") as f:
    content = f.read()
print(content)  # Output: Hello World

3. 3 Pitfalls for Beginners

🚫 Trap 1: Avoid directly deleting elements while iterating over a list

# Incorrect approach (will skip elements)
lst = [1, 2, 3, 4]
for num in lst:
    if num % 2 == 0:
        lst.remove(num)

# Correct approach: List comprehension
lst = [num for num in lst if num % 2 != 0]

🚫 Trap 2: Using mutable objects (like lists) as default parameters

# Incorrect approach (will share default list across calls)
def add_item(item, lst=[]):
    lst.append(item)
    return lst

# Correct approach: Use None instead

def add_item(item, lst=None):
    if lst is None:
        lst = []
    lst.append(item)
    return lst

🚫 Trap 3: Confusing <span>==</span> and <span>is</span>

a = [1, 2]
b = [1, 2]
print(a == b)  # True (values are the same)
print(a is b)  # False (not the same object)

Leave a Comment