#DAY 21/ 50DaysChallenge #Python Modules 🔥 Module: A module is a file that contains Python code (functions, variables, classes) which we can reuse in another program. 👉 Python has built-in modules. Example: math random datetime ✅ Import a Module: Code import math print(math.sqrt(16)) Output 4.0 👉 sqrt() means square root. ✅ Using math module: Code import math print(math.pow(2,3)) print(math.factorial(5)) Output 8.0 120 ✅ Import specific function: Instead of importing full module. Code from math import sqrt print(sqrt(25)) Output 5.0 ✅random Module: Used to generate random numbers. Code import random print(random.randint(1,10)) Output Example: 7 (Random value each time) ✅ Create Your Own Module: #Step 1: Create file mymodule.py def greet(name): print("Hello", name) #Step 2: Use it in another file import mymodule mymodule.greet("Durga") Output Hello Durga 🎯 Task : 👉 Generate a random number between 1 and 100 ✅ Code import random num = random.randint(1,100) print("Random number:", num) Output Random number: 57 #50dayschallenge #coding #pythonbasics #ECE #BVCEC
Python Modules: Importing and Creating Your Own
More Relevant Posts
-
🚀 Mastering Loops in Python 🐍 Loops in Python are essential for repeating tasks efficiently. They allow you to iterate over a sequence of elements such as lists or strings, executing the same block of code multiple times. This is incredibly useful for automating repetitive operations and processing large amounts of data in your programs. For developers, understanding loops is crucial as they form the backbone of many algorithms and data processing tasks. By mastering loops, you can write more concise and elegant code, improving the efficiency and readability of your applications. 🔎 Let's break it down step by step: 1️⃣ Initialize a counter variable 2️⃣ Set the condition for the loop to continue 3️⃣ Execute the code block inside the loop 4️⃣ Update the counter to progress through the sequence ```python # Example of a for loop in Python for i in range(5): print("Iteration", i) ``` 🚩 Pro Tip: Use `enumerate()` to access both the index and value of an item in a loop effortlessly. ❌ Common Mistake: Forgetting to update the counter variable in a loop, leading to an infinite loop and crashing your program. 🤔 What's your favorite use case for loops in Python? 🌐 View my full portfolio and more dev resources at tharindunipun.lk #PythonProgramming #DeveloperTips #CodingCommunity #LearnToCode #LoopInPython #CodeNewbie #TechTalks #ProgrammingLife
To view or add a comment, sign in
-
-
Challenge 89 Days Subject :- Why do we use 'f' in Python's print statement? 🐍 When I started learning Python, I noticed this tiny prefix f before strings, like this: Example:- Python name = "Umesh" language = "Python" 👉 # The Magic of f-strings print(f"My name is {name} and my favorite language is {language}.") 🤜 What exactly is this f? It stands for Formatted String Literal (commonly known as f-strings). Introduced in Python 3.6, it’s a game-changer for writing clean and readable code. 😎 Why use f-strings instead of old methods? 1. Readability: You don't have to deal with messy plus signs + or commas , to join text and variables. You just write the sentence naturally. 2. Variable Embedding: You can place variables directly inside the string using curly braces {}. Python automatically replaces them with their values. 3. No Type Errors: Usually, Python complains if you try to add a Number to a String. With f-strings, Python handles the conversion for you behind the scenes. 4. Speed: Not only is it easier to read, but f-strings are also faster than the older .format() or % methods. Big thanks to Parth Verma sir & The Valuation School for the inspiration. I really appreciate your guidance, Kuldeep Singh Rathore Keep Learning, Keep Growing Umesh Jadhav #Python #Coding #ProgrammingTips #LearningToCode #Python3 #SoftwareDevelopment #TipsAndTricks #CFA #LinkedinLearning
To view or add a comment, sign in
-
-
For my Day 15 I covered Python String Formatting and I have to say — this one clicked really fast for me. Here is what I covered: F-Strings This is the modern way to format strings in Python (available from version 3.6). All you do is put the letter f in front of your string and use curly brackets {} to drop in your variables directly. It is clean, simple, and easy to read. I actually enjoyed writing these. .format() Method This is the older way of doing the same thing. Instead of putting f at the front, you write your string normally and call .format() at the end to pass in the values. Still works perfectly and good to know, but f-strings feel much neater. Placeholders and Modifiers This was my favourite part. Inside the curly brackets {}, you can add a colon : followed by a modifier to control exactly how your value is displayed. For example: :.2f gives you 2 decimal places :, adds a comma as a thousand separator :% converts a number to a percentage :> :< :^ lets you align your text right, left, or centre So instead of just printing a raw number like 1500000, I can now print it as $1,500,000.00. That small difference makes everything look so much more professional. As someone learning Python for data analysis, I can already see how useful this will be when I start working with real data and building reports in pandas. Day 16 we go into revision mode 5 days of practicing everything I have covered so far before jumping into pandas. The foundation is almost ready. #Python #100DaysOfCode #DataAnalysis #LearningInPublic #W3Schools #NeverStopLearning
To view or add a comment, sign in
-
-
🧠 Python Concept: for-else Loop Yes… Python loops can have an else block 👀 🤔 How It Works The else runs only if the loop completes normally (not if it breaks). 🧪 Example numbers = [1, 3, 5, 7] for num in numbers: if num % 2 == 0: print("Even found") break else: print("No even numbers") ✅ Output No even numbers ⚡ When break Happens numbers = [1, 3, 4, 7] for num in numbers: if num % 2 == 0: print("Even found") break else: print("No even numbers") ✅ Output Even found 🧒 Simple Explanation Imagine a teacher checking students 👩🏫 If she finds a rule-breaker → stops (no else) If she checks everyone → says “All good!” (else runs) 💡 Why This Matters ✔ Cleaner search logic ✔ Avoids extra flags ✔ Pythonic pattern ✔ Useful in real projects 🐍 Python loops can have an else block 🐍 It runs only when the loop completes without a break. 🐍 A small feature, but very powerful. #Python #PythonTips #PythonTricks #AdvancedPython #Loop #ForElse #PythonLoop #CleanCode #LearnPython #Programming #DeveloperLife #DailyCoding #100DaysOfCode
To view or add a comment, sign in
-
-
Day 47 : Python For Loops & Iteration Today I understood the For Loop and how it is helpful for iteration. Hands-on : - Today I learned about for loops in Python, which are used to iterate over sequences and perform repetitive tasks efficiently. - I started with the basic syntax of a for loop, understanding how it loops through elements in a sequence like a list, string, or range. - I then explored how to properly use variable names in for loops, making the code more readable and meaningful. - Moving forward, I practiced looping through a dictionary, where I accessed keys, values, and key-value pairs using different methods. - Finally, I worked on nested for loops, where one loop runs inside another. - - This is especially useful when working with multi-dimensional data or patterns. Result : - Successfully understood how to use for loops to iterate through different data structures and perform repetitive operations in Python. Key Takeaways : - For loops are used to iterate over sequences like lists, strings, and ranges. - Meaningful loop variable names improve code readability. - Dictionaries can be iterated using keys, values, or items. - Nested loops help handle complex data structures and repeated patterns. #Python #Programming #DataAnalytics #LearningJourney #ForLoop #CodingBasics #DataScience #BeginnerPython #AnalyticsSkills
To view or add a comment, sign in
-
-
Day 49 : Python Functions & Arguments Today I understood the functions and arguments and its usage. Hands-on : - Today I learned about functions in Python, which help organize code into reusable blocks. I started with the basic syntax of a function, understanding how to define and call functions for specific tasks. - I then explored arguments in functions, which allow passing data into functions. - I practiced using multiple arguments to handle more than one input. - ------ Moving forward, I learned about arbitrary arguments (*args), which let functions accept a variable number of positional inputs. - I also worked with keyword arguments, where values are passed using parameter names for better clarity. - Finally, I explored **arbitrary keyword arguments (kwargs), which allow passing a variable number of named arguments into a function. - These concepts are essential for writing flexible and reusable code in real-world applications. Result : - Successfully understood how to create functions and use different types of arguments to make code more modular and dynamic. Key Takeaways : - Functions help in code reusability and modular programming. - Arguments allow passing data into functions. - Multiple arguments enable handling several inputs. - *args allows variable positional arguments. - Keyword arguments improve readability. - **kwargs allows variable named arguments. #Python #Programming #DataAnalytics #LearningJourney #Functions #CodingBasics #DataScience #BeginnerPython #AnalyticsSkills
To view or add a comment, sign in
-
-
Day 46 : Python Conditional Statements – If/Else Today I understood the conditional statements used in Python. Hands-on : - Today I learned about conditional statements in Python, which are used to control the flow of a program based on conditions. - I started with the basic syntax of the if statement, understanding how Python evaluates conditions and executes code blocks. -I then explored the if/else statement, which allows execution of alternate code when a condition is false. - Moving forward, I practiced if/elif/else statements to handle multiple conditions efficiently. - I also learned how to write if/else in a single line (ternary operator), which makes simple conditions more concise. - Finally, I explored nested if/else statements, where one condition is placed inside another to handle more complex logic. Result : - Successfully understood how to implement conditional logic in Python using different forms of if/else statements. Key Takeaways : - If statement executes code only when a condition is true. - If/Else provides an alternative execution path. - If/Elif/Else helps handle multiple conditions efficiently. - One-line if/else (ternary) makes code concise for simple conditions. - Nested conditions allow handling complex decision-making scenarios. #Python #Programming #DataAnalytics #LearningJourney #ConditionalStatements #CodingBasics #DataScience #BeginnerPython #AnalyticsSkills
To view or add a comment, sign in
-
-
🧠 Python Concept: Chained Comparisons ✨ Python lets you combine multiple comparisons in one expression. ❌ Traditional Way x = 10 if x > 5 and x < 20: print("x is between 5 and 20") ✅ Pythonic Way x = 10 if 5 < x < 20: print("x is between 5 and 20") Cleaner and easier to read 🎯 ⚡ Another Example score = 85 if 60 <= score <= 100: print("Valid score") 🧒 Simple Explanation Imagine checking if a student’s height is between two marks 📏 Instead of saying: height > 100 AND height < 150 You simply say: 100 < height < 150 Python understands it directly. 💡 Why This Matters ✔ Cleaner conditions ✔ More readable code ✔ Fewer logical mistakes ✔ Pythonic style 🐍 Python allows elegant chained comparisons 🐍 Instead of writing x > 5 and x < 20, you can simply write 5 < x < 20. #Python #PythonTips #PythonTricks #AdvancedPython #CleanCode #LearnPython #Programming #DeveloperLife #DailyCoding #100DaysOfCode
To view or add a comment, sign in
-
-
Really excited to share my week1 python learning blog. Topic: variables and data Types In this article ,I have covered up by exploring the fundamentals of python including variables, examples and , data types as well with the common bugs with the solutions. please read here: https://lnkd.in/gryZb6-t #python #programing #Learningjourney#GitHub #Coding#Developers
To view or add a comment, sign in
-
Python list: a simple tool with real power In Python, list is one of the most commonly used data structures. It’s simple, flexible, and essential for everyday development. A list is an ordered, mutable collection: items = [1, "text", True] You can easily modify it: items.append(4) items[0] = 10 One important detail: because lists are mutable, they should not be used as default arguments in functions. def add_item(item, my_list=[]): # ⚠️ bad practice my_list.append(item) return my_list This can lead to unexpected behavior because the same list is reused between function calls. Better approach: def add_item(item, my_list=None): if my_list is None: my_list = [] my_list.append(item) return my_list One of the most powerful features is list comprehension, which makes code concise and readable: squares = [x**2 for x in range(10)] Why it matters Lists are everywhere - from API responses to data processing and backend logic. Understanding their behavior helps you avoid subtle bugs and write more reliable code. #Python #Programming #SoftwareEngineering
To view or add a comment, sign in
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