2 Variables and Data Types
Learn how Python names, assignments, core data types, conversions, strings, numeric operations, and Boolean logic work together in practical programs.
Names, Values, and Types
Python programs work with values such as numbers, text, and truth values. A gives a readable way to refer to an object, while a variable is commonly understood as a whose binding can change during execution.
Python uses : the type belongs to the object currently referred to, not permanently to the . For example, a may first refer to an integer and later refer to a . Names are case-sensitive and should communicate purpose, commonly through snake_case, such as student_name or total_cost.
A describes what kind of value an object represents and which operations make sense for it. The main basic types are:
int: integer values such as , , and .float: approximate floating-point values such as and .complex: complex numbers such as .str: text values.bool: the truth valuesTrueandFalse.NoneType: the type of the single valueNone, commonly used to represent no value.
Use type() when you need to inspect the type of an object.
Takeaway: Names provide readable access to objects, and Python determines the type from the value currently bound to each .
Binding and Rebinding Names
binds a to the result of an expression with the operator . The right-hand expression is evaluated first. For example, if width is and height is , the expression for the area is:
can rebind a . If temperature is first assigned and then assigned , it refers to afterward. is therefore different from mathematical equality: performs binding in Python, while compares values.
Python also supports multiple . Two names can receive two values in one statement, and the existing values can be swapped without a temporary because the right-hand side is evaluated before the new bindings take effect. Augmented , such as count += 1, updates a value using its previous value.
A must be assigned before it is read. Attempting to use an unassigned produces a NameError.
Takeaway: controls which object a refers to, and reassignment changes that reference rather than changing the meaning of the itself.
Numeric Values and Operations
Python provides familiar operations for numeric values. For an integer whole with value , the main arithmetic operators behave as follows:
Addition uses , so .
Subtraction uses , so .
Multiplication uses , so .
Ordinary division uses conceptually and produces a
float; .Floor division uses
//and returns the floor of the quotient; .The remainder operation uses
%; .Exponentiation uses
**; .
When an operation combines an integer and a floating-point value, Python generally performs the calculation using a floating-point value. Floating-point numbers are approximate, so a decimal calculation such as may display as . Applications that require exact decimal precision may need a specialized decimal type.
Takeaway: Choose the arithmetic operator according to whether you need ordinary division, a floor quotient, a remainder, or a power, and remember that floating-point results can be approximate.
Converting Between Types
changes a value into another type. Common constructors include int(), float(), str(), bool(), and complex().
Examples include converting the text "25" to the integer , converting "19.99" to a floating-point value, and converting to text. The input must be compatible with the requested type. For instance, int("2.5") is invalid because the text represents a decimal value rather than an integer literal.
Converting a float to an int discards the fractional part rather than rounding. Thus, int(7.9) produces , while int(-7.9) produces .
The input() function always returns a . Convert that result before performing numeric operations. A program that receives an age should convert the input to int before adding a year or comparing it with a numeric threshold.
Convert values near the boundary where their intended meaning becomes known, such as immediately after user input. This keeps later calculations and comparisons consistent.
Takeaway: Conversion is necessary when a value arrives in the wrong representation, but the conversion must match the value’s actual content.
Working with Text
A is an immutable sequence of Unicode characters. Single quotes, double quotes, and triple quotes can delimit literals. Raw- notation can be useful when text contains backslashes.
Strings support several operations:
Concatenation joins strings with
+.Repetition creates repeated text with
*.Indexing retrieves one position, starting at index .
Negative indexing counts from the end.
Slicing creates a portion of a , such as the characters from one boundary up to another.
len()reports the number of characters.An f- inserts current values into readable text.
Because strings are immutable, an operation cannot replace one character inside an existing . Instead, it creates a new and the can be rebound to that new value.
For example, adding a title to the Maya creates a new value such as Dr. Maya; it does not modify the original object in place.
Takeaway: Treat strings as sequences that can be inspected and used to build new text, not as mutable character containers.
Conditions and Truth Values
A produces a truth value through comparison or logic. The comparison operators are less than , less than or equal to , greater than , greater than or equal to , equal to , and not equal to .
Do not confuse with comparison . An changes a binding, while a comparison asks whether two values match. For example, a program can assign limit the value , then evaluate whether limit == 10 is true.
Use and, or, and not to combine conditions. A condition such as being at least and having an identification document requires both parts to be true. A condition using or is true when at least one part is true. not reverses a truth value.
Python also assigns truth values to many non-Boolean objects. False, None, numeric zero, empty strings, and empty collections are false in Boolean contexts; most other values are true. This allows concise checks, although explicit comparisons are clearer when a program must distinguish among zero, None, and an empty .
and and or use short-circuit evaluation: Python may stop as soon as the final result is known. This can prevent unnecessary evaluation of later expressions.
Takeaway: Use comparisons to produce conditions, logical operators to combine them, and explicit checks when different false-like values have different meanings.
Practical Patterns and Review
A reliable Python workflow keeps values compatible with the operations being applied to them.
Choose a descriptive that reflects the value’s purpose.
Inspect an unfamiliar value with
type()andprint().Convert input as soon as its intended meaning is known.
Compare compatible types rather than comparing a number directly with text.
Use clear Boolean conditions when distinguishing among values such as zero,
None, and an empty .
For a simple total, a program might keep a quantity as an integer, a unit price as a floating-point value, and calculate the result with:
The important design principle is consistency: each should refer to a value whose type matches the operations the program needs to perform.
Final takeaway: Python programs become easier to understand and debug when names are clear, assignments are deliberate, types are known, conversions happen at boundaries, and conditions compare compatible values.