Unit 1 · Expressions and Type Conversion
Expressions and Type Conversion
Learn how Python combines values and operators into expressions, and how to safely convert values from one data type to another.
Introduction
An expression is any valid combination of values, variables, and operators that Python can evaluate to produce a result. Along with expressions, this chapter covers type conversion — the process of converting one data type into another, either automatically or manually.
Table of Contents
What is an Expression?
An expression is a combination of values, variables, operators, and function calls that
Python evaluates to produce a single result. For example, 5 + 3 is an expression that evaluates to 8.
Tip
Even a single value like 10 or a variable like x counts as a valid
expression, since it evaluates to itself.
Types of Expressions
Arithmetic Expression
Combines numeric values using arithmetic operators. Example:
a + b * 2
Relational Expression
Compares two values and returns a boolean. Example: a >
b
Logical Expression
Combines boolean values. Example: a > 0 and b > 0
String Expression
Combines strings, often with concatenation. Example: "Hi " +
name
What is Type Conversion?
Type conversion is the process of converting a value from one data type to another —
for example, converting a string "25" into an integer
25. Python supports two kinds: implicit
(automatic) and explicit (manual).
Implicit Type Conversion
Python automatically converts one data type to another without any explicit instruction from the programmer, usually to avoid data loss.
a = 5 # int b = 2.5 # float c = a + b # Python converts 'a' to float automatically print(c) # 7.5 print(type(c)) # <class 'float'>
Explicit Type Conversion (Type Casting)
When the programmer manually converts a value from one type to another using built-in functions, it is called explicit conversion or type casting.
age_str = "20" age_int = int(age_str) # explicitly convert str to int print(age_int + 5) # 25
Common Conversion Functions
| Function | Converts To | Example |
|---|---|---|
int() |
Integer | int("10") → 10 |
float() |
Float | float("3.5") → 3.5 |
str() |
String | str(10) → "10" |
bool() |
Boolean | bool(0) → False |
list() |
List | list("ab") → ['a', 'b'] |
Warning: Converting a non-numeric string
like int("abc") raises a ValueError — always ensure the value is
convertible before casting.
Code Example
num1 = input("Enter first number: ") num2 = input("Enter second number: ") # input() always returns a string, so we convert it to int total = int(num1) + int(num2) print("Sum:", total)
Interview Tip
Remember: Python's input() function always returns data as a string,
even if the user types a number. You must explicitly convert it using int() or
float() before performing arithmetic.
Quick Revision
| Concept | Key Point |
|---|---|
| Expression | Combination of values/operators that evaluates to a result. |
| Implicit Conversion | Done automatically by Python. |
| Explicit Conversion | Done manually using functions like int(). |
input() |
Always returns a string. |
Summary
Expressions are the combinations of values and operators that Python evaluates to produce a result. Type
conversion — whether implicit (automatic) or explicit (manual, using functions like int()
and str()) — ensures that values of different types can be safely combined and used
together in your programs.