Teach me Python from Scratch.
- User
Teach me Python from Scratch.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Teach me Python from Scratch.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below, I'll teach you Python from scratch in a structured, beginner-friendly way. We'll start with the absolute basics and build up step by step. Python is a versatile, easy-to-learn programming language used for web development, data analysis, automation, AI, and more. It's known for its simple syntax (it reads like English) and huge community support.
This guide is hands-on: I'll explain concepts, provide code examples, and suggest exercises. To follow along, you'll need to install Python and run code. If you're new, don't worry—I'll guide you.
Download and Install Python:
python --version. It should show something like "Python 3.12.0".Choose an Editor/IDE:
Run Your First Code:
python to enter the interactive shell (REPL).print("Hello, World!") and press Enter. It should output: Hello, World!.exit().Congrats! You've run your first Python program.
Python code is executed line by line. Comments start with # (ignored by the computer).
Variables store data. No need to declare types—Python figures it out.
Examples:
# Integer (whole number)
age = 25
print(age) # Output: 25
# Float (decimal number)
height = 5.9
print(height) # Output: 5.9
# String (text)
name = "Alice"
print("Hello, " + name) # Output: Hello, Alice
# Boolean (True/False)
is_student = True
print(is_student) # Output: True
Exercise: Create variables for your name, age, and a fun fact about yourself. Print them in a sentence.
Use these for math and comparisons.
Arithmetic: +, -, *, /, // (integer division), % (modulo), ** (exponent).
a = 10
b = 3
print(a + b) # 13
print(a / b) # 3.333...
print(a % b) # 1 (remainder)
print(a ** 2) # 100
Comparison: == (equal), != (not equal), >, <, >=, <=.
print(10 > 5) # True
Exercise: Calculate the area of a rectangle (length * width) and print if it's greater than 50.
Get data from the user:
name = input("Enter your name: ")
print("Hi, " + name)
For decisions:
age = int(input("Enter your age: ")) # Convert input to integer
if age >= 18:
print("You can vote!")
elif age >= 13:
print("You're a teen!")
else:
print("You're too young!")
Indentation (spaces or tabs) matters in Python—it's how blocks of code are grouped.
Exercise: Write a program that checks if a number is even or odd (use % 2 == 0).
For Loop (repeat a fixed number of times):
for i in range(5): # 0 to 4
print(i) # Outputs: 0 1 2 3 4
While Loop (repeat until a condition is false):
count = 0
while count < 5:
print(count)
count += 1 # Same as count = count + 1
Exercise: Use a loop to print numbers from 1 to 10, then modify it to print only even numbers.
Functions are reusable blocks of code. Define them with def.
def greet(name):
print("Hello, " + name + "!")
greet("Bob") # Output: Hello, Bob!
With return value:
def add(x, y):
return x + y
result = add(5, 3)
print(result) # 8
Exercise: Write a function that takes two numbers and returns their product. Call it with user input.
Like arrays:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # apple (index starts at 0)
fruits.append("date") # Add to end
print(fruits) # ['apple', 'banana', 'cherry', 'date']
# Loop through list
for fruit in fruits:
print(fruit)
Can't change after creation:
colors = ("red", "green", "blue")
print(colors[1]) # green
Like a real dictionary:
person = {"name": "Alice", "age": 25, "city": "New York"}
print(person["name"]) # Alice
person["age"] = 26 # Update
pop() or remove()), and print the list.Read/write files:
# Write to file
with open("example.txt", "w") as file:
file.write("Hello, file!")
# Read from file
with open("example.txt", "r") as file:
content = file.read()
print(content) # Hello, file!
Handle mistakes gracefully:
try:
number = int(input("Enter a number: "))
print(10 / number)
except ZeroDivisionError:
print("Can't divide by zero!")
except ValueError:
print("That's not a number!")
Python has built-in modules. Import them:
import math
print(math.sqrt(16)) # 4.0
import random
print(random.randint(1, 10)) # Random number between 1 and 10
Classes are blueprints for objects.
class Dog:
def __init__(self, name): # Constructor
self.name = name
def bark(self):
print(self.name + " says Woof!")
my_dog = Dog("Buddy")
my_dog.bark() # Buddy says Woof!
Car class with attributes like color and methods like drive().:) after if, for, etc.print() to see variable values. Google errors—they're common!You've covered the fundamentals! Practice daily for 15-30 minutes. If you get stuck or want to dive deeper into a topic (e.g., "Teach me more about lists" or "Help with a project"), reply and I'll help. What's your first question or what do you want to try next? Let's code! 🚀