Skip to content

Latest commit

 

History

History
195 lines (143 loc) · 4.65 KB

File metadata and controls

195 lines (143 loc) · 4.65 KB

Hello Python - Key Concepts

Quick reference guide for your first Python concepts. Use this as a cheat sheet when working on problems or projects.

📋 Quick Summary

Python is a beginner-friendly programming language. You can run Python interactively (REPL) or write scripts (.py files). The print() function displays output, and comments help document your code.

🔑 Core Concepts

Concept 1: Running Python

What it is:
Python can be run in two ways: interactively in the REPL or by executing script files.

Syntax:

# Start interactive Python
python3

# Run a script
python3 filename.py

When to use:

  • REPL: Quick tests, calculations, experiments
  • Scripts: Code you want to save and run repeatedly

Concept 2: The print() Function

What it is:
A built-in function that displays output to the screen.

Syntax:

print(value)
print(value1, value2, ...)

Common use cases:

# Print text
print("Hello, World!")

# Print numbers
print(42)

# Print multiple values
print("The answer is:", 42)

# Customize separator and ending
print("A", "B", "C", sep="-")     # A-B-C
print("Hello", end=" ")
print("World")                     # Hello World

When to use:

  • Display results to the user
  • Debug your code (see what's happening)
  • Create output for your programs

Concept 3: Comments

What they are:
Notes in your code that Python ignores.

Syntax:

# Single-line comment

"""
Multi-line comment
Can span multiple lines
"""

Common use cases:

# Explain what code does
print("Hello")  # Inline comment

# Temporarily disable code
# print("This won't run")

"""
Document larger sections
Explain complex logic
Leave notes for future reference
"""

When to use:

  • Explain why (not just what) your code does something
  • Document complex logic
  • Leave TODO notes
  • Temporarily disable code while testing

⚡ Common Patterns

Pattern 1: Basic Output

# Simple message
print("Welcome to Python!")

Pattern 2: Multiple Line Output

# Multiple related messages
print("Name: Alice")
print("Age: 25")
print("City: New York")

Pattern 3: Formatted Display

# Aligned output with tabs
print("Item\t\tPrice")
print("Coffee\t\t$3.50")
print("Muffin\t\t$2.75")

⚠️ Common Pitfalls

Pitfall Example Why It's Wrong Correct Approach
Missing quotes print(Hello) Python looks for a variable print("Hello")
Mismatched quotes print("Hello') Quotes don't match print("Hello") or print('Hello')
Forgetting parentheses print "Hello" Old Python 2 syntax print("Hello")
Wrong command Using python instead of python3 Might use Python 2 Always use python3

💡 Best Practices

  1. Use Python 3

    • Always use python3 command
    • Python 2 is outdated
  2. Add comments

    • Explain what your code does
    • Your future self will thank you
  3. Test frequently

    • Run your code often
    • Catch errors early

🔧 Useful Escape Characters

Character Effect Example Output
\n New line print("Hi\nBye") Hi
Bye
\t Tab print("Name:\tJohn") Name:    John
\" Quote mark print("Say \"Hi\"") Say "Hi"
\\ Backslash print("C:\\Users") C:\Users

📊 print() Parameters

Parameter Purpose Example
(default) Values to print print("Hello", 42)
sep Separator between values print("A", "B", sep="-") → A-B
end What to print at end print("Hi", end="!") → Hi!

🎯 Checklist

Use this checklist when writing your first programs:

  • Python 3.8+ is installed (python3 --version)
  • File ends with .py extension
  • Text is enclosed in quotes
  • print() has parentheses
  • Quotes match (both single or both double)
  • Code runs without errors

📚 Related Topics

🔗 Quick Links


💡 Pro Tip: Run your code frequently. Don't write 20 lines and then test—write 2-3 lines, test, then continue. This makes debugging much easier!

🎓 Remember: Everyone starts here. The print() function might seem simple, but it's one of the most used functions in Python!