Skip to content

Class 11 · Computer Science · Computer Science

Chapter 5: Getting Started with Python Notes

The big idea

Python is a free, interpreted, case-sensitive high-level language; this chapter covers its modes, identifiers, data types, operators, input and output, type conversion and the three kinds of errors.

Things to remember

  • Python is open source, interpreted (statements are translated and run one by one), portable, case-sensitive and uses indentation for blocks. Interactive mode runs one statement at the prompt; script mode runs a saved file with the .py extension.
  • Keywords are reserved words like if, while and True. An identifier must begin with a letter or underscore, can contain letters, digits and underscore, cannot be a keyword and cannot contain symbols like @. A variable is created when a value is first assigned.
  • Data types: int, float, complex, bool (True, False), string, list [ ], tuple ( ), set { } without duplicates, None, and dictionary of key:value pairs. Mutable types (list, set, dictionary) can change; immutable ones (numbers, string, tuple) cannot. Comments start with #.
  • Arithmetic operators: + - * / % // and the power operator written with two stars. For example 17 // 5 is 3, 17 % 5 is 2, 3 raised to the power 4 is 81, and 'India' * 2 gives IndiaIndia. Relational operators compare; logical operators are and, or, not; is and in test identity and membership.
  • Precedence from highest: the power operator, unary minus, * / % //, + -, relational, then not, and, or. Equal precedence goes left to right and brackets override it. So 20 + 30 * 40 is 1220 but (20 + 30) * 40 is 2000.
  • input() always returns a string, so entering 2 and printing the value times 2 gives 22. Use int() or float() to convert. print() takes sep and end options; commas add spaces between values, while + joins only strings.
  • Explicit conversion (type casting) uses int(), float(), str(); int(20.67) gives 20 and loses the fraction. Implicit conversion happens automatically, so 10 + 20.0 becomes the float 30.0. Writing a string plus an integer gives an error; use str().
  • Debugging removes errors. Syntax errors break the language rules, like a missing bracket, and stop execution. Logical errors give wrong output, such as 10 + 12 / 2 giving 16 instead of 11. Runtime errors occur during running, such as division by zero.

Words to know

Tap a card to see what it means

Quick quiz

⭐ 0 / 5
  1. What does the Python statement print(17 // 5, 17 % 5) display?

  2. A program uses x = input() and the user types 2. What does print(x * 2) display?

  3. A student writes 10 + 12 / 2 to find the average of 10 and 12. The program runs and prints 16. What type of error is this?

  4. Assertion (A): print('Total Rs.' + 70) gives an error. Reason (R): Python does not implicitly convert an integer to a string for the + operator. Which is correct?

  5. Which of the following is a valid Python identifier?

Match the pairs

Match each operator or function with its result.

Try this at home

Open an online Python shell with an adult. Try print(10 + 20), print('10' + '20') and print(2 * 5) and compare the outputs with your guesses. Write down what surprised you.

All Computer Science notesChapter page and exercisesComputer Science syllabus