Python Introduction
What is Python?β
Python is a high-level programming language created by Guido van Rossum in 1991. With its easy-to-read syntax and powerful features, it's loved by both beginners and experts.
Key Featuresβ
β
Concise and readable syntax
β
Extensive standard library
β
Versatile applications (Web, Data, AI, Automation)
β
Active community
β
Cross-platform support
Why Python?β
1. Easy to Learnβ
# Python - Intuitive!
print("Hello, World!")
numbers = [1, 2, 3, 4, 5]
squared = [n ** 2 for n in numbers]
print(squared) # [1, 4, 9, 16, 25]
2. Versatileβ
# Web development
from fastapi import FastAPI
app = FastAPI()
# Data analysis
import pandas as pd
df = pd.read_csv('data.csv')
# Machine learning
from sklearn import tree
model = tree.DecisionTreeClassifier()
# Automation
import os
for file in os.listdir('.'):
print(file)
3. Rich Ecosystemβ
Web: Django, Flask, FastAPI
Data: NumPy, Pandas, Matplotlib
AI/ML: TensorFlow, PyTorch, scikit-learn
Automation: Selenium, BeautifulSoup, Requests
Installationβ
Windowsβ
1. Download from Python official website
https://www.python.org/downloads/
2. Check during installation
β Add Python to PATH (important!)
β Install pip
3. Verify installation
python --version
# Python 3.11.x
pip --version
# pip 23.x.x
macOSβ
Method 1: Homebrew (recommended)
# Install Homebrew (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Python
brew install python
# Verify
python3 --version
Method 2: Official download
https://www.python.org/downloads/
Linux (Ubuntu/Debian)β
# Update
sudo apt update
# Install Python
sudo apt install python3 python3-pip
# Verify
python3 --version
pip3 --version
Your First Python Programβ
Starting with REPLβ
# Launch Python interactive mode
python
# or
python3
>>> print("Hello, Python!")
Hello, Python!
>>> 2 + 3
5
>>> name = "νκΈΈλ"
>>> f"μλ
νμΈμ, {name}λ!"
'μλ
νμΈμ, νκΈΈλλ!'
>>> exit() # Exit
Running from Fileβ
Create hello.py
# hello.py
print("Hello, Python!")
name = input("μ΄λ¦μ μ
λ ₯νμΈμ: ")
print(f"μλ
νμΈμ, {name}λ!")
Execute
python hello.py
# or
python3 hello.py
Setting Up Development Environmentβ
1. Text Editor/IDEβ
VS Code (recommended)
1. Install VS Code
2. Install Python extension
3. Create file (.py)
4. Run with F5
PyCharm
- Free: Community Edition
- Paid: Professional Edition
- Powerful features, easy for beginners
Jupyter Notebook
# Install
pip install jupyter
# Run
jupyter notebook
# Code interactively in browser
2. Virtual Environment (important!)β
Why is it needed?
Use different package versions for each project
Separate from system Python
Create and activate
# Create virtual environment
python -m venv myenv
# Activate
# Windows
myenv\Scripts\activate
# macOS/Linux
source myenv/bin/activate
# Deactivate
deactivate
3. Package Management (pip)β
# Install package
pip install requests
# Install multiple
pip install numpy pandas matplotlib
# Install specific version
pip install django==4.2.0
# List installed packages
pip list
# Uninstall package
pip uninstall requests
# Create requirements.txt
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt
Practical Examplesβ
Calculatorβ
def calculator():
print("=== κ°λ¨ν κ³μ°κΈ° ===")
num1 = float(input("첫 λ²μ§Έ μ«μ: "))
operator = input("μ°μ°μ (+, -, *, /): ")
num2 = float(input("λ λ²μ§Έ μ«μ: "))
if operator == '+':
result = num1 + num2
elif operator == '-':
result = num1 - num2
elif operator == '*':
result = num1 * num2
elif operator == '/':
result = num1 / num2
else:
print("μλͺ»λ μ°μ°μμ
λλ€.")
return
print(f"κ²°κ³Ό: {result}")
calculator()
Number Guessing Gameβ
import random
def guessing_game():
answer = random.randint(1, 100)
attempts = 0
print("1λΆν° 100 μ¬μ΄μ μ«μλ₯Ό λ§μΆ°λ³΄μΈμ!")
while True:
guess = int(input("μ«μ μ
λ ₯: "))
attempts += 1
if guess < answer:
print("λ ν° μ«μμ
λλ€!")
elif guess > answer:
print("λ μμ μ«μμ
λλ€!")
else:
print(f"μ λ΅! {attempts}λ² λ§μ λ§μ·μ΅λλ€!")
break
guessing_game()
Python Style Guideβ
PEP 8 (Python Coding Rules)β
# β
Good example
def calculate_total(price, quantity):
total = price * quantity
return total
user_name = "νκΈΈλ"
MAX_SIZE = 100
# β Bad example
def CalculateTotal(Price,Quantity):
Total=Price*Quantity
return Total
UserName="νκΈΈλ"
maxsize=100
Key Rules
- Indentation: 4 spaces
- Variable names: snake_case
- Class names: PascalCase
- Constants: UPPER_CASE
- Line length: maximum 79 characters
Useful Tipsβ
1. Getting Helpβ
# Function help
help(print)
# Module help
import math
help(math)
# Check object attributes
dir("hello")
2. Interactive Debuggingβ
# Pause code execution
import pdb
pdb.set_trace()
# Or Python 3.7+
breakpoint()
3. Timing Measurementβ
import time
start = time.time()
# Code to measure
sum([i for i in range(1000000)])
end = time.time()
print(f"μ€ν μκ°: {end - start}μ΄")
Frequently Asked Questionsβ
Q1. Python 2 vs Python 3?β
A: Use Python 3!
# Python 2 support ended in 2020
# Python 3 is the standard
Q2. python vs python3 command?β
A: It depends on the system
# Windows: usually python
python --version
# macOS/Linux: usually python3
python3 --version
# Check and use the right one!
Q3. Which IDE is good?β
A: Choose based on preference and purpose
Beginners: VS Code (lightweight and free)
Professional development: PyCharm (powerful features)
Data analysis: Jupyter Notebook (interactive)
Simple tasks: Text editor + Terminal
Q4. Package installation doesn't work!β
A: It might be a permission issue
# Add --user option
pip install --user ν¨ν€μ§λͺ
# Or use virtual environment (recommended)
python -m venv myenv
source myenv/bin/activate # Windows: myenv\Scripts\activate
pip install ν¨ν€μ§λͺ
Next Stepsβ
You're now ready to learn Python basics!
Key Summary:
β
Python installation and environment setup
β
REPL and file execution
β
IDE and virtual environment
β
Package management with pip
β
Writing your first program
Next Step: Learn the basics of Python programming in Variables and Types!