Skip to main content

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!