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

Python Basics

Python Programming · Middle Stage (6–8) · 13 courses, 178 worked problems, 429 practice questions

Python is a programming language with clear, readable code, used in science, data work and the web. This area starts from the very first program and builds the core skills. It begins with getting started with Python, then variables and data types, operators and expressions, and input and output. Next come conditionals, which let a program choose, and loops, which repeat steps. Strings, lists and functions follow. Each idea is used in short programs about real things, such as a speed calculator or a list of marks, and mini projects bring the ideas together.

With practice you will be able to

  • Write and run a short Python program that does a calculation
  • Use if, for and while to control what a program does
  • Write a function that takes inputs and returns a result

On the syllabus of

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

Parts of this topic

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

Lessons

  • Three Mini ProjectsShould a quiz accept PaRiS for paris? How do you find the fault in your own program?Middle Stage (6–8)
  • Functions: Name a RecipeWhat does return do that print cannot? Why does a variable vanish after a function?Middle Stage (6–8)
  • Lists: Many Values in One VariableAfter b = a, why does changing b also change a?Middle Stage (6–8)
  • Repeat with for: LoopsWhy does range(1, 6) stop at 5? Can a loop add 1 to 100 for you?Middle Stage (6–8)
  • Repeat Until: while LoopsWhy does a loop sometimes never stop? How do you leave a loop early?Middle Stage (6–8)
  • If, Elif, Else: Making DecisionsWhy does if x = 5: give an error? Where do mistakes in a decision hide?Middle Stage (6–8)

All 13 courses on Python Basics →

Practice questions

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

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?

Two functions share a global variable. The first call is made before the variable exists, inside a try statement. def setup(): global level level = 1 def up(): global level level += 1 return level try: print(up()) except NameError: setup() print(up(), level) What happens when the program runs?

A function deletes an item of a list by its index, and returns the new length. It is called twice, and each result is printed on the same line. def remove_at(items, i): del items[i] return len(items) data = [10, 20, 30] print(remove_at(data, -1), end=" ") print(remove_at(data, 2), end=" ") print(data) What happens when the program runs?

A function builds a list from one column of a table that is a list of lists. Then the program changes both the new list and the table. def column(grid, k): return [row[k] for row in grid] g = [[1, 2, 3], [4, 5, 6]] c = column(g, 1) c[0] = 99 g[1][1] = 0 print(c, g[0][1]) What does the program print?

A function builds the initials of a name, which has two spaces after its first word. Then the program splits a text on one space. def initials(name): out = "" for word in name.split(): out += word[0].upper() return out print(initials("ada king lovelace"), len("ada king".split(" "))) What happens when the program runs?

A function sorts the list it is given, and then returns a slice of it. The caller changes the slice. def top_two(scores): scores.sort(reverse=True) return scores[:2] marks = [55, 90, 72, 81] best = top_two(marks) best.append(0) print(marks[0], len(marks), best) What does the program print?

Some list methods change the list and give back None. A function such as sorted() gives back a new list instead. nums = [3, 1, 2] a = nums.sort() b = sorted(nums, reverse=True) c = nums.append(4) Which of these expressions are True after the code has run? 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.

In the middle of a for loop, the function tally puts 10 into i. It returns a sum and i together. def tally(n): acc = 0 for i in range(n): if i == 2: i = 10 acc += i return acc, i print(tally(5)) What does the program print?

All 429 practice questions on Python Basics →

Worked problems

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

  • The secret number is 42, the limit is 7 tries, and the player types 50, 30, 36, 42…Medium
  • A quiz has 5 questions but only 4 answers…Easy
  • The marks are [72, 85, 64, 90, 58, 91, 60, 74]…Medium
  • This program should show the average of the even numbers in the list…Medium
  • 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
  • The list is days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]…Easy
  • Find the positionEasy
  • The list is marks = [45, 78, 90, 62, 38, 85, 71]…Easy
  • What do a and b show at the end? Say it before you run it.Medium

All 178 worked problems on Python Basics →

Python labs

  • Test the judge function
  • Quiz marker
  • Grade book report
  • Fix the program
  • Say it with a function
  • The area function