Loading lesson...
Making decisions with data
Making decisions with data
Topics covered: Type Conversions, Comparison Operators, Logical Operators, Multiple Assignment, None and Identity
Remember from the beginner lesson that every value has a type? An integer like 42 is different from a string like "42", even though they look similar. Sometimes you need to convert between these types. Why Convert Between Types? Data doesn't always arrive in the format you need. Here are common situations where type conversion is essential: For example, if a user types "25" into an age field, your program receives the string "25", not the number 25. To check if they're old enough to vote, you ne
Programs need to make decisions. Should this user be granted access? Is the account balance sufficient? Has the deadline passed? These are all yes-or-no questions, and comparison operators let you ask them. What is a Comparison? Six Comparison Operators Python has six comparison operators. Each one asks a different type of question: Checking if two values are exactly the same: Greater-than and less-than operators compare which side is larger or smaller: Comparing Strings You can compare strings
Real-world decisions are rarely simple. "Can this user access the file?" might depend on multiple factors: Are they logged in? Do they have permission? Is the file not locked? Logical operators let you combine multiple conditions into one decision. The Three Logical Operators Python has three logical operators. Think of them as ways to connect yes-or-no questions: The "and" Operator Imagine a nightclub that requires guests to be 21+ AND have valid ID. Both conditions must be met: can_enter is Tr
Python has a convenient feature that lets you assign values to multiple variables in a single line. This makes certain patterns cleaner and more readable. Assigning Multiple Vars The values on the right are matched up with the variables on the left, in order. Swapping Values In many languages, swapping requires a temporary variable. In Python: Python evaluates the right side first (b, a), then assigns to the left side. No temp variable needed. Unpacking from Collections You can "unpack" a tuple
What is None? Functions and None Checking for None Why "is" Instead of "=="?