Python: Conditionals & Truthy/Falsy if name != "": print(f"Hello {name}") But in Python, you can do it the clean way 👇 if name: print(f"Hello {name}") Why? Because an empty string "" is falsy, Python treats it as False automatically. Common falsy values: False None 0 "" [] {} Quick test: print(bool(0)) # False print(bool("hi")) # True print(bool([])) # False print(bool([1, 2, 3])) # True Resource: https://lnkd.in/dEMihMwF #Python
Python Conditionals: Truthy and Falsy Values Explained
More Relevant Posts
-
Let's look at Python (in)consistency: >>> s='a' >>> s.split()==s.split(' ') True >>> s='' >>> s.split()==s.split(' ') False How come? The point here -- let me quote the docs -- "If sep is not specified or is None, a different splitting algorithm is applied". If separator is not specified, empty strings at the beginning and the end of the result are discarded, so while ''.split(' ') is [''], ''.split() is []. #Python #gotcha
To view or add a comment, sign in
-
🐍 Python Tip: = vs == in if statements A common beginner mistake in Python is using = instead of == inside if conditions. = → assignment == → comparison Inside if / elif, Python expects a boolean expression, not an assignment. ❌ Wrong: if score >= 90 and project = True: print("A") ✅ Correct (Pythonic): if score >= 90 and project: print("A") 🧠 Tip: If a variable is already boolean, don’t compare it to True. Just use it. Small detail, big difference. #Python #PythonProgramming #LearnPython #Coding #Programming #SoftwareDevelopment #DataAnalytics #DataScience #TechCareers #CodingTips #BeginnerTips #CleanCode #PythonTips #DeveloperLife
To view or add a comment, sign in
-
Python Strings Today I learned about Strings in Python, and this topic is way more powerful than it looks at first glance. Strings are used to store and manipulate text — names, messages, user input, file data, and more. In real-world applications, text handling is everywhere, which makes strings a core concept in Python. I explored: Creating strings using single and double quotes Indexing and slicing strings Common string methods like lower(), upper(), strip(), replace(), find(), count() String concatenation and formatting One important thing I learned is that strings are immutable in Python. You can’t change a character directly — instead, Python creates a new string. Understanding this helps avoid a lot of beginner confusion and bugs. This topic made me realize that even simple-looking concepts have deep logic behind them. The better you understand strings, the cleaner and more efficient your programs become. Learning slowly. Building strong foundations. #Python #Strings #PythonBasics #LearningJourney #ProgrammingConcepts #DeveloperMindset
To view or add a comment, sign in
-
-
Python Sets — Explained Simply. A set in Python is an unordered, mutable collection of unique elements. Sets are one of the most powerful (and often misunderstood) data structures in Python. They are: • Unordered • Mutable • Unique by nature This one infographic covers: ✔ What a set is ✔ When to use it ✔ Most important set methods with examples If you work with Python, this is something you’ll use more often than you realize. - Save this for quick revision - Which set method do you use the most? #Python #LearnPython #PythonBasics #DataStructures #Coding #SoftwareEngineering #PythonDeveloper
To view or add a comment, sign in
-
-
Tuples in Python: Immutable (cannot be changed after creation), ordered collection that allows duplicate elements. Syntax:() Two methods: Index(),count() Python 3.14.0 (tags/v3.14.0:ebf955d, Oct 7 2025, 10:15:03) [MSC v.1944 64 bit (AMD64)] on win32 Enter "help" below or click "Help" above for more information. >>> #tuples() >>> a=(4,5.7,"python",4+9j,True,False) >>> print(a) (4, 5.7, 'python', (4+9j), True, False) >>> type(a) <class 'tuple'> >>> len(a) 6 >>> a.count("python") 1 >>> a.index(True) 4 Pooja Chinthakayala Mam,Saketh Kallepu Sir,Uppugundla Sairam Sir
To view or add a comment, sign in
-
🟦🟨🟩 🐍 PYTHON – DAY 7 🟦🟨🟩 📌 If–Else Statements What is If–Else? If–else statements are used to make decisions based on conditions in Python. ✨ Example: age = 18 if age >= 18: print("Eligible to vote") else: print("Not eligible to vote") 🔹 Key Points ✔ Used for decision making ✔ Conditions return True or False ✔ Supports elif for multiple conditions 📌 Why If–Else is Important? Decision-making logic is a core concept and is commonly asked in Python interviews.
To view or add a comment, sign in
-
Am I too late? I just discovered match-case in Python! If you have used "switch-case" in other languages, "match-case" is Python’s way of doing something similar, but with more flexibility. It helps handle multiple conditions in a clean, readable way. Where it really comes in handy: 1. Routing logic in applications (choosing actions based on user input). 2. Handling different types of messages or events. 3. Simplifying long if / elif / else chains. 4. Working with structured data like tuples, lists, or dictionaries. Honestly, it makes your code much easier to read and maintain when there are multiple possibilities to consider. If you are just finding out about it like I did, I would definitely recommend checking it out and getting familiar with how it works, you might be surprised. If you have used it before, I’d love to hear your take on it. #Python #BackendDevelopment
To view or add a comment, sign in
-
📘 Python Comments Comments are used to explain code and make it easier to understand. They are ignored by the Python interpreter during execution. 🔹 Single-Line Comments • Created using the # symbol • Used to explain a single line of code • Multiple single-line comments can be used for multiline explanations 🔹 Multi-Line Comments • Written using triple quotes (''' or """) • Used to describe code logic or add documentation • Often used for docstrings Comments do not affect the output of a program, but they greatly improve code clarity. #Python #PythonComments #ProgrammingBasics #LearningJourney #Upskilling
To view or add a comment, sign in
-
-
🔷 Python Type Casting | Using type() Function In Python, the type() function is used to check the data type of a variable. Type casting helps us convert one data type into another when needed. 🔹 Example • x = 100 print(type(x)) ▶ Output: <class 'int'> • x = float(100) print(type(x)) ▶ Output: <class 'float'> 🔹 Key Point ✔ type() shows the current data type ✔ float() converts an integer into a decimal value Using type() with type casting helps in writing accurate and flexible programs. #Python #TypeCasting #TypeFunction #ProgrammingBasics #LearningJourney #Upskilling
To view or add a comment, sign in
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