Introduction to Python
Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum and first released in 1991. It emphasizes code readability with its notable use of significant whitespace.
History of Python
- 1989: Python development started by Guido van Rossum
- 1991: Python 0.9.0 released
- 2000: Python 2.0 released
- 2008: Python 3.0 released
- 2020: Python 2.7 end-of-life
First Python Program
Python Features
Python has several distinctive features that make it popular for various applications.
Key Features of Python
- Easy to Learn: Simple syntax similar to English
- Interpreted Language: Code executed line by line
- Cross-platform: Runs on Windows, Linux, Mac OS
- Object-Oriented: Supports OOP concepts
- Dynamic Typing: No need to declare variable types
- Large Standard Library: Rich set of modules and packages
- Open Source: Free to use and distribute
- GUI Programming: Supports GUI application development
Python Data Types
Python has various built-in data types that are used to store different kinds of data.
Basic Data Types
| Data Type | Description | Example |
|---|---|---|
| int | Integer numbers | 5, -10, 1000 |
| float | Floating point numbers | 3.14, -2.5, 10.0 |
| str | String of characters | "Hello", 'Python' |
| bool | Boolean values | True, False |
| complex | Complex numbers | 3+4j, 2-5j |
Sequence Data Types
| Data Type | Description | Example |
|---|---|---|
| list | Ordered, mutable collection | [1, 2, 3], ['a', 'b', 'c'] |
| tuple | Ordered, immutable collection | (1, 2, 3), ('a', 'b', 'c') |
| range | Sequence of numbers | range(5), range(1, 10) |
Variables and Operators
Variables are used to store data values. Python has no command for declaring variables - they are created when you assign a value.
Variable Naming Rules
- Must start with a letter or underscore
- Cannot start with a number
- Can only contain alpha-numeric characters and underscores
- Case-sensitive (age, Age, and AGE are different variables)
Python Operators
- Arithmetic Operators: +, -, *, /, %, **, //
- Comparison Operators: ==, !=, >, <,>=, <=
- Logical Operators: and, or, not
- Assignment Operators: =, +=, -=, *=, /=
- Identity Operators: is, is not
- Membership Operators: in, not in
Control Statements
Control statements determine the flow of execution in a Python program.
Conditional Statements
- if statement: Executes block if condition is true
- if-else statement: Executes one block if true, another if false
- if-elif-else statement: Multiple conditions checking
Looping Statements
- for loop: Iterates over a sequence
- while loop: Executes while condition is true
Loop Control Statements
- break: Exits the loop
- continue: Skips the current iteration
- pass: Does nothing (placeholder)
Functions in Python
A function is a block of code that performs a specific task and can be reused.
Function Definition and Calling
Function with Return Value
Types of Function Arguments
- Positional arguments: Passed in correct positional order
- Keyword arguments: Passed with parameter names
- Default arguments: Parameters with default values
- Variable-length arguments: *args and **kwargs
Python Data Structures
Python provides several built-in data structures to store and organize data efficiently.
Lists
Tuples
Dictionaries
Sets
Object-Oriented Programming in Python
Python supports object-oriented programming (OOP) concepts like classes, objects, inheritance, polymorphism, and encapsulation.
Class and Object
Inheritance
Encapsulation
File Handling in Python
Python provides built-in functions for creating, reading, updating, and deleting files.
File Operations
File Modes
| Mode | Description |
|---|---|
| "r" | Read (default) |
| "w" | Write (creates new file or truncates existing) |
| "a" | Append |
| "r+" | Read and write |
| "b" | Binary mode |
Exception Handling
Exception handling allows programs to handle runtime errors gracefully.
Try-Except Block
Multiple Exceptions
Finally Block
Modules and Packages
Modules are Python files containing functions, classes, and variables. Packages are collections of modules.
Creating and Using Modules
Standard Library Modules
- math: Mathematical functions
- datetime: Date and time operations
- os: Operating system interfaces
- sys: System-specific parameters
- random: Generate random numbers
- json: JSON encoding and decoding
Popular Python Libraries
Python has a rich ecosystem of third-party libraries for various applications.
Data Science Libraries
- NumPy: Numerical computing
- Pandas: Data manipulation and analysis
- Matplotlib: Data visualization
- Scikit-learn: Machine learning
Web Development Libraries
- Django: High-level web framework
- Flask: Micro web framework
- Requests: HTTP library
Other Important Libraries
- Tkinter: GUI programming
- OpenCV: Computer vision
- BeautifulSoup: Web scraping
- PyGame: Game development