Environment Setup
Download the latest installer from the official Python website. During installation on Windows, ensure the option to add Python to the system PATH is checked. This allows execution from any command prompt. Verify the setup by running:
py --versionOn macOS, Python 3 typically requires invoking python3 explicitly to avoid conflicts with the pre-installed Python 2. Verify via:
python3 -VThe interactive REPL (Read-Eval-Print Loop) is accessed by typing python or python3 in the terminal. It immediately evaluates expressions. Exit the shell using quit().
Development Tools
PyCharm Community Edition provides a robust, free IDE tailored for Python. Visual Studio Code requires the official Microsoft Python extension to execute scripts. For data-driven tasks, JupyterLab offers a browser-based interactive environment. Install it via the package manager:
pip install jupyterlabLaunch the interface from the terminal within your project directory:
jupyter labCore Syntax
Output to the console is handled by the built-in print() function. Variables act as labeled containers for data, eliminating the need to hardcode repetitive values.
greeting_text = "HelloWorld"
print(greeting_text)Variable names must begin with a letter or underscore, avoiding spaces and leading digits.
Python categorizes data into several fundamental types: Strings (str), Integers (int), Floating-point numbers (float), Booleans (bool representing True or False), and the null value (NoneType representing None).
Comments and Operators
Single-line comments are prefixed with #, while multi-line blocks are enclosed in triple quotes """ ... """.
Arithmetic operations include addition (+), subtraction (-), multiplication (*), true division (/), floor division (//), modulo (%), and exponentiation (**).
result = 5 ** 2 # 25
remainder = 10 % 3 # 1Comparison operators (==, !=, >, <, >=, <=) evaluate conditions and yield Boolean outcomes. Logical operators (and, or, not) combine or invert these Boolean expressions.
Control Flow
Conditional execution relies on indentation to define code blocks, utilizing if, elif, and else.
score = 85
if score >= 90:
rating = "Excellent"
elif score >= 80:
rating = "Good"
else:
rating = "Average"
print(rating)The for loop iterates sequentially over elements within a string, list, or other iterable object.
for character in "Python":
print(character)Data Structures
Lists store ordered sequences of items enclosed in square brackets [], accessible by zero-based indexing. They support dynamic modifications like appending or removing elements.
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits[0]) # Outputs: appleDictionaries map unique keys to corresponding values, wrapped in curly braces {}. Keys serve as identifiers to retrieve the stored data.
config = {"host": "localhost", "port": 8080}
print(config["host"]) # Outputs: localhostFunctions and Modules
Reusable code blocks are defined as functions using the def keyword, optionally accepting parameters.
def calculate_area(width, height):
return width * height
area = calculate_area(5, 10)
print(area)External functionality is incorporated through modules using the import keyword, extending the capabilities of a script without writing custom logic.
import random
print(random.randrange(1, 10))