Python Basics – Learn Python Free 2026 | Poetic Bytes
Master Python fundamentals—variables, data types, conditionals, loops, functions, lists, and dictionaries—with 24 interactive lessons by @PoeticGlenn.
Python Basics
Build a Solid Foundation
Master the core building blocks of Python: variables, data types, conditionals, loops, functions, and data structures. 24 interactive lessons with runnable code — crafted by @PoeticGlenn.
What you will learn
Everything you need to start writing Python confidently — from understanding variables and data types to building functions and working with collections. Each concept is explained step-by-step with interactive code examples from @PoeticGlenn.
Why Python Basics Matter
Before you can build web apps, analyze data, or automate tasks, you need a rock-solid grasp of Python's fundamentals. This curriculum, designed by @PoeticGlenn, takes you from zero to confident in writing clean, efficient Python code. We'll cover variables, data types, control flow, functions, and essential data structures — everything you need to start building real projects.
Think Like a Programmer
Learn the core concepts that apply to any programming language, not just Python syntax.
Build Real Projects
From simple scripts to small applications, you'll have the skills to build what you imagine.
Lifelong Foundation
Mastering basics ensures you can learn advanced topics like OOP, data science, and web development.
"Everyone should learn to program a computer, because it teaches you how to think." — Steve Jobs 🐍
Every Python Developer Starts Here
Python's simplicity and readability make it the perfect first language, but its power makes it a career-long companion. This curriculum, curated by @PoeticGlenn, focuses on the 20% of Python that you'll use 80% of the time, giving you a strong foundation for any path you choose.
🧠 Universal Concepts
Variables, loops, and conditionals are the same in every language. Learn them once, use them everywhere.
📊 Data Handling
Python's lists, dictionaries, and strings are incredibly powerful. Master them to manipulate data effectively.
🔧 Build Tools
Learn to write reusable functions and modules so you can build and share your own toolkits.
🚀 Career Foundation
Whether you want to be a web developer, data scientist, or DevOps engineer, basics are your first step.
Why Python Dominates Modern Development
Python is the language of choice for web development, data science, AI, automation, and more. Understanding its fundamentals opens doors to countless opportunities. This curriculum, designed by @PoeticGlenn, reflects the core knowledge required to contribute to Python projects anywhere.
Web & APIs
Build backends with Django, Flask, or FastAPI. Python powers the web.
Data & AI
Pandas, NumPy, TensorFlow, and PyTorch — the AI revolution runs on Python.
Automation & DevOps
Script repetitive tasks, manage infrastructure, and build CI/CD pipelines.
The fundamentals you're about to learn are the same building blocks used by Google, NASA, and Spotify.
📑 Table of Contents
Jump to any section — all entries are equal size
🏷️ Variables & Assignment — Storing Data
Variables are names that point to values. They are the building blocks of any program.
📝 1.1 Basic Variable Assignment
Use = to assign a value to a variable. No declaration needed — Python infers the type.
Step-by-step explanation:
- Step 1: Assign the integer
25to the variableage. - Step 2: Assign the string
"Alice"to the variablename. - Step 3: Assign the float
3.14to the variablepi. - Step 4: Assign the boolean
Trueto the variableis_student. - Step 5: Use the
type()function to show the data type of each variable. - Step 6: Print the values using f-strings.
age = 25 name = "Alice" pi = 3.14 is_student = True print(f"Name: {name}, Age: {age}") print(f"pi = {pi}, Type of pi: {type(pi)}") print(f"Student: {is_student}")
📝 1.2 Reassigning Variables
Variables can be reassigned to new values at any time.
Step-by-step explanation:
- Step 1: Assign
score = 10and print it. - Step 2: Reassign
score = 20and print it. - Step 3: Reassign
score = "high"(changing type) and print it. - Step 4: Use
type()to show that the type changed from int to str.
score = 10 print(f"Score: {score}, Type: {type(score)}") score = 20 print(f"Score: {score}, Type: {type(score)}") score = "high" print(f"Score: {score}, Type: {type(score)}")
📝 1.3 Multiple Assignment
Assign multiple variables in one line — useful for swapping values.
Step-by-step explanation:
- Step 1: Assign
a, b, c = 1, 2, 3and print them. - Step 2: Swap
aandbusinga, b = b, a. - Step 3: Print the new values to verify the swap.
a, b, c = 1, 2, 3 print(f"a={a}, b={b}, c={c}") a, b = b, a print(f"After swap: a={a}, b={b}")
🗂️ Data Types — Integers, Floats, Strings, and Booleans
Understanding data types is crucial for writing correct programs.
🔢 2.1 Numeric Types: int & float
Integers are whole numbers. Floats are numbers with decimals.
Step-by-step explanation:
- Step 1: Create an integer
x = 5and a floaty = 3.14. - Step 2: Perform basic arithmetic: addition, subtraction, multiplication, division.
- Step 3: Use
type()to show the type of each result. - Step 4: Show integer division
//and exponentiation**.
x = 5 y = 3.14 print(f"x = {x} (type: {type(x)})") print(f"y = {y} (type: {type(y)})") print(f"x + y = {x + y}") print(f"x * y = {x * y}") print(f"x / y = {x / y}") print(f"x // 2 = {x // 2}")
📝 2.2 Strings — str
Strings are sequences of characters. Use single or double quotes.
Step-by-step explanation:
- Step 1: Create a string
name = "Alice". - Step 2: Use f-strings for interpolation:
f"Hello, {name}". - Step 3: Concatenate strings with
+:"Hello, " + name. - Step 4: Show string multiplication:
"ha" * 3. - Step 5: Use
len()to get the string length.
name = "Alice" greeting = f"Hello, {name}" print(greeting) print("Concatenation: " + name) print("ha" * 3) print(f"Length of name: {len(name)}")
🔘 2.3 Booleans — bool
Booleans represent True or False. They are the result of comparisons.
Step-by-step explanation:
- Step 1: Create booleans
is_raining = Falseandhas_umbrella = True. - Step 2: Use logical operators
and,or,not. - Step 3: Show comparisons:
5 > 3,5 == 5,5 != 3.
is_raining = False has_umbrella = True print(f"Is raining: {is_raining}") print(f"Has umbrella: {has_umbrella}") print(f"Raining AND has umbrella: {is_raining and has_umbrella}") print(f"Raining OR has umbrella: {is_raining or has_umbrella}") print(f"NOT raining: {not is_raining}") print(f"5 > 3: {5 > 3}") print(f"5 == 5: {5 == 5}")
🔀 Conditionals — if, elif, else
Make decisions in your code based on conditions.
📝 3.1 if Statement
The simplest form of conditional.
Step-by-step explanation:
- Step 1: Assign
age = 18. - Step 2: Write an
ifstatement:if age >= 18:. - Step 3: Indent the block to execute when the condition is True.
- Step 4: The block runs if the condition is True.
age = 18 if age >= 18: print("You are an adult.") print("You can vote.")
📝 3.2 if/else — Two Paths
Handle both True and False cases.
Step-by-step explanation:
- Step 1: Assign
temperature = 25. - Step 2: Check
if temperature > 30:. - Step 3: If True, print "It's hot outside.".
- Step 4: In the
elseblock, print "It's not too hot.".
temperature = 25 if temperature > 30: print("It's hot outside.") else: print("It's not too hot.")
📝 3.3 if/elif/else — Multiple Conditions
Chain multiple conditions with elif.
Step-by-step explanation:
- Step 1: Assign
score = 85. - Step 2: Check multiple ranges using
elif. - Step 3: Print the corresponding grade.
score = 85 if score >= 90: print("Grade: A") elif score >= 80: print("Grade: B") elif score >= 70: print("Grade: C") elif score >= 60: print("Grade: D") else: print("Grade: F")
🔄 Loops — for and while
Repeat code blocks efficiently.
🔄 4.1 for Loop — Iterating Over Sequences
Use for to iterate over lists, strings, ranges, etc.
Step-by-step explanation:
- Step 1: Create a list of colors:
colors = ["red", "green", "blue"]. - Step 2: Write a
forloop:for color in colors:. - Step 3: Print each color.
- Step 4: Use
range()to iterate over numbers.
colors = ["red", "green", "blue"] for color in colors: print(f"Color: {color}") print("\nNumbers 1 to 3:") for i in range(1, 4): print(f"Number: {i}")
🔄 4.2 while Loop — Repeat Until Condition
Use while when you don't know how many times to iterate.
Step-by-step explanation:
- Step 1: Initialize
count = 0. - Step 2: Write
while count < 5:. - Step 3: Print the count and increment it.
- Step 4: The loop stops when
count >= 5.
count = 0 while count < 5: print(f"Count: {count}") count += 1 print("Loop finished!")
while loops — if the condition never becomes False, you create an infinite loop.📦 Functions — Reusable Code Blocks
Functions let you organize, reuse, and structure your code.
📝 5.1 Defining and Calling Functions
Use def to define a function.
Step-by-step explanation:
- Step 1: Define
greet()usingdef. - Step 2: The function body is indented.
- Step 3: Call the function using
greet(). - Step 4: Define a function with a parameter.
def greet(): print("Hello, world!") greet() def greet_person(name): print(f"Hello, {name}!") greet_person("Alice")
📝 5.2 Return Values
Use return to send a value back to the caller.
Step-by-step explanation:
- Step 1: Define
add(a, b)that returnsa + b. - Step 2: Call the function and store the result in
result. - Step 3: Print the result.
def add(a, b): return a + b result = add(5, 3) print(f"Result: {result}")
📝 5.3 Default Parameters
Provide default values for parameters.
Step-by-step explanation:
- Step 1: Define
greet(name, greeting="Hello"). - Step 2: Call with just the name:
greet("Alice"). - Step 3: Call with a custom greeting:
greet("Bob", "Hi").
def greet(name, greeting="Hello"): print(f"{greeting}, {name}!") greet("Alice") greet("Bob", "Hi")
📋 Lists — Ordered, Mutable Collections
Lists are one of the most versatile data structures in Python.
📝 6.1 Creating and Indexing Lists
Access elements by index (starting from 0).
Step-by-step explanation:
- Step 1: Create a list:
fruits = ["apple", "banana", "cherry"]. - Step 2: Access the first element:
fruits[0]. - Step 3: Access the last element with negative index:
fruits[-1]. - Step 4: Use slicing:
fruits[1:3].
fruits = ["apple", "banana", "cherry"] print(f"First fruit: {fruits[0]}") print(f"Last fruit: {fruits[-1]}") print(f"Slicing 1 to 3: {fruits[1:3]}")
📝 6.2 List Methods — append, insert, remove
Lists are mutable — you can change them in place.
Step-by-step explanation:
- Step 1: Create an empty list:
items = []. - Step 2: Add an item using
append(). - Step 3: Insert an item at a specific position using
insert(). - Step 4: Remove an item using
remove().
items = [] items.append("first") items.append("second") print(f"After append: {items}") items.insert(0, "zero") print(f"After insert: {items}") items.remove("second") print(f"After remove: {items}")
📝 6.3 Looping Over Lists
Combine lists and loops for powerful data processing.
Step-by-step explanation:
- Step 1: Create a list of numbers:
numbers = [1, 2, 3, 4, 5]. - Step 2: Use a
forloop to calculate the sum. - Step 3: Use
enumerate()to get both index and value.
numbers = [1, 2, 3, 4, 5] total = 0 for num in numbers: total += num print(f"Sum: {total}") for i, num in enumerate(numbers): print(f"Index {i}: {num}")
📖 Dictionaries — Key-Value Pairs
Dictionaries store data in key-value pairs, making data retrieval fast and intuitive.
📝 7.1 Creating and Accessing Dictionaries
Use curly braces {} and colons : to separate keys and values.
Step-by-step explanation:
- Step 1: Create a dictionary:
person = {"name": "Alice", "age": 30}. - Step 2: Access a value using square brackets:
person["name"]. - Step 3: Use
get()to avoid KeyError:person.get("city"). - Step 4: Add a new key-value pair:
person["city"] = "New York".
person = {"name": "Alice", "age": 30} print(f"Name: {person['name']}") print(f"Age: {person['age']}") print(f"City (using get): {person.get('city', 'unknown')}") person["city"] = "New York" print(f"City after adding: {person['city']}")
📝 7.2 Looping Over Dictionaries
Iterate over keys, values, or both.
Step-by-step explanation:
- Step 1: Create a dictionary:
grades = {"Alice": 90, "Bob": 85}. - Step 2: Loop over keys using
for key in grades:. - Step 3: Loop over values using
for value in grades.values():. - Step 4: Loop over items using
for key, value in grades.items():.
grades = {"Alice": 90, "Bob": 85, "Charlie": 92} print("Keys:") for key in grades: print(f" {key}") print("Values:") for value in grades.values(): print(f" {value}") print("Items:") for name, score in grades.items(): print(f" {name}: {score}")
📝 Strings — Text Manipulation
Python strings come with a rich set of methods for text processing.
📝 8.1 Common String Methods
Use methods like .upper(), .lower(), .strip(), and .replace().
Step-by-step explanation:
- Step 1: Create a string:
text = " Hello, Python! ". - Step 2: Remove whitespace with
.strip(). - Step 3: Convert to uppercase and lowercase.
- Step 4: Replace substrings with
.replace(). - Step 5: Split a string into a list with
.split().
text = " Hello, Python! " print(f"Original: '{text}'") stripped = text.strip() print(f"Stripped: '{stripped}'") print(f"Uppercase: {stripped.upper()}") print(f"Lowercase: {stripped.lower()}") replaced = stripped.replace("Python", "World") print(f"Replaced: {replaced}") words = stripped.split(", ") print(f"Split: {words}")
📝 8.2 String Formatting
Use f-strings or .format() to embed variables in strings.
Step-by-step explanation:
- Step 1: Define variables:
name = "Alice",age = 30. - Step 2: Use an f-string to embed variables:
f"My name is {name}.". - Step 3: Use
.format()for older codebases.
name = "Alice" age = 30 print(f"My name is {name} and I am {age} years old.") print("My name is {} and I am {} years old.".format(name, age))
❓ Frequently Asked Questions — Python Basics
Common questions about Python fundamentals, answered.
Is Python dynamically typed or statically typed?
+Python is dynamically typed. You don't need to declare the type of a variable; Python infers it at runtime. You can reassign a variable to a value of a different type.
What is the difference between a list and a tuple?
+Lists are mutable (you can change them), while tuples are immutable (cannot be changed after creation). Tuples are faster and use less memory, and they can be used as dictionary keys.
Why are indentation rules so strict in Python?
+Indentation is Python's way of defining blocks of code. Unlike languages that use braces, Python uses indentation to group statements. This enforces readability and reduces syntax clutter.
What does if __name__ == "__main__": do?
+It ensures that a block of code runs only if the script is executed directly, not when imported as a module. It's a standard pattern for making Python files both reusable modules and executable scripts.
How do I convert between data types?
+Use built-in functions: int(), float(), str(), list(), tuple(), dict(). For example, int("10") gives the integer 10, and str(10) gives the string "10".
What is the difference between == and is?
+== checks if the values are equal, while is checks if they refer to the same object in memory. Use == for value comparison, and is for identity comparison (e.g., None).
How do I debug my Python code?
+Use print() statements to inspect variables. For more advanced debugging, use the built-in pdb module (import pdb; pdb.set_trace()). Many IDEs also offer graphical debugging tools.
💛 You've Built a Strong Python Foundation
Congratulations! You've covered the core building blocks of Python — variables, data types, conditionals, loops, functions, lists, dictionaries, and strings. You now have the essential toolkit to start building scripts, solving problems, and exploring advanced topics like object-oriented programming, web development, and data science.
This curriculum, crafted by @PoeticGlenn, is your launchpad into the world of Python. Keep practicing, keep building, and never stop learning. The journey has just begun!
"Simple can be harder than complex. But it's worth it." — Steve Jobs — Glenn Junsay Pansensoy (@PoeticGlenn)
Comments
Post a Comment