1. What is Python?
Python is a high-level, interpreted, object-oriented programming language created by Guido van Rossum in 1991. It emphasizes code readability and allows programmers to express concepts in fewer lines of code than other languages like C++ or Java.
Python is widely used in web development, data science, artificial intelligence, machine learning, scientific computing, and education.
2. Key Features of Python
- Simple and Easy to Learn: Python has a clean syntax that reads like English.
- Interpreted: Code is executed line by line, which helps in debugging.
- Platform Independent: Python programs run on Windows, Linux, macOS, etc.
- Free and Open Source: Python can be used and distributed freely.
- High-Level Language: No need to manage memory or CPU details.
- Object-Oriented: Supports classes, objects, inheritance, and polymorphism.
- Extensive Library Support: Thousands of built-in modules for various tasks.
- Dynamic Typing: Variable types are determined at runtime.
- Large Community: Active community provides support and resources.
3. History of Python
| Year | Version | Major Changes |
|---|---|---|
| 1991 | Python 0.9.0 | First public release by Guido van Rossum |
| 2000 | Python 2.0 | List comprehensions, garbage collection |
| 2008 | Python 3.0 | Major revision, not backward-compatible with 2.x |
| 2020 | Python 3.9+ | Modern features, pattern matching, improved performance |
4. Installing Python
To install Python: (1) Visit python.org and download the latest version. (2) Run the installer. (3) Check "Add Python to PATH" during installation. (4) Verify installation by opening a terminal and typing python --version.
For CBSE Class 11, any Python 3.x version is suitable. Python 3.12+ is recommended.
5. Working Modes - Interactive and Script
Python can be used in two modes:
| Mode | Description | How to Start |
|---|---|---|
| Interactive Mode | Commands are executed immediately; useful for testing small code snippets | Type python in terminal or use IDLE shell |
| Script Mode | Write code in a file (.py) and execute the entire program | python filename.py |
6. Python IDLE
IDLE (Integrated Development and Learning Environment) is Python's built-in IDE. It provides:
- Interactive interpreter with syntax highlighting.
- File editor for writing scripts.
- Debugger for finding errors.
- Auto-completion and indentation support.
7. Your First Python Program
# Program to print Hello World
print("Hello, World!")
# Output: Hello, World!The print() function displays text on the screen. To run this program: Save it as hello.py and run python hello.py in the terminal.
9. Indentation in Python
Indentation is Python's way of grouping statements in blocks. Unlike other languages that use braces {}, Python uses whitespace (typically 4 spaces).
if 5 > 2:
print("Five is greater than two!") # Indented blockInconsistent indentation causes IndentationError. Always use consistent spacing.
10. Python Keywords
Keywords are reserved words that have special meaning in Python. There are 35 keywords in Python 3.12.
False, None, True, and, as, assert, async, await, break, class,
continue, def, del, elif, else, except, finally, for, from, global,
if, import, in, is, lambda, nonlocal, not, or, pass, raise,
return, try, while, with, yieldKeywords cannot be used as variable names, function names, or identifiers.
11. Built-in Functions
Python has many built-in functions available without importing any module.
| Function | Description | Example |
|---|---|---|
| print() | Prints output to console | print("Hello") |
| input() | Takes input from user | name = input("Enter name: ") |
| type() | Returns data type of a value | type(10) returns int |
| len() | Returns length of sequence | len("Python") returns 6 |
| int() | Converts to integer | int("10") returns 10 |
| float() | Converts to float | float("3.14") returns 3.14 |
| str() | Converts to string | str(100) returns "100" |
12. Common Errors in Python
| Error Type | Cause | Example |
|---|---|---|
| SyntaxError | Incorrect Python syntax | Missing colon after if statement |
| IndentationError | Incorrect indentation | Inconsistent spaces/tabs |
| NameError | Using undefined variable | print(x) when x is not defined |
| TypeError | Invalid operation for data type | "2" + 3 |
13. Best Practices
- Use 4 spaces for indentation (not tabs).
- Use meaningful variable names.
- Add comments to explain complex logic.
- Keep lines under 79 characters.
- Use lowercase with underscores for variable names (snake_case).
- Regularly save and test your code.
- Use the script mode for larger programs.
14. Revision Questions and Answers
Very Short Answer Questions
1. Who created Python?
Guido van Rossum.
2. What are the two working modes in Python?
Interactive mode and Script mode.
3. What is the extension of a Python file?
.py
4. Which symbol is used for single-line comments in Python?
# (hash/pound symbol).
5. Is Python an interpreted or compiled language?
Python is an interpreted language.
Short Answer Questions
1. Differentiate between interactive mode and script mode.
In interactive mode, commands are executed line by line immediately, suitable for testing small snippets. In script mode, code is written in a .py file and executed as a complete program, suitable for larger applications.
2. Explain the importance of indentation in Python.
Indentation defines blocks of code in Python. Unlike other languages that use braces, Python uses indentation to group statements. Consistent indentation (4 spaces) is required; otherwise, Python raises IndentationError.
Long Answer Questions
1. Describe any five key features of Python programming language.
(1) Simple and easy to learn with clean syntax. (2) Interpreted language that executes line by line. (3) Platform independent - runs on Windows, Linux, macOS. (4) Object-oriented with support for classes and inheritance. (5) Extensive standard library with modules for various tasks like file handling, networking, and data processing.
2. Write the steps to install Python and write your first program.
Steps: (1) Download Python from python.org. (2) Run installer and check "Add Python to PATH". (3) Verify installation with python --version. (4) Open a text editor and write: print("Hello, World!"). (5) Save as hello.py. (6) Run using: python hello.py. The output displays Hello, World! on the screen.
8. Comments in Python
Comments are used to explain code and are ignored by the interpreter.
Use comments to document your code, explain complex logic, and temporarily disable code.