Introduction to Programming in Python
Python is a high-level, interpreted programming language known for its simplicity and readability. It is widely used in web development, data science, artificial intelligence, automation, and more. Python’s extensive standard library and active community make it a great choice for beginners and experienced developers alike.
This guide will introduce you to the fundamentals of Python programming, helping you write clean, efficient, and maintainable code.
Table of Contents
- What is Python?
- Setting Up Your Environment
- Variables and Data Types
- Control Flow
- Functions
- Lists and Dictionaries
- Object-Oriented Programming
- Error Handling
- File I/O
- Modules and Packages
What is Python?
Python is a versatile programming language that emphasizes code readability and simplicity. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
# Example: Your first Python program
print("Hello, Python Programming!")
Setting Up Your Environment
To write and run Python programs, you need to install Python on your system. Follow the instructions on the official Python website.
# Example: Running a Python script
python main.py
Variables and Data Types
Python is dynamically typed, meaning you don’t need to declare variable types explicitly. Common data types include integers, floats, strings, and booleans.
# Example: Declaring variables
age = 25
height = 5.9
name = "Alice"
is_student = True
print(f"Name: {name}, Age: {age}, Height: {height}")
Control Flow
Python supports if
, else
, elif
, and loops (for
, while
) for controlling program flow.
# Example: Conditional statement
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Functions
Functions in Python are defined using the def
keyword. They can return values or perform actions.
# Example: Function
def add(a, b):
return a + b
result = add(5, 10)
print(f"Result: {result}") # 15
Lists and Dictionaries
Lists are ordered collections, while dictionaries are collections of key-value pairs. Both are commonly used in Python.
# Example: List
fruits = ["Apple", "Banana", "Orange"]
for fruit in fruits:
print(fruit)
# Example: Dictionary
person = {"name": "Alice", "age": 25}
print(f"Name: {person['name']}, Age: {person['age']}")
Object-Oriented Programming
Python supports object-oriented programming (OOP) with classes, objects, inheritance, and more.
# Example: Class and Object
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def display(self):
print(f"Name: {self.name}, Age: {self.age}")
person = Person("Alice", 25)
person.display()
Error Handling
Python uses try
, except
, and finally
blocks for error handling, allowing you to handle exceptions gracefully.
# Example: Error handling
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
finally:
print("Execution complete.")
File I/O
Python provides simple and powerful tools for reading from and writing to files.
# Example: Reading from a file
with open("example.txt", "r") as file:
for line in file:
print(line)
# Example: Writing to a file
with open("output.txt", "w") as file:
file.write("Hello, Python!")
Modules and Packages
Python’s modular design allows you to organize code into reusable modules and packages.
# Example: Importing a module
import math
result = math.sqrt(16)
print(f"Square root: {result}") # 4.0