Python Programming Basics: Syntax, Functions, and Tips
Python Programming Basics: Syntax, Functions, and Tips
1. Variables and Data Types:
- Examples: `x = 10`, `name = "Alice"`
- Common types: int, float, str, list, dict.
2. Control Structures:
- If-else: `if x > 0: print("Positive") else: print("Negative")`
- Loops: `for i in range(5): print(i)`
3. Functions:
- Defining: `def greet(name): return f"Hello, {name}"`
- Calling: `greet("Bob")`
4. Modules:
- Importing: `import math`
- Using: `math.sqrt(16)`
5. Tips:
- Use comments for readability: `# This is a comment`
- Debugging: Use `print()` or IDE debuggers.
Code Snippet Example:
```
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(5)) # Output: 120
```