Introduction to Python
Introduction to Python
🎯 By the end of this lesson
After reading this lesson, you will be able to confidently do the following three things.
- ▸✅ Explain why Python became the standard language for AI and data
- ▸✅ Set up a venv + requirements.txt environment for Python 3.x
- ▸✅ Use the four built-in functions: print / input / type / dir
Keep the learning goals as a checklist and close the lesson once you can answer all of them.
Python Syntax — A 5-Minute Summary
Python is a highly readable programming language. The key idea is that it is 'almost as concise as natural language.'
For each example, follow this order: code → output → explanation.
0. Python Basic Data Types — One Table
Every Python program is built from 'values'. Memorize these six most common types first.
Bottom line: At first, you only need to know these four: int, float, str, and bool. list and dict are covered in the data structures section.
The key difference from Java — Python has no type declarations:
This is secret #1 behind 'Python being concise.'
1. Variables — Storing a Value Under a Name
= means 'assign the right-hand value to the left-hand variable.' It is not a mathematical equals sign.
Difference from Java: No type declaration like String name — Python figures it out automatically.
2. Output print() — Displaying to the Screen
Output:
f"..." is an f-string — a modern way to embed variables inside {} (Python 3.6+).
3. Input input() — Receiving Input from the User
Terminal interaction:
⚠️ input() always returns a string. If you need a number, convert it: int(input(...)).
4. Colon : + Indentation — Python's 'Block' Syntax
Before diving into conditionals, loops, and functions, you need to understand Python's unique block syntax.
Java uses curly braces { }, Python uses a colon : + 4 spaces of indentation:
A colon is required after these keywords:
Forgetting the colon causes a SyntaxError:
The colon is also used to denote 'left:right pairs':
→ A colon signals either 'block starts on the next line' or 'a left:right pair.'
5. Conditionals if·elif·else — Branching
Output: B (85 is ≥ 80, so the second branch is taken)
Key rules:
- ▸No curly braces
{ }— blocks are defined by 4-space indentation - ▸A
:colon is required at the end of theifline - ▸
==(equals) /!=(not equals) />=(greater than or equal) /<=(less than or equal)
6. Loops for·while
Output: 1
2
3
4
5 (one per line)
Output:
⚠️ range(1, 6) goes from 1 up to but not including 6 (i.e., up to 5). This is a common Python gotcha.
7. Functions def — Reusing Code
Output: Hello Hong Gil-dong
Syntax breakdown:
- ▸
def= short for 'define' - ▸
(name)= the value to receive (parameter). Multiple:(name, age) - ▸
return= sends the result back to the caller. If omitted, returnsNone - ▸Call a function with
function_name(value)— forgetting()refers to the function itself
8. Four Data Structures
Output: [1, 2, 3, 4]
Output: 37.5
Output:
Output: {'python', 'ai'} (duplicate 'python' removed)
When to use what:
- ▸Order matters + mutable → list
- ▸Order matters + immutable → tuple
- ▸Look up by name → dict
- ▸Remove duplicates → set
9. Classes class — Creating Objects
Output: Hi, I'm Hong Gil-dong (28 years old)
Key concepts:
- ▸
__init__= called automatically when the object is first created (like a constructor in Java) - ▸
self= the object itself (likethisin Java). Required as the first parameter of every method - ▸Calling
Person("Hong Gil-dong", 28)automatically runs__init__
One-Line Summary
These 8 patterns cover 90% of Python.
🐍 Try It Out — Python Introduction
🤖 Try Prompting Your AI Like This
Once you understand the concepts in this lesson, you can give your AI specific instructions. Instead of a vague 'fix this,' use vocabulary-driven requests — that is the starting point for saving tokens.
- ▸'Apply the Python introduction concepts to this Python code'
- ▸'Add type hints and pytest unit tests to this code'
- ▸'Check this code for PEP 8 violations related to Python introduction concepts'
Why This Reduces Tokens
Without knowing the concepts, you have to ask 'what does that mean?' after each AI response. Those follow-up questions consume tokens. Master the concepts once and the conversation ends in one round.