CBSETest.comby Bimal Publications

Need help with Getting Started with Python?

Practice Tests
Class 11 · Computer Science NCERT Class 11 Computer Science · Ch. 55 min read · 15 questions

Getting Started with Python

Computer Science

Getting Started with Python

Python is a high-level, interpreted, interactive, and object-oriented programming language. Created by Guido van Rossum and first released in 1991, Python is celebrated for its clean, readable syntax that resembles plain English. It is widely used in web development, data science, AI, automation, and education.

Why Python?

  • Simple and readable syntax reduces the cost of program maintenance.
  • Interpreted language — code runs line by line without a separate compilation step.
  • Dynamically typed — variable types are determined at runtime.
  • Portable — the same code runs on Windows, Linux, and macOS.
  • Rich standard library and a huge ecosystem of third-party packages (NumPy, Pandas, Django).

Python Basics

  • Interactive Mode vs Script Mode:
  • · Interactive Mode: · Type Python commands directly in the Python shell (REPL); get immediate results. Great for experimenting.
  • · Script Mode: · Write code in a .py file and run the file; suitable for complete programs.

print() function: Displays output.
print("Hello, World!")

input() function: Reads user input as a string.
name = input("Enter your name: ")

Variables and Data Types

A variable is a name that refers to a memory location storing a value. Python infers the type automatically.

| Data Type | Example | Description |
|-----------|---------|-------------|
| int | 42, -7 | Whole numbers |
| float | 3.14, -0.5 | Decimal numbers |
| bool | True, False | Boolean values |
| str | "Hello" | Text / string |
| NoneType | None | Represents absence of value |

Type checking: use type() function. e.g., type(3.14) returns float.

Type conversion (casting): int("5") converts string "5" to integer 5. float(3) gives 3.0. str(42) gives "42".

Operators in Python

  • Arithmetic: + - · / // % · · (floor division, modulo, exponentiation)
  • Comparison: == ≠ < > ≤ ≥ (return True or False)
  • Logical: and, or, not
  • Assignment: = += -= · = /=
  • Membership: in, not in (test if value is in a sequence)

Important: / gives float division; // gives integer (floor) division.
Example: 7/2 = 3.5, but 7//2 = 3.

Strings in Python

  • Concatenation: "Hello" + " " + "World" gives "Hello World"
  • Repetition: "ha" · 3 gives "hahaha"
  • Indexing: s[0] gives first character; s[-1] gives last character.
  • Slicing: s[1:4] gives characters at indices 1, 2, 3.

Comments

  • Single-line: # This is a comment
  • Multi-line: use triple quotes or consecutive # lines.

Worked Examples

Example 1

Write a Python statement to display "Welcome to Python".
print("Welcome to Python")

Example 2

Store the value 25 in a variable called age and display it.
age = 25
print(age)
Python automatically knows age is an int.

Example 3

What is the output of 10 // 3 and 10 % 3?
10 // 3 = 3 (floor division — quotient without decimal)
10 % 3 = 1 (modulo — remainder after dividing)

Example 4

Convert the string "123" to an integer and add 7.
result = int("123") + 7
print(result) # Output: 130

Example 5

Demonstrate string slicing. If s = "PYTHON", what is s[1:4]?
s = "PYTHON"
s[1:4] gives characters at positions 1, 2, 3 = "YTH"
(Slicing is from start index up to but NOT including end index.)

Example 6

Write code to take two numbers as input from the user and print their sum.
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Sum =", a + b)
Note: input() always returns a string, so we cast to float.

Example 7

What is the result of: True and False, True or False, not True?
True and False = False (both must be True for 'and' to return True)
True or False = True (at least one must be True for 'or')
not True = False (not inverts the boolean)

Common mistakes

> A very common error: forgetting that input() always returns a string. If you need a number, you must convert with int() or float(). Also, Python is case-sensitive — Variable, variable, and VARIABLE are three different names. Finally, = is assignment (stores a value), while == is comparison (checks equality).

Summary

Python is an interpreted, high-level language with simple syntax. Variables are dynamically typed. Core data types include int, float, str, bool, and None. Operators handle arithmetic, comparison, logic, and assignment. Strings support indexing and slicing. Always convert input() output when expecting numbers. Python can be used in interactive mode for quick tests or script mode for complete programs.

Practice Problems

15 questions with instant feedback.

Question 1 of 15Score 0

Which function is used to display output in Python?