HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Programming
  2. /Python Basics
  3. /Functions

Functions

Python Programming · Middle Stage (6–8) · 1 course, 14 worked problems, 50 practice questions

A function is a named block of code that does one job. You define it with def, give it parameters for its inputs, and send back a result with return. For example, def kinetic_energy(m, v): can return 0.5 * m * v ** 2. Once it is written, a function can be called many times with different values, so you do not repeat code. Variables made inside a function are local, and they vanish when the function ends. Short functions with clear names make a program easier to read, test and fix.

With practice you will be able to

  • Define a function with parameters and a return value
  • Call a function with different arguments and use the result
  • Tell local variables from global variables

On the syllabus of

  • CBSE / State Class 12
  • Python Certification
  • AP
  • IB
  • A-Level
  • IGCSE

Lessons

  • Functions: Name a RecipeWhat does return do that print cannot? Why does a variable vanish after a function?Middle Stage (6–8)

Practice questions

Choose an answer, then check it. Each one shows a full explanation.

A function looks for the first vowel of a word. It returns from inside its loop, or after it. def first_vowel(word): for ch in word: if ch in "aeiou": return ch return "-" print(first_vowel("rhythm"), first_vowel("strong")) What does the program print?

A function changes a string in a loop, but its result is not what the student expected. def stretch(s, n): for i in range(n): s = s + s[-1] result = stretch("ab", 2) print(result) What does the program print?

A function pads a word with a fill character up to a given width. It is defined like this, and then one call below is run. def pad(text, width, fill="*"): return text + fill * (width - len(text)) Which calls run with no error? Choose every correct option.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

A function prints the length of a tuple, but it has no return statement. The program prints what the call gives back. def show(v): print(len(v), end=" ") result = show((1, 2, 3)) print(result) What does the program print?

A function writes into a dictionary that it is given. It has no return statement. def register(book, name): book[name] = len(book) + 1 book = {} register(book, "Ann") register(book, "Ben") register(book, "Ann") print(book) What does the program print?

A function should give back a list without the first x in it, and leave the list it was given as it was. Its first line is missing, marked ____. def without(v, x): ____ out.remove(x) return out data = [1, 2, 1] print(without(data, 1), data) Which line fills the gap so that the output is exactly [2, 1] [1, 2, 1]?

Two short functions are defined. One gives back a slice, and the other adds an item to a list. def head(v, n=2): return v[:n] def bump(v): v.append(len(v)) return len(v) Which of these comparisons are True? Choose two.

One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.

A program calls a function on its first line. The def statement of that function comes after the call. print(double(4)) def double(n): return n * 2 What happens when the program runs?

A function uses a local variable named sum, the same name as a built-in function. After the call, the program uses the built-in sum(). def total(values): sum = 0 for v in values: sum += v return sum print(total([1, 2, 3]), sum([4, 5])) What happens when the program runs?

The def of price uses the name rate, which gets its first value only on a later line. Between two calls, the program changes rate. def price(qty): return qty * rate rate = 3 a = price(2) rate = 5 print(a, price(2)) What does the program print?

All 50 practice questions on Functions →

Worked problems

Each one has a hint, the key idea and a full worked solution.

  • Which of these four programs shows something? Say it before you run each one.Easy
  • This program should show The total is 50. It stops with an error. Find the fault and fix it.Medium
  • Same name, two variablesMedium
  • A function ticket_price(age) gives the price of a museum ticket: under 5 is free (0), under 13 pays…Medium
  • Write a function say_hello that shows Hello!. Call it three times.Easy
  • Write a function greet(name) that shows Hello, followed by the name and an exclamation mark…Easy
  • Write a function area(length, width) that returns the area. What are area(5, 3) and area(12.5, 4)?Easy
  • One function shows its answer with print…Hard
  • Write a function is_even(n) that returns True if n is even, and False if it is odd…Medium
  • Write a function bigger(a, b) that returns the bigger of two numbers, without using max…Medium
  • Write a function c_to_f(celsius) with F = C × 9/5 + 32. What does it return for 37, 100 and -40?Medium
  • Write a function power(base, exponent=2). What are power(5), power(2, 10) and power(10, 3)?Medium

All 14 worked problems on Functions →

Python labs

  • Say it with a function
  • The area function
  • Yes or no functions
  • Return, do not reach out
  • Test the grade function
  • Project: your toolbox

Related topics

  • Python Basics
  • Getting Started with Python
  • Variables and Data Types
  • Operators and Expressions
  • Input and Output
  • Strings
  • Conditionals: if, elif, else
  • Loops
  • Lists
  • Mini Projects