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. /Variables and Data Types

Variables and Data Types

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

A variable is a name that holds a value, like a labelled box. In Python you make one with =, for example mass = 2.5. Each value has a type. Whole numbers are int, numbers with a decimal point are float, text is str, and True or False is bool. The type decides what you can do with a value. You can add two numbers, but adding a number to a string gives an error. The function type() tells you the type, and int(), float() and str() change one type to another. Clear names such as speed make code easy to read.

With practice you will be able to

  • Create variables of type int, float, str and bool
  • Find the type of a value with type()
  • Convert a string to a number and a number to a string

On the syllabus of

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

Lessons

  • Boxes with Names: VariablesWhy is 12 = age an error? Are score and Score the same variable?Middle Stage (6–8)

Practice questions

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

A program converts values with int(), str() and float() before it uses them. print(int("7") + int(7.9), str(3) + str(4.0), float("2") * 2) What does the program print?

Half of seven is turned into a whole number in two ways: by rounding it and by converting it with int(), for the positive and the negative value. a = 7 / 2 print(a, round(a), int(-a), round(-a)) What is the output?

A program holds a number as text, and multiplies it in two ways. a = "7" b = 2 print(int(a) * b, a * b) What does the program print?

A student turns values into numbers with int() and float(). Each line below is tried on its own. Which lines run without an error? Choose three.

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.

Two names get new values in one assignment, and the new value of b uses a. a, b = 1, 2 a, b = b, a + b print(a, b) What does the program print?

Whole numbers can be written in binary, octal or hexadecimal, and with underscores. A student checks five statements about such literals. Which of these expressions are True? 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 program turns four floats into whole numbers, three with round() and one with int(). print(round(2.5), round(3.5), round(-1.5), int(-1.7)) What does the program print?

A program compares floating point results with ==. x = 0.1 + 0.2 print(x == 0.3, round(x, 2) == 0.3, 3 * 0.1 == 0.3) What does the program print?

A student predicts the type of the result of some expressions. Which of the type tests below are True? 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.

Four assignments are offered. Each one is tried alone, as a complete program. Which assignment is accepted by Python?

All 31 practice questions on Variables and Data Types →

Worked problems

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

  • Trace the programMedium
  • A student wants to swap a and b, and writes…Easy
  • Say what the type of each result is, and what value it has. Decide on paper first.Medium
  • Each of these three programs stops with an error…Easy
  • Store your name, your age and your city in three variables…Easy
  • Which of these are valid variable names? …Easy
  • Give the type (int, float, str or bool) of each value: 7, 7.0, "7", True, 3 > 2.Easy
  • Follow the valuesMedium
  • A game score starts at 0. The player gets 10 points, then 5 points, and then the score is doubled…Easy
  • a = 3 and b = 8. Write code that swaps them, so that a is 8 and b is 3.Medium
  • age_text = "12" and age_number = 12. What is the difference? …Medium
  • What do these lines show? print(5 > 3), print(2 == 3), print("a" == "a"), print(type(5 > 3))…Medium

All 14 worked problems on Variables and Data Types →

Python labs

  • Make your first variables
  • Update the score
  • Swap without losing
  • Right type, right result
  • Fix the names
  • Project: the shop bill

Related topics

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