Basic Practice Programs in Python: class BannkAccount: def __init__(self, name, balance): self.name = name self.balance = balance def deposit(self,amount): if amount > 0: self.balance += amount def withdraw(self,amount): if amount <=self.balance: self.balance -= amount return True print("Insufficient funds") return False def showbalance(self): print(f"{self.name} balance: {self.balance}") if __name__ == "__main__": acc = BannkAccount("Venkata", 1000) acc.showbalance() acc.deposit(500) acc.showbalance() acc.withdraw(300) acc.showbalance() acc.withdraw(2000) acc.showbalance()
Creating a Basic Bank Account Class in Python
More Relevant Posts
-
Solved problem 347 in Leetcode! Solution Python: class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count_nums = {} for num in nums: count_nums[num] = count_nums.get(num, 0) + 1 array_nums = [[] for i in range(len(nums) + 1)] for num, count in count_nums.items(): array_nums[count].append(num) result = [] for i in range(len(nums), 0, -1): for num in array_nums[i]: result.append(num) if len(result) == k: return result
To view or add a comment, sign in
-
Understanding Python's Anonymous Lambda Functions Lambda functions in Python provide a concise way to create anonymous functions. They are particularly useful when you need a small function for a short period, without the need to formally define it using `def`. This allows for cleaner and more readable code, especially in functions like `map()`, `filter()`, or `sorted()` where a full function definition may feel unnecessarily verbose. The syntax is quite straightforward: `lambda arguments: expression`. The body of a lambda function can only contain a single expression and cannot contain commands or multiple statements. While this limitation might seem restrictive, it encourages a more focused approach to small operations, making them easily readable. When using lambda functions for operations like sorting, they become a powerful tool. In the provided example, the list of tuples is sorted based on the string representation of the second element. This wouldn't be as elegant with a traditional function defined using `def`, which would require additional lines to define and call. Understanding these nuances of lambda functions is critical in writing efficient Python code. They shine most when used in contexts where you need a quick, throwaway function. Quick challenge: How would you modify the lambda function to return the cube of a number instead of the square? #WhatImReadingToday #Python #PythonProgramming #LambdaFunctions #CleanCode #Programming
To view or add a comment, sign in
-
-
Python Internals: Mutable vs Immutable In Python, variables don’t store values. They store references to objects. Whether an object is mutable or immutable determines how Python handles: • Assignment • Memory • Object identity • Shared state And this distinction explains many subtle bugs in real systems. Immutable Objects: Examples: int, float, str, tuple They cannot be modified in-place. When you “change” them, Python creates a new object and rebinds the reference. The object’s identity changes. Mutable Objects: Examples: list, dict, set They support in-place modification. When mutated, the object’s identity remains the same, but its internal state changes. Core Principle: Assignment ≠ Mutation Rebinding creates a new object. Mutation modifies the existing object. Understanding this is fundamental to mastering Python’s object model. #connections
To view or add a comment, sign in
-
Most Python code gets harder to read as the logic gets smarter. But not 𝗧𝗼𝗼𝗹𝘇 for Python. It gives you utility functions for iterators, functions, and dictionaries that work together naturally. Toolz feels especially relevant for data work, where clean transformations can save more time than any micro optimization. What I like most is that it stays pragmatic. Just simple Python functions that help you write code that is easier to understand without giving up performance. 🔗 Link to repo: github(.)com/pytoolz/toolz --- ♻️ Found this useful? Share it with another builder. ➕ For daily practical AI and Python posts, follow Banias Baabe.
To view or add a comment, sign in
-
-
Python: try-except-else-finally Python provides four blocks for handling exceptions: • try: - Code that might raise an error. • except: - Runs only if an error occurs. • else: - Runs only if no error occurs in the try block. • finally: - Always runs — whether there is an error or not. In short: - Error happens → except runs. - No error → else runs. - In both cases → finally runs. Why this structure is useful: • Keeps error handling separate from success logic. • Ensures cleanup always happens. • Helpful for closing files or database connections. • Makes programs safer and more predictable. Do you use all four blocks in your projects?
To view or add a comment, sign in
-
-
Python: try-except-else-finally Python provides four blocks for handling exceptions: • try: - Code that might raise an error. • except: - Runs only if an error occurs. • else: - Runs only if no error occurs in the try block. • finally: - Always runs — whether there is an error or not. In short: - Error happens → except runs. - No error → else runs. - In both cases → finally runs. Why this structure is useful: • Keeps error handling separate from success logic. • Ensures cleanup always happens. • Helpful for closing files or database connections. • Makes programs safer and more predictable. Do you use all four blocks in your projects?
To view or add a comment, sign in
-
-
🚀 Mini Project: Emoji Converter using Python As part of learning Python strings, I built a simple Emoji Converter that transforms text-based emoticons like :) and :( into actual emojis 😊 💡 How the Code Works: The program takes user input using input(). It uses the string replace() method to find specific text patterns (like :)). Since strings are immutable in Python, each replace() call returns a new updated string. The final modified string is printed as output. 😊 How I Added Emojis: Emojis were added directly inside the string as Unicode characters (e.g., "😊"). Python supports Unicode by default, so emojis can be stored and printed like normal text. This project helped me strengthen my understanding of: ✔ String manipulation ✔ The replace() method ✔ Unicode characters in Python Small projects like this help build strong fundamentals 💻✨ Excited to keep learning and building more! #Python #BeginnerProject #BTechCSE #CodingJourney
To view or add a comment, sign in
-
-
Hii, Everyone Today I Went Into More Topics Like 1.Adding Whitespace to Strings with Tabs or Newlines : In Python, you can add whitespace to strings using escape characters like tabs (\t) and new lines (\n). These help format text output neatly. You Can also Combine tabs and Newlines in a Single String 2.Avoiding Syntax Errors with Strings : In Python, syntax errors with strings usually happen when quotes are used incorrectly. Python cannot understand where the string starts or ends. To avoid this, you need to follow some simple rules. 3.INTEGERS : In Python, an integer is a whole number (a number without a decimal point). Definition An integer is a number that can be: - Positive - Negative - Zero But it does not contain decimal values. 4.FLOATS : In Python, a float (floating-point number) is a number that contains a decimal point. Definition A float is a number used to represent decimal values. It can be: - Positive - Negative Zero with decimal 5.Avoiding Type Errors with the str() Function : In Python, a TypeError happens when you try to combine different data types incorrectly, such as adding a string and an integer. The str() function helps avoid this error by converting other data types into strings.
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