Yo, what's good, bros? I'm stoked to kick off my 14-day Python revision challenge for machine learning, and today’s Day 1! I’m going to break down some basic Python concepts in a way that’s super chill and easy to get. We’re talking about basic output, variables, and strings today. Let’s keep it real simple, with clean bullet points and examples, so everyone can vibe with it. Ready? Let’s dive in!
Day 1: Python Basics for ML - Let’s Get It! 💪
I’m starting with the fundamentals. These are the building blocks you need before you start slaying those ML models. Here’s what I covered today:
1. Basic Output with print() 🗣️
print("hello world", "I am ridwan", sep=" & ", end="?")
- Output: hello world & I am ridwan?
- sep=" & " adds “ & ” between the words.
- end="?" slaps a question mark at the end instead of a new line.
2. Variables - Your Data’s Home 🏠
Variables are like your backpack—you store stuff in them to use later. Let’s break it down:
- What’s Inside a Variable?
var = "rdwan" print(id(var)) # Outputs a unique ID (memory address) for the # Memory address import sys print(sys.getrefcount(var)) # How many references point to this object print(sys.getsizeof(var)) # Size of the object in bytes
- id(var) gives you the memory address of var. Think of it as the variable’s home address.
- sys.getrefcount(var) tells you how many things are pointing to it (kinda nerdy, but cool).
- sys.getsizeof(var) shows how much space it takes in memory.
Fun Fact: Same Object, Same Address:
x = 100 y = 100 print(id(x)) # Same address print(id(y)) # Same address print(x == y) # True
- Python’s smart. If x and y are the same small number (like 100), they point to the same object in memory. Saves space
Invalid Variable Names:
- These won’t work: _name, 1name, name@, var-1
- Good ones: name1, my_var, coolVariable
Multiple Variables in One Line:
x, y, z = 10, 20, 30 print(x, y, z) # Output: 10 20 30
- Assign multiple variables at once. Clean and quick.
3. Strings - Playing with Words ✍️
Strings are just text, like your WhatsApp messages. Python’s got a ton of tricks to mess with ‘em.
Input and Type Shenanigans
x = input() # You type: 10 y = input() # You type: 20 print(type(x)) # Output: <class 'str'> print(x + y) # Output: 1020 (strings get concatenated) print(int(x) + int(y)) # Output: 30 (convert to numbers first)
- input() always gives you a string, even if you type numbers.
- Want to add numbers? Convert strings to integers with int().
String Methods - Your Text Toolkit
x = "hello world" print(x.upper()) # HELLO WORLD print(x.lower()) # hello world print(x.split()) # ['hello', 'world'] print(x.split("l")) # ['he', '', 'o wor', 'd'] print(x.__len__()) # 11 (length of string) print(x.count("l")) # 3 (how many 'l's) print(x.find("l")) # 2 (first 'l' at index 2) print(x.find("ll")) # 2 (first 'll' at index 2) print(x.find("l", 10)) # -1 (no 'l' after index 10) print(x.capitalize()) # Hello world print(x.title()) # Hello World print(x.swapcase()) # HELLO WORLD print(x.isdigit()) # False (not all digits) print(x.isalpha()) # False (not all letters) print(x.isalnum()) # False (has a space) print(x.center(20)) # ' hello world ' print(x.ljust(20)) # 'hello world ' print(x.rjust(20)) # ' hello world' print(x.replace("l", "L")) # heLLo worLd
- These methods are like filters for your text. Uppercase, lowercase, split it, count stuff, find stuff, align it—whatever you need, Python’s got you.
Day 1 Vibe Check ✅
Today was all about getting comfy with Python’s basics. I’m feeling pumped! Here’s the game plan:
- Output: Printing stuff with print() is your first step to flexing in Python.
- Variables: They’re your storage units. Know their IDs and sizes for extra nerd points.
- Strings: Text is powerful, and Python’s string methods let you slice and dice it like a pro.
Tomorrow, I’m diving deeper—maybe some lists, loops, or conditionals. Stay tuned, bros! If you’re following along, drop a comment on my blog. Let’s learn this ML stuff together! 😎
Pro Tip Want to try this yourself? Fire up a Python editor (like VS Code or Jupyter Notebook) and mess around with the code snippets above. It’s the best way to lock this stuff in.
Peace out, and catch ya on Day 2! ✌️
Top comments (0)