Skip to main content

Class 8 (Grade 8) ยท Computer Science

Python Programming Basics: Class 8 Computer Science

The first steps in Python: print and comments, variables, data types, arithmetic, input, casting, simple if statements, and how to read an error message. Every example was run and the output is real.

  • Lesson
  • 6 quizzes
  • Worksheet PDF

A step-by-step lesson, 6 self-marking quizzes and a printable worksheet, all on Python Programming Basics for Class 8.

Learn it: the lesson

Python Programming Basics8 steps ยท one idea at a time ยท quick check at the endStart the lesson โ†’
What the lesson covers8 steps ยท tap to see
  1. 1Your first Python program

    Python is a text-based programming language that is popular with beginners and professionals alike.

    Learn this in the lesson โ†’
  2. 2Variables

    A variable is a named place in the computerโ€™s memory that holds a value.

    Learn this in the lesson โ†’
  3. 3Data types

    Every value in Python has a data type, which says what kind of thing it is.

    Learn this in the lesson โ†’
  4. 4Arithmetic operators

    Python can do sums. These are the operators you need.

    Learn this in the lesson โ†’
  5. 5Getting input

    The input() function pauses the program and waits for the user to type something.

    Learn this in the lesson โ†’
  6. 6Casting: changing the type

    To turn one type into another, we use casting.

    Learn this in the lesson โ†’
  7. 7Making decisions with if

    A program can choose what to do.

    Learn this in the lesson โ†’
  8. 8Reading error messages

    Every programmer gets errors, all the time.

    Learn this in the lesson โ†’
  9. Quick check: see what you remember

Python Programming Basics: Class 8 Computer Science notes

Your first Python program

Python is a text-based programming language that is popular with beginners and professionals alike. A program is a list of instructions. Python reads them from the top, one line at a time, and does each one in order.

The first instruction most people learn is print(). It shows something on the screen.

# My first programprint("Hello, world!")print("Python is fun")

Hello, world!Python is fun

Text goes inside quotation marks. A line that starts with # is a comment. Python skips it completely. Comments are notes for people, to explain what the code is for.

Maths needs no quotes: print(2 + 3) shows 5. But print("2 + 3") shows exactly 2 + 3, because it is text.

Variables

A variable is a named place in the computer’s memory that holds a value. You create one with the equals sign. The name goes on the left and the value on the right.

score = 10score = score + 5print(score)

This prints 15. In the second line, Python works out the right side first (10 + 5) and then stores the answer back in score, replacing the old value.

  • Names can use letters, digits and underscores, but cannot start with a digit or contain spaces.
  • Capital letters matter: Score and score are two different variables.
  • Choose meaningful names such as lives or total_price, rather than a bare x.

Key facts

  • print() shows output. # starts a comment that Python ignores.
  • A variable stores a value: name = value. = stores, == compares.
  • The basic types are int, float, str and bool. Operators: + - * / // % **.
  • input() always gives text. Cast it with int() or float() to do maths.
  • if, elif and else choose a path. Mind the colon and the indentation.
  • Read errors from the last line up: the type, the reason, then the line number.

Python Programming Basics MCQs with answers

Tap an answer to check it.

  1. Which Python function shows text on the screen?

    Answer: B. print() In Python, print() displays text or values on the screen, while input() reads what the user types.

  2. Which of these is a Boolean value?

    Answer: C. True True, written without quotes and with a capital T, is a Python Boolean value; in quotes it would be a string.

  3. What does print(17 // 5) show?

    Answer: A. 3 The // operator gives whole-number division, and 5 goes into 17 three times (15), so the result is 3.

  4. Line 1: price = 2.5. Line 2: qty = 4. Line 3: print(price * qty). What is printed?

    Answer: D. 10.0 Multiplying 2.5 by 4 gives 10, and because price is a float the result prints as 10.0.

More questions in the 6 quizzes below โ†“ ยท All Class 8 Computer Science MCQs with answers โ†’

Practise real quizzes on this topic: 6 quizzes

Each quiz marks itself the moment it is finished and explains every answer. Start easy and work up.

๐Ÿ–จ๏ธPython Programming BasicsShort Python snippets to read and predict. Pupils work out the output of print, variables, //, % and **, name data types, cast text to numbers, trace if and else, read a TypeError message and write a two line input program. Fifteen questions.See the worksheet โ†’

โฌ‡ Download free PDFFree sample copy with a light QuizByt mark. A free account gives you more.