Ever feel like your code is "doing the work" but then immediately forgetting everything it just did? 🧠 If you’re a beginner learning Python, the difference between print and return is one of the biggest "Aha!" moments you'll have. Here is the breakdown using your example. 1. The "Show-and-Tell" (print) When you use print, the function calculates the answer and shouts it out to the console so you can see it. But that's it. It doesn't "save" the answer anywhere. Python def adding(a, b): print(a + b) adding(20, 12) # Output: 32 Think of it like: A chef cooking a meal, showing it to you, and then immediately throwing it in the trash. You saw it, but you can't eat it (or use it) later! 2. The "Hand-off" (return) When you use return, the function calculates the answer and hands it back to you. Now, you can store that answer in a variable and keep using it for other things. Python def adding_return(a, b): return a + b # We catch the value in a variable called 'result' result = adding_return(20, 12) # Now we can actually use it! print(result - 10) # Output: 22 Think of it like: A chef cooking a meal and putting it in a takeout box for you. You can take it home, add extra salt, or save it for tomorrow. 🔑 The Key Difference print is for Humans. It helps us see what’s happening during debugging. return is for Code. It allows different parts of your program to talk to each other and pass data around. Why this matters: If you tried to do adding(20, 12) - 10 with the first function, Python would give you an error. Why? Because the function didn't "give" you anything back to subtract from! #Python #CodingTips #LearnToCode #ProgrammingBeginner #SoftwareDevelopment #PythonBasics #TechEducation
Python Print vs Return: Understanding the Difference
More Relevant Posts
-
Day 17 – Strings, Lists & Practical Logic Building in Python Today’s session was a mix of real-world logic building and deeper practice with strings and lists in Python. I started with a simple electricity bill calculation program using conditional statements. It helped me understand how tier-based logic works in real-life scenarios and how to structure conditions properly using if-elif-else. Strings – More Practice Revised and practiced important string methods: upper() and lower() for case conversion isupper() and islower() for validation capitalize() vs title() replace() with control over number of replacements This reinforced how strings are immutable and how every operation returns a new modified string. Lists – Slicing & Methods in Depth Worked extensively on list operations and understood the difference between modifying and non-modifying methods. Slicing Practice: Normal slicing Step slicing Reverse slicing Skipping elements using step values List Methods Explored: append() → add single element extend() → add multiple elements from iterable insert() → add element at specific index remove() → remove first occurrence pop() → remove by index (returns removed value) clear() → empty the list index() → find position of element sort() → modifies original list sorted() → returns new sorted list reverse() → reverse list order join() → join list elements into a string I also observed: How insert() behaves with negative and out-of-range indexes Difference between sort() and sorted() How extend() works differently with list, tuple, and set Key Takeaways Understanding method behavior is more important than just memorizing syntax Some methods modify the original list, others return new values Real learning happens when testing edge cases Logic building is improving step by step Day 17 was more about strengthening fundamentals and building clarity in how Python handles data structures. #Python #PythonLists #PythonStrings #ProgrammingBasics #DataStructures #CodingPractice #DailyLearning #ProblemSolving #LearnToCode #SoftwareDevelopment
To view or add a comment, sign in
-
📘 Day 3 — Lists, If Conditions, For Loops… and Why Indentation Matters! Today’s learning felt like the moment Python started behaving like a real analysis tool. I covered: 🔹 Lists → storing multiple values 🔹 If Conditions → applying logic 🔹 For Loops → automating repetition 🔹 Indentation → the rule that makes everything work First — Lists Just like a column in Excel, Python lets us store multiple values together: marks = [65, 72, 58, 90, 48] Second — If Condition This allows Python to make decisions based on rules: if mark > 60: print("Pass") else: print("Fail") Third — For Loop Instead of checking each value manually, Python can go through the whole list: for mark in marks: if mark > 60: print("Pass") else: print("Fail") Now the Most Important Part — Indentation In many tools, spacing is just for readability. But in Python, indentation defines the structure of the code. Python uses indentation to understand: ✔ What belongs inside the loop ✔ What belongs inside the condition ✔ Where logic starts and ends Notice how everything inside the loop is indented: for mark in marks: if mark > 60: print("Pass") If indentation is wrong, Python throws an error — even if the logic is correct. So the key rule I learned today: 👉 Same logic block = Same indentation (usually 4 spaces) Today felt like moving from “writing code” to “teaching Python how to think through data.” #PythonLearning #DataAnalyticsJourney #codebasics #OnlineCredibility
To view or add a comment, sign in
-
-
🚀 Day 6/60 – Loops in Python (Automate Repetition Like a Pro) Writing the same code again and again? ❌ Let Python do it for you ✅ That’s what loops are for 👇 🔁 What is a Loop? A loop lets you repeat a block of code multiple times. 🔹 1️⃣ For Loop (Most Used) for i in range(5): print(i) 👉 Output: 0 1 2 3 4 🔹 2️⃣ Loop Through List fruits = ["apple", "banana", "mango"] for fruit in fruits: print(fruit) 🔹 3️⃣ While Loop count = 0 while count < 5: print(count) count += 1 ⚡ Real Example for i in range(1, 6): print("Hello", i) 👉 Prints "Hello" 5 times ❌ Common Mistake (Infinite Loop) count = 0 while count < 5: print(count) ❌ This never stops! Correct: count += 1 🔥 Pro Tip Use range(start, stop, step) 👇 for i in range(1, 10, 2): print(i) 👉 1, 3, 5, 7, 9 🔥 Challenge for today Write a program: 👉 Print numbers from 1 to 10 👉 Print only even numbers Comment “DONE” when finished ✅ Follow Adeel Sajjad to stay consistent for 60 days 🚀 #Python #LearnPython #PythonProgramming #Coding #Programming
To view or add a comment, sign in
-
-
Day 18: Scope and Precision — The Limits of Logic 🌐 As your programs grow, you'll start having variables with the same names in different places. How does Python know which one to use? And when doing math, how many decimals can Python actually "remember"? 1. Local vs. Global Scope Think of Scope as the "area of visibility" for a variable. Global Scope: Variables defined at the top level (outside any function). They can be read from anywhere in your script. Local Scope: Variables defined inside a function. They only exist while that function is running. Once the function ends, the variable is deleted. 💡 The Engineering Lens: Avoid using too many Global variables. If every function can change a variable, it becomes a nightmare to track down bugs. Keep data "Local" whenever possible! 2. The LEGB Rule: Python’s Search Engine When you call a variable name, Python searches in a very specific order to find it. This is the LEGB rule: Local: Inside the current function. Enclosing: Inside any nested "parent" functions. Global: At the top level of the file. Built-in: Python’s pre-installed names (like len or print). 3. Precision: The Decimal Limit When you use a Float (a decimal number), Python has to fit that number into a fixed amount of memory. Maximum Precision: Python floats are typically "double-precision" (64-bit). This means they can hold about 15 to 17 significant decimal digits. The Default: When you perform a calculation, Python will show as many decimals as are relevant, but it stops being accurate after that 15–17 digit mark. 💡 The Engineering Lens: Because of this limit, 0.1 + 0.2 often equals 0.30000000000000004. If you are building a banking app or a scientific tool where you need infinite precision, don't use floats! Use Python’s decimal module instead. #Python #SoftwareEngineering #CleanCode #ProgrammingTips #DataPrecision #LearnToCode #TechCommunity #PythonDev
To view or add a comment, sign in
-
🚀 Day 13/100: Mastering Python String Methods! Today marks Day 13 of my #100DaysOfCode journey, and I’m diving deep into the world of Python String Methods. 🐍 Strings are everywhere in programming—from data cleaning to building web apps. Understanding these built-in methods is a total game-changer for writing clean, efficient code. 🛠️ Key Takeaways from Today: Case Transformations: Using .upper(), .lower(), and .capitalize() to standardize text data. Searching & Counting: Using .count() to find frequencies and .find() to locate substrings. Data Cleaning: The power of .replace() for formatting (like changing dates from / to -) and .split() for turning strings into lists. Validation: Checking inputs with .isalnum() and .isnumeric(). 💡 Quick Correction & Tip: While learning from cheatsheets, I noticed a tiny detail! In Python, the method is actually .isnumeric() (not just .numeric()). Always double-check your documentation while coding! 💻 I'm feeling more confident with Python every day. Looking forward to what Day 14 brings! #Python #CodingChallenge #100DaysOfCode #SoftwareDevelopment #ProgrammingTips #DataScience #WebDev #LearnToCode #PythonStrings
To view or add a comment, sign in
-
-
𝐍𝐮𝐦𝐏𝐲 𝐁𝐨𝐨𝐥𝐞𝐚𝐧 𝐓𝐫𝐚𝐩 I’m still at the very beginning of my Python journey. But even with my tiny amount of experience, I already hit a subtle NumPy trap that can easily sneak into real code. Python is full of surprises — even at the very beginning It happens when you create an untyped NumPy array and fill it with a function that should return booleans… …but sometimes returns 𝙉𝙤𝙣𝙚 when processing fails. At first, you expect a clean boolean array — because the function normally returns 𝙏𝙧𝙪𝙚 or 𝙁𝙖𝙡𝙨𝙚. But NumPy has other plans. Here’s the trap 👇 🟥 𝟏) 𝐔𝐧𝐭𝐲𝐩𝐞𝐝 𝐚𝐫𝐫𝐚𝐲 + 𝐚 𝐟𝐮𝐧𝐜𝐭𝐢𝐨𝐧 𝐭𝐡𝐚𝐭 “𝐬𝐡𝐨𝐮𝐥𝐝” 𝐫𝐞𝐭𝐮𝐫𝐧 𝐛𝐨𝐨𝐥𝐞𝐚𝐧𝐬 𝒂𝒓𝒓 = 𝒏𝒑.𝒆𝒎𝒑𝒕𝒚(10) # 𝒏𝒐 𝒅𝒕𝒚𝒑𝒆 𝒂𝒓𝒓[𝒊] = 𝒎𝒚_𝒇𝒖𝒏𝒄() # 𝑻𝒓𝒖𝒆 / 𝑭𝒂𝒍𝒔𝒆 ... 𝒐𝒓 𝑵𝒐𝒏𝒆 You expect a clean boolean array because the function usually returns 𝙏𝙧𝙪𝙚/𝙁𝙖𝙡𝙨𝙚. But if even one value is 𝙉𝙤𝙣𝙚, NumPy must pick a type that can hold all values. 🟦 𝟐) 𝐍𝐮𝐦𝐏𝐲 𝐬𝐢𝐥𝐞𝐧𝐭𝐥𝐲 𝐬𝐰𝐢𝐭𝐜𝐡𝐞𝐬 𝐭𝐨 𝐝𝐭𝐲𝐩𝐞=𝐨𝐛𝐣𝐞𝐜𝐭 𝒂𝒓𝒓𝒂𝒚([𝑻𝒓𝒖𝒆, 𝑭𝒂𝒍𝒔𝒆, 𝑵𝒐𝒏𝒆, ...], 𝒅𝒕𝒚𝒑𝒆=𝒐𝒃𝒋𝒆𝒄𝒕) Impact: no vectorization logical operations break masks behave unpredictably performance collapses You think you have a NumPy boolean array. You actually have a Python object array. 🟩 𝟑) 𝐓𝐡𝐞 𝐬𝐢𝐥𝐞𝐧𝐭 𝐜𝐨𝐧𝐯𝐞𝐫𝐬𝐢𝐨𝐧 𝐭𝐫𝐚𝐩 Trying to fix it: 𝒂𝒓𝒓 = 𝒏𝒑.𝒆𝒎𝒑𝒕𝒚(10, 𝒅𝒕𝒚𝒑𝒆=𝒃𝒐𝒐𝒍) 𝒂𝒓𝒓[𝒊] = 𝒎𝒚_𝒇𝒖𝒏𝒄() NumPy converts: 𝑵𝒐𝒏𝒆 → 𝑭𝒂𝒍𝒔𝒆 (silently) Impact: 👉you lose the meaning of “no result” 👉your data becomes wrong 👉the bug becomes invisible ⭐ 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Same code. Same function. Two completely different arrays. NumPy’s dtype inference can hide subtle bugs — and I found this one with almost no Python experience. 𝐂𝐮𝐫𝐢𝐨𝐮𝐬 𝐭𝐨 𝐤𝐧𝐨𝐰: 👉 Have you ever run into this behavior? 👉 Or another NumPy dtype surprise? #python #numpy #datascience #cleanCode #devTips #programming
To view or add a comment, sign in
-
-
Day 4 of Python was a masterclass in decision-making. I stopped writing "scripts" and started writing "logic flows." Here’s what I learned about building a resilient program: 🔹 The Power of the 'If': From simple one-way decisions to complex Nested and Multi-way (elif) structures. It’s not just about "Yes or No"; it’s about mapping out every possible fog in the road. 🔹 Indentation is Architecture: In Python, a few spaces aren't just for clean looks—they are the law. Indentation tells the computer exactly which code belongs to which decision. If your alignment is off, your logic is off. 🔹 The (Try/Except): I learned how to anticipate human error. Instead of letting the program crash when a user enters a string instead of a number, I used try/except to catch the mistake gracefully and keep the engine running. I’m training my brain to remember that = assigns a value, but == asks a question. I am slowly getting there 🙂 Day 5 is moving into the world of Functions and Iterations (Loops). I’m shifting from making decisions to automating repetitive tasks. The pieces are finally starting to click together. #Python #CodingLogic #BuildInPublic #SoftwareDevelopment #TechJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 42 of My Learning Journey – HTML Parsing with Python Today I explored how to extract meaningful information from HTML using Python’s built-in HTMLParser. 🌐 🔍 What I learned: ✔ How HTML content is parsed into structured components ✔ Difference between Start Tags, End Tags, and Empty Tags ✔ Handling attributes and cases where values may be missing ✔ Implementing a custom parser by extending HTMLParser 💡 Key Concept: HTML parsing works in an event-driven manner, where specific methods are triggered when different parts of HTML are encountered. 🛠 What I built: A Python program that: ✅ Detects HTML tags ✅ Separates start, end, and empty tags ✅ Extracts attributes and their values (even when they are None) 📌 Example Output: Start : body -> data-modal-target > None -> class > 1 ✨ This task helped me understand how web data is structured behind the scenes and how parsers interpret it step by step. 📚 Takeaway: Parsing is a powerful concept used in web scraping, compilers, and data processing. Mastering it opens doors to many real-world applications. #Day42 #Python #HTMLParser #WebDevelopment #LearningJourney #Coding #ComputerScience #StudentLife #Consistency
To view or add a comment, sign in
-
-
🔥 Day 3 of #PythonLearningSeries Hey everyone 👋 Welcome to Day 3! So far, we’ve learned: ✔ How to print output ✔ Variables & data types Today, let’s make our programs more interactive 😄 👉 Input & Output in Python 📌 What is Output? Output means displaying something to the user. We already used this: print("Hello, World!") 👉 print() is used to show output on the screen. 📌 What is Input? Input means taking data from the user. In Python, we use: 👉 input() 💻 Example: name = input("Enter your name: ") print("Hello", name) 👉 What happens here? The program asks for your name You type something It prints a message using your input 🧠 Important Concept: 👉 By default, input() takes data as a string Even if you enter a number, Python treats it as text. ⚠️ Common Beginner Mistake: age = input("Enter your age: ") If you try: print(age + 5) ❌ This will give an error (or wrong result) ✔️ Correct way: age = int(input("Enter your age: ")) print(age + 5) 📌 Type Conversion (Very Important) We convert input into different data types: 👉 int() → Integer 👉 float() → Decimal 👉 str() → String 💻 Example: num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) print("Sum is:", num1 + num2) ✨ Your Turn! 📍 Practice Task: 1️⃣ Ask user for their name and age 2️⃣ Print: "Hello [name], you are [age] years old" 3️⃣ Take two numbers and print their sum 🤔 Quick Question: What will be the type of this input? value = input("Enter something: ") 👉 String or Integer? Comment your answer 👇 🚀 You’re building real logic now—great progress! 🔁 Follow me for Day 4: Operators in Python #Python #LearnPython #CodingJourney #Programming #Beginners #Tech #100DaysOfCode
To view or add a comment, sign in
-
Ever explained Duck Typing in Python to someone and watched their face go from 😃 → 🤯 in 3 seconds? Here’s how I tried explaining it to a friend: Friend: “How does Python know if something is a duck?” Me: Python doesn’t care if it’s a duck 🦆, a robot 🤖, or a developer pretending to work on Friday afternoon 😅 If it walks like a duck and quacks like a duck, Python just says: "Cool… must be a duck." Example 👇 class Duck: def quack(self): print("Quack!") class Person: def quack(self): print("I can imitate a duck!") def make_it_quack(obj): obj.quack() make_it_quack(Duck()) make_it_quack(Person()) Python: "Both quack? Perfect. I’m not asking for ID." Meanwhile in some other languages: "Excuse me sir, please submit 4 forms, 2 interfaces, and a type certificate before quacking." 🧾 That’s the beauty of Python — behavior matters more than type. So remember: In Python, nobody asks what you are. They only check what you can do. And honestly… that’s a life lesson too. 😄 #Python #DuckTyping #ProgrammingHumor #LearnToCode #PythonDeveloper #CodingLife #SoftwareEngineering #TechHumor #DeveloperLife
To view or add a comment, sign in
More from this author
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development