Spaces:
Sleeping
Sleeping
File size: 14,698 Bytes
a77376b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# Python Programming: Complete Guide from Basics to Advanced
## Introduction to Python
Python is a high-level, interpreted, and general-purpose programming language created by Guido van Rossum and first released in 1991. Known for its clear syntax and readability, Python has become one of the most popular programming languages in the world, especially in fields like data science, web development, automation, and artificial intelligence.
### Why Python?
**Advantages**:
- **Easy to Learn**: Simple, readable syntax resembling natural language
- **Versatile**: Used in web development, data science, AI, automation, and more
- **Large Community**: Extensive documentation and community support
- **Rich Ecosystem**: Thousands of libraries and frameworks
- **Cross-Platform**: Runs on Windows, macOS, Linux
- **Open Source**: Free to use and distribute
- **High-Level**: Automatic memory management
- **Interpreted**: No compilation needed, quick development cycle
**Use Cases**:
- Data Science and Machine Learning
- Web Development (Django, Flask)
- Automation and Scripting
- Scientific Computing
- Game Development
- Desktop Applications
- DevOps and System Administration
## Python Basics
### Installation and Setup
```python
# Check Python version
python --version # or python3 --version
# Install packages using pip
pip install package_name
# Create virtual environment
python -m venv myenv
# Activate virtual environment
# Windows:
myenv\Scripts\activate
# macOS/Linux:
source myenv/bin/activate
```
### Variables and Data Types
```python
# Variables (no declaration needed)
name = "Alice" # String
age = 25 # Integer
height = 5.6 # Float
is_student = True # Boolean
empty = None # NoneType
# Type checking
print(type(name)) # <class 'str'>
# Type conversion
num_str = "42"
num_int = int(num_str) # 42
num_float = float(num_str) # 42.0
str_num = str(num_int) # "42"
```
### Basic Data Structures
#### Lists (Mutable, Ordered)
```python
# Creating lists
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "two", 3.0, True]
# Accessing elements
first = fruits[0] # "apple"
last = fruits[-1] # "cherry"
slice = fruits[1:3] # ["banana", "cherry"]
# Modifying lists
fruits.append("orange") # Add to end
fruits.insert(1, "grape") # Insert at index
fruits.remove("banana") # Remove item
popped = fruits.pop() # Remove and return last
fruits[0] = "pear" # Change item
# List operations
length = len(fruits)
sorted_fruits = sorted(fruits)
fruits.reverse()
fruits.sort()
# List comprehension
squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]
```
#### Tuples (Immutable, Ordered)
```python
# Creating tuples
coordinates = (10, 20)
single = (42,) # Single element tuple
empty = ()
# Accessing elements
x = coordinates[0] # 10
y = coordinates[1] # 20
# Unpacking
x, y = coordinates
a, *rest, b = (1, 2, 3, 4, 5) # a=1, rest=[2,3,4], b=5
# Tuples are immutable
# coordinates[0] = 15 # This raises an error
```
#### Dictionaries (Key-Value Pairs)
```python
# Creating dictionaries
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Accessing values
name = person["name"]
age = person.get("age")
city = person.get("country", "Unknown") # Default value
# Modifying dictionaries
person["email"] = "alice@example.com" # Add
person["age"] = 26 # Update
del person["city"] # Delete
person.pop("email") # Remove and return
# Dictionary operations
keys = person.keys()
values = person.values()
items = person.items()
# Dictionary comprehension
squares = {x: x**2 for x in range(6)}
```
#### Sets (Unique, Unordered)
```python
# Creating sets
fruits = {"apple", "banana", "cherry"}
numbers = set([1, 2, 2, 3, 3, 3]) # {1, 2, 3}
empty = set()
# Set operations
fruits.add("orange")
fruits.remove("banana")
fruits.discard("grape") # No error if doesn't exist
# Mathematical set operations
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
union = a | b # {1, 2, 3, 4, 5, 6}
intersection = a & b # {3, 4}
difference = a - b # {1, 2}
symmetric_diff = a ^ b # {1, 2, 5, 6}
```
### Control Flow
#### Conditional Statements
```python
# if-elif-else
age = 18
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
else:
print("Adult")
# Ternary operator
status = "Adult" if age >= 18 else "Minor"
# Multiple conditions
if age >= 18 and has_license:
print("Can drive")
if name == "Alice" or name == "Bob":
print("Welcome!")
```
#### Loops
```python
# For loops
for i in range(5):
print(i) # 0, 1, 2, 3, 4
for fruit in ["apple", "banana", "cherry"]:
print(fruit)
for i, fruit in enumerate(fruits):
print(f"{i}: {fruit}")
for key, value in person.items():
print(f"{key}: {value}")
# While loops
count = 0
while count < 5:
print(count)
count += 1
# Break and continue
for i in range(10):
if i == 3:
continue # Skip 3
if i == 7:
break # Stop at 7
print(i)
# Else clause (runs if loop completes normally)
for i in range(5):
print(i)
else:
print("Loop completed")
```
### Functions
```python
# Basic function
def greet(name):
return f"Hello, {name}!"
result = greet("Alice")
# Default parameters
def power(base, exponent=2):
return base ** exponent
print(power(3)) # 9
print(power(3, 3)) # 27
# Variable arguments
def sum_all(*args):
return sum(args)
print(sum_all(1, 2, 3, 4)) # 10
# Keyword arguments
def person_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
person_info(name="Alice", age=25, city="NYC")
# Lambda functions
square = lambda x: x**2
add = lambda x, y: x + y
# Higher-order functions
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))
evens = list(filter(lambda x: x % 2 == 0, numbers))
from functools import reduce
product = reduce(lambda x, y: x * y, numbers)
```
### Object-Oriented Programming
```python
# Class definition
class Dog:
# Class variable
species = "Canis familiaris"
# Constructor
def __init__(self, name, age):
self.name = name # Instance variable
self.age = age
# Instance method
def bark(self):
return f"{self.name} says Woof!"
# String representation
def __str__(self):
return f"{self.name} is {self.age} years old"
# Creating instances
buddy = Dog("Buddy", 5)
miles = Dog("Miles", 3)
print(buddy.bark()) # Buddy says Woof!
print(miles) # Miles is 3 years old
# Inheritance
class GoldenRetriever(Dog):
def __init__(self, name, age, color):
super().__init__(name, age)
self.color = color
def fetch(self):
return f"{self.name} is fetching!"
max_dog = GoldenRetriever("Max", 2, "golden")
# Encapsulation
class BankAccount:
def __init__(self, balance):
self.__balance = balance # Private attribute
def deposit(self, amount):
if amount > 0:
self.__balance += amount
def get_balance(self):
return self.__balance
# Polymorphism
class Cat:
def speak(self):
return "Meow"
class Dog:
def speak(self):
return "Woof"
def animal_sound(animal):
print(animal.speak())
animal_sound(Cat()) # Meow
animal_sound(Dog()) # Woof
```
## Advanced Python Concepts
### Decorators
```python
# Function decorator
def uppercase_decorator(func):
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result.upper()
return wrapper
@uppercase_decorator
def greet(name):
return f"hello, {name}"
print(greet("alice")) # HELLO, ALICE
# Decorator with parameters
def repeat(times):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(times):
result = func(*args, **kwargs)
return result
return wrapper
return decorator
@repeat(3)
def say_hello():
print("Hello!")
```
### Generators
```python
# Generator function
def count_up_to(n):
count = 1
while count <= n:
yield count
count += 1
counter = count_up_to(5)
for num in counter:
print(num) # 1, 2, 3, 4, 5
# Generator expression
squares = (x**2 for x in range(10))
# Fibonacci generator
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
fib = fibonacci()
first_10 = [next(fib) for _ in range(10)]
```
### Context Managers
```python
# File handling with context manager
with open('file.txt', 'r') as file:
content = file.read()
# File automatically closed
# Custom context manager
class DatabaseConnection:
def __enter__(self):
print("Opening connection")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Closing connection")
def query(self, sql):
print(f"Executing: {sql}")
with DatabaseConnection() as db:
db.query("SELECT * FROM users")
```
### Exception Handling
```python
# Try-except
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
except Exception as e:
print(f"An error occurred: {e}")
else:
print("No errors!")
finally:
print("Cleanup code")
# Raising exceptions
def divide(a, b):
if b == 0:
raise ValueError("Denominator cannot be zero")
return a / b
# Custom exceptions
class InsufficientFundsError(Exception):
pass
def withdraw(balance, amount):
if amount > balance:
raise InsufficientFundsError("Not enough funds")
return balance - amount
```
### List, Dict, and Set Comprehensions
```python
# List comprehension
squares = [x**2 for x in range(10)]
evens = [x for x in range(20) if x % 2 == 0]
matrix = [[i*j for j in range(3)] for i in range(3)]
# Dictionary comprehension
square_dict = {x: x**2 for x in range(6)}
flipped = {v: k for k, v in person.items()}
# Set comprehension
unique_squares = {x**2 for x in [-2, -1, 0, 1, 2]}
# Nested comprehension
flattened = [num for row in matrix for num in row]
```
### Itertools and Collections
```python
from itertools import *
from collections import *
# Itertools
combinations_list = list(combinations([1, 2, 3], 2))
permutations_list = list(permutations([1, 2, 3], 2))
product_list = list(product([1, 2], ['a', 'b']))
# Infinite iterators
counter = count(start=10, step=2)
cycler = cycle(['A', 'B', 'C'])
repeater = repeat(10, times=3)
# Collections
counter = Counter(['a', 'b', 'a', 'c', 'b', 'a'])
# Counter({'a': 3, 'b': 2, 'c': 1})
default_dict = defaultdict(list)
default_dict['key'].append('value')
ordered_dict = OrderedDict()
ordered_dict['first'] = 1
ordered_dict['second'] = 2
named_tuple = namedtuple('Point', ['x', 'y'])
p = named_tuple(10, 20)
print(p.x, p.y)
```
## File Handling
```python
# Reading files
with open('file.txt', 'r') as f:
content = f.read() # Read entire file
lines = f.readlines() # Read lines into list
for line in f: # Iterate line by line
print(line.strip())
# Writing files
with open('output.txt', 'w') as f:
f.write("Hello, World!\n")
f.writelines(["Line 1\n", "Line 2\n"])
# Appending
with open('output.txt', 'a') as f:
f.write("Appended text\n")
# Working with CSV
import csv
with open('data.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Name', 'Age'])
writer.writerows([['Alice', 25], ['Bob', 30]])
# Working with JSON
import json
data = {'name': 'Alice', 'age': 25}
# Write JSON
with open('data.json', 'w') as f:
json.dump(data, f, indent=2)
# Read JSON
with open('data.json', 'r') as f:
data = json.load(f)
```
## Popular Python Libraries
### NumPy (Numerical Computing)
```python
import numpy as np
# Arrays
arr = np.array([1, 2, 3, 4, 5])
matrix = np.array([[1, 2, 3], [4, 5, 6]])
# Operations
print(arr * 2)
print(arr + 10)
print(np.mean(arr))
print(np.std(arr))
# Array creation
zeros = np.zeros((3, 3))
ones = np.ones((2, 4))
range_arr = np.arange(0, 10, 2)
linspace = np.linspace(0, 1, 5)
```
### Pandas (Data Manipulation)
```python
import pandas as pd
# DataFrames
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'city': ['NYC', 'LA', 'Chicago']
})
# Operations
print(df.head())
print(df.describe())
print(df['age'].mean())
# Filtering
adults = df[df['age'] >= 30]
# Reading data
df = pd.read_csv('data.csv')
df = pd.read_excel('data.xlsx')
df = pd.read_json('data.json')
```
### Requests (HTTP Library)
```python
import requests
# GET request
response = requests.get('https://api.example.com/data')
data = response.json()
# POST request
payload = {'key': 'value'}
response = requests.post('https://api.example.com/submit', json=payload)
# With headers
headers = {'Authorization': 'Bearer token'}
response = requests.get('https://api.example.com/protected', headers=headers)
```
### Matplotlib (Visualization)
```python
import matplotlib.pyplot as plt
# Line plot
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Line Plot')
plt.show()
# Bar chart
categories = ['A', 'B', 'C']
values = [10, 20, 15]
plt.bar(categories, values)
plt.show()
# Scatter plot
plt.scatter(x, y)
plt.show()
```
## Best Practices
1. **PEP 8 Style Guide**: Follow Python's style conventions
2. **Meaningful Names**: Use descriptive variable and function names
3. **Documentation**: Write docstrings for functions and classes
4. **Error Handling**: Use try-except blocks appropriately
5. **Virtual Environments**: Isolate project dependencies
6. **Type Hints**: Use type annotations for clarity
7. **List Comprehensions**: Use when appropriate for readability
8. **Context Managers**: Use 'with' statements for resource management
9. **DRY Principle**: Don't Repeat Yourself
10. **Testing**: Write unit tests for your code
## Conclusion
Python's simplicity, readability, and extensive ecosystem make it an ideal language for beginners and experts alike. Whether you're building web applications, analyzing data, training machine learning models, or automating tasks, Python provides the tools and libraries to get the job done efficiently.
Continuous learning and practice are key to mastering Python. Explore different libraries, contribute to open-source projects, and build your own applications to deepen your understanding and skills. |