Classic use of #Python's *args: def mysum(*args): total = 0 for n in args: total += n return total Invoke it with separate int arguments: mysum(10, 20, 30, 40, 50) and *not* with a list containing ints: mysum([10, 20, 30, 40, 50]) # Error!
Python Function Args Summation
More Relevant Posts
-
🧠 Python Trap You’ll Probably Hit Once When you create a list using multiplication like * 3, Python doesn’t create separate inner lists. Instead, it creates multiple references to the same list in memory. So when you modify one, all of them change together. But when you use a list comprehension, each inner list is created independently. That means changes stay isolated, exactly as you’d expect. This small difference is responsible for a lot of confusing bugs, especially in nested data structures. Reference: https://lnkd.in/gWBiknUH #pythonprogramming #learnpython #coding #python
To view or add a comment, sign in
-
-
Day 31/100 – #100DaysOfCode 🚀 Solved LeetCode #1346 – Check If N and Its Double Exist (Python). Today I practiced brute-force comparison to check whether there exist two indices i and j such that arr[i] = 2 * arr[j]. Approach: 1) Use two nested loops to check all possible pairs. 2) Ensure that i ≠ j. 3) For each pair, check if arr[i] == 2 * arr[j]. 4) If condition is satisfied, return True. 5) If no such pair is found, return False. Time Complexity: O(n²) Space Complexity: O(1) Starting with brute force helps build understanding before optimization 💪 #LeetCode #Python #DSA #Arrays #BruteForce #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
If a #Python string contains an ISO-formatted date, skip strptime and use datetime.fromisoformat: import datetime as dt dt.datetime.fromisoformat('2026-04-12') # returns datetime at midnight dt.datetime.fromisoformat('2026-04-12 12:34:56') # returns datetime at 12:34:56
To view or add a comment, sign in
-
-
Want to parse a #Python string into a datetime? Use strptime, passing (a) the string and (b) the date format spec: datetime.datetime.strptime('2026-04-01', '%Y-%m-%d') datetime.datetime.strptime('26-April-01', '%y-%B-%d') Both return datetime.datetime(2026, 4, 1, 0, 0)
To view or add a comment, sign in
-
-
Here’s a tiny Python change that pays off fast. Python tip: use `@dataclass(slots=True)` for high-volume models. It removes per-instance `__dict__`, which usually means lower memory usage and slightly faster attribute access. Great for DTOs, parser outputs, event payloads, and cache objects where shape is fixed. Mini rule: if the object schema is stable, add `slots=True` by default. #Python #Performance #CodeQuality #SoftwareEngineering
To view or add a comment, sign in
-
More from this author
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