02. Variables, Data Types, and Input–Output

Learn how Python programs store values, distinguish data types, convert and validate input, perform assignment, and produce useful output.

Storing Values with Variables

Programs are built by manipulating values. A variable is a named reference to a value, allowing a program to store information and use it later.

For example, a program might use age to refer to an integer and name to refer to a string. A variable's value can usually change during execution when a new value is assigned to the same name.

A variable must receive a value before the program uses it. Using an undefined name causes an error such as Python's NameError.

Good variable names describe their purpose. Names such as number_of_students and average_temperature communicate more clearly than vague names such as x or data when the context does not explain them.

Takeaway: Use variables to give meaningful names to values, assign them before use, and choose names that make the program's intent clear.

Names, , and Constants

An is a name used for a programming element, including a variable, function, or class. Python identifiers may contain letters, digits, and underscores, but they may not begin with a digit. They are case-sensitive, so total, Total, and TOTAL are different names.

Reserved keywords such as if, class, and return cannot be used as identifiers. A common convention is snake_case, in which words are separated by underscores, as in maximum_speed.

stores the result of an expression in a variable. Python uses a single equals sign, =, for . The expression on the right is evaluated first, and the result is then placed in the name on the left.

is not mathematical equality. The statement x = x + 1 means that the current value of x is increased by one and the new result is stored back in x. By contrast, x == 5 tests whether x has the value five. Python also supports multiple , such as x, y = 10, 20, and values can be exchanged with x, y = y, x.

A is a value intended to remain unchanged. Python programmers commonly write constants with uppercase names, such as DAYS_PER_WEEK or PI_APPROXIMATION, although Python does not enforce this convention.

Takeaway: Identifiers name program elements, changes what a variable refers to, and constants communicate values that should remain stable.

Understanding Data Types

A describes the kind of value stored and the operations that make sense for it. Common basic types include:

  • Integer: a whole number, such as -4, 0, or 27.

  • Floating-point number: a number with a fractional part, such as 3.14 or -0.5.

  • Boolean: a logical value, either True or False.

  • Character: a single text symbol in languages that distinguish characters from strings.

  • String: a sequence of text characters, such as "hello" or "42".

Python commonly represents these categories with int, float, bool, and str. The value "42" is a string even though its characters look like a number; it cannot automatically be treated as the integer 42 in every operation.

A variable may refer to one basic value or to a collection of values. For example, a list can group several numbers. Lists and arrays are compound data structures, while an integer, Boolean, or string represents one basic piece of information.

The type() function can inspect the type of a value. Understanding types helps explain why some operations are valid and why others produce errors.

Takeaway: A value's type determines what the value represents and which operations can be performed safely.

Converting Between Types

changes a value from one to another. Python provides several built-in conversion functions:

  • int() converts to an integer when possible.

  • float() converts to a floating-point number.

  • str() creates a string representation.

  • bool() converts to a Boolean value.

For example, int("42") produces the integer 42, float("3.5") produces a floating-point value, and str(2026) produces text. Conversion is not always possible: int("hello") raises a ValueError because the text does not represent an integer.

Some conversions lose information. In Python, converting 7.9 to an integer produces 7, while converting -7.9 produces -7; the fractional part is truncated toward zero.

An explicit conversion is requested by the programmer. An implicit conversion, also called coercion, happens automatically in certain language contexts. For example, Python's / operation produces a floating-point result when dividing two integers. Because conversion rules vary among programming languages, explicit conversion is often clearer and safer.

Takeaway: Convert values deliberately, check whether the conversion is valid, and remember that conversion can change or discard information.

, Processing, and

is data supplied to a program from an external source, such as a keyboard, file, sensor, or another program. Python's () function reads a line from the user and returns it as a string, even if the user types digits.

When a program needs an integer, the returned text must be converted, conceptually as int(("Enter your age: ")). Decimal can be converted with float(). A robust program anticipates invalid entries, such as text that cannot be interpreted as a number, and responds appropriately.

is information produced by a program. Python's print() function writes values to standard . It can print individual values or several values together, such as a name and score. Formatted strings, or f-strings, make it convenient to combine text with variable values. A format specification such as .2f displays two digits after the decimal point.

A common program structure is :

  1. : receive the required data.

  2. Process: calculate or transform the data.

  3. : report the result.

For a rectangle-area program, the inputs are the length and width, the process multiplies them, and the reports the calculated area. If the inputs are 8 and 5, the result is 40, which could be displayed as 40.00 when two decimal places are requested.

Takeaway: Treat as text until it has the required type, validate data that may be invalid, and use to communicate the processed result clearly.

Checking Types and Avoiding Common Errors

Several mistakes occur repeatedly when working with variables and -:

  • Using a name before : Referencing total before giving it a value causes an error.

  • Confusing strings and numbers: () returns text, so adding 1 directly to the returned value is invalid. Convert it to an integer first.

  • Confusing and comparison: Use = to store a value and == to test equality.

  • Choosing misleading names: days_in_year communicates more than x when the value represents the number of days in a year.

  • Assuming every conversion is valid: Converting text such as "three" with int() fails and should be validated or handled.

A useful debugging approach is to inspect each stage of the program. Confirm that every name has been assigned, check the type of each value, verify that conversions match the expected , and examine whether the reflects the intended process.

Takeaway: Most beginner errors come from unclear names, incorrect types, invalid conversions, or confusing with comparison.