Introduction to Python
What is Python? Python is a high-level, general-purpose, interpreted programming language created by Guido van Rossum in 1991. It is known for its simplicity, readability, and versatility.
Key Features of Python:
- Interpreted: Python code is executed line by line, without requiring compilation.
- Object-oriented: Python supports object-oriented programming, where data and methods are organized into objects.
- Dynamically typed: Python doesn't require explicit type declarations for variables; types are assigned automatically.
- Extensible: Python allows extending its functionality with modules and packages.
- Cross-platform: Python code can run on multiple operating systems such as Windows, Mac, and Linux.
Getting Started with Python:
- Install Python: Visit the Python website (https://www.python.org/) and download the latest version.
- Open a Python Interpreter: In your terminal or command window, type
python
to open the Python interactive interpreter. - Write Python Code: Enter Python code into the interpreter, and press Enter to execute it. For example:
print("Hello, world!")
Basic Syntax:
- Indentation is significant in Python. Statements within blocks (e.g., functions, loops) must be indented using whitespace.
- Variables in Python are not declared with a specific type. They can be assigned values using the assignment operator (=).
- Comments start with a hash sign (#) and are used to add annotations or clarify code.
Examples:
# Calculate the area of a circle with radius 5
radius = 5
area = math.pi * radius ** 2
print(area)
# Loop over a list of numbers and print each one
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
Advantages of Python:
- Easy to learn and use
- Versatile and can be used for various tasks
- Strong community support and extensive libraries
- Cross-platform and portable
Applications of Python:
- Web development
- Data science and machine learning
- Scripting and automation
- Game development
- Artificial intelligence