Functions & Files · Unit 4

Built-in Functions

Conversion, math and string functions

Unit 4 · Python’s ready-made toolkit

Choose a small tool. Understand its contract.

A built-in function is available without importing a module. It accepts inputs (arguments), follows a defined rule, and returns a result. String operations such as strip() are methods called on a string; len() is a built-in function that accepts a string. Select an entry to load an editable example into the live runner.

Convert carefully

Input is text. Convert only after deciding which values are valid.

Aggregate honestly

sum(), min(), max(), and len() answer different questions.

Strings do not mutate

Methods return a new string. Save the result to keep the transformation.

Function explorer

Browse all 45 names listed in the course plan: 12 conversion, 7 numeric, and 26 string operations.

Conversion · built-in function

int("42")

Convert a valid numeric value to an integer.

Expected result

42 · int

Remember: int(3.9) truncates toward zero; it does not round.

Try int()
Starting up Python…
value = int("42")
print(value)
print(type(value).__name__)
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Worked patterns

Combine tools to answer a real question

Change the data and predict the output before running each example.

Validate, then convert

Conversion may fail for invalid text. Production code should validate or handle ValueError.

Conversion worked example
Starting up Python…
raw_marks = "91"
marks = int(raw_marks)
print(marks + 4)
print(type(marks).__name__)
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Summarize class marks

sum() gives a total, len() gives a count, and round() creates a useful display value.

Numbers worked example
Starting up Python…
marks = [82, 91, 76]
average = sum(marks) / len(marks)
print("Total:", sum(marks))
print("Highest:", max(marks))
print("Average:", round(average, 1))
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Normalize a name

strip() removes edge spaces, title() changes capitalization, and startswith() checks the cleaned result.

Strings worked example
Starting up Python…
student = "  mira patel  "
clean_name = student.strip().title()
print(clean_name)
print("Length:", len(clean_name))
print("Starts with Mira:", clean_name.startswith("Mira"))
The editor is ready while Python loads in the background.

Output

Your program's output will appear here.
Common Python mistakes

NameError: name 'mark' is not defined

NameError: A name was used before Python could find a variable or function with that name.

IndexError: list index out of range

IndexError: The requested position is outside the items in this list or sequence.

KeyError: 'email'

KeyError: That key does not exist in this dictionary.

TypeError: can only concatenate str (not "int") to str

TypeError: This operation does not work with the kind of value it received.

ZeroDivisionError: division by zero

ZeroDivisionError: A number cannot be divided by zero.

SyntaxError: invalid syntax

SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.

IndentationError: expected an indented block

IndentationError: The spaces at the start of a line do not match Python's block structure.

Common mistakes and good habits

TypeError

sum(["82", "91"])

These are strings, not numbers. Convert validated numeric text before arithmetic.

ValueError

int("nine")

Python cannot parse this as an integer. Validate input and explain the expected format.

Result not saved

name.strip() # result ignored

Strings are immutable. Assign the result: name = name.strip().

Boundary surprise

"room 2".isalpha()

False: isalpha requires every character to be a letter.

Check your understanding

1. What does int(3.9) produce?

2. Which expression counts items in marks?

3. What is " Mira ".strip()?

4. What does "banana".find("z") return?

Choose functions by asking three questions

What type is the data now? What result do I need? What should happen for empty or invalid data? For example, guard an average against an empty list before dividing by len(marks). Knowing edge cases is part of using a function well.