Python Definition
Python is a high-level, interpreted, general-purpose programming language. It emphasizes code
readability using simple and easy-to-understand syntax. Python supports multiple programming
paradigms including procedural, object-oriented, and functional programming.
Variables
Variables are used to store data in memory. In Python, variables do not need explicit declaration.
The type of variable is determined automatically based on the assigned value. Example: x = 10,
name = 'John'.
Data Types
Python provides several built-in data types: int (integer numbers), float (decimal numbers), str
(strings), bool (True/False), list (ordered mutable collection), tuple (ordered immutable collection),
set (unordered unique elements), and dictionary (key-value pairs).
Operators
Operators are symbols used to perform operations on variables and values. Types include:
Arithmetic (+, -, *, /, %), Relational (>, <, ==, !=), Logical (and, or, not), and Assignment (=, +=, -=).
They are essential for performing calculations and comparisons.
Control Flow Statements
Control flow statements control the execution of a program. Decision-making statements include if,
elif, and else. Looping statements include for and while loops. These help in repeating tasks and
making logical decisions in programs.
Functions
Functions are reusable blocks of code that perform a specific task. They help in modular
programming. A function is defined using the def keyword. Functions can take arguments and
return values.
List
A list is an ordered, mutable collection of elements. It allows duplicate values and supports indexing
and slicing. Lists are defined using square brackets []. Example: [1, 2, 3].
Tuple
A tuple is an ordered, immutable collection of elements. Once created, it cannot be modified.
Tuples are defined using parentheses (). They are faster than lists.
Set
A set is an unordered collection of unique elements. It does not allow duplicates and is useful for
mathematical operations like union and intersection.
Dictionary
A dictionary is a collection of key-value pairs. Keys must be unique and immutable. Dictionaries are
defined using curly braces {}. Example: {'name': 'John', 'age': 20}.
Strings
Strings are sequences of characters enclosed in single or double quotes. They support indexing,
slicing, and various built-in methods like upper(), lower(), and replace().
Object-Oriented Programming (OOP)
OOP is a programming paradigm based on classes and objects. A class is a blueprint for creating
objects. Key concepts include Encapsulation (data hiding), Inheritance (reusing code),
Polymorphism (multiple forms), and Abstraction (hiding implementation details).
Exception Handling
Exception handling is used to handle runtime errors and prevent program crashes. It is
implemented using try, except, else, and finally blocks.
File Handling
File handling allows reading from and writing to files. Python provides functions like open(), read(),
write(), and close(). Modes include read (r), write (w), and append (a).
Modules and Libraries
Modules are Python files containing functions and variables that can be reused. Libraries are
collections of modules that provide extensive functionality such as data analysis, machine learning,
and visualization.