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.
1# Example: Your first Python program
2print("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.
1# Example: Running a Python script
2python 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.
1# Example: Declaring variables
2age = 25
3height = 5.9
4name = "Alice"
5is_student = True
6
7print(f"Name: {name}, Age: {age}, Height: {height}")
Control Flow
Python supports if
, else
, elif
, and loops (for
, while
) for controlling program flow.
1# Example: Conditional statement
2age = 18
3
4if age >= 18:
5 print("You are an adult.")
6else:
7 print("You are a minor.")
Functions
Functions in Python are defined using the def
keyword. They can return values or perform actions.
1# Example: Function
2def add(a, b):
3 return a + b
4
5result = add(5, 10)
6print(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.
1# Example: List
2fruits = ["Apple", "Banana", "Orange"]
3for fruit in fruits:
4 print(fruit)
5
6# Example: Dictionary
7person = {"name": "Alice", "age": 25}
8print(f"Name: {person['name']}, Age: {person['age']}")
Object-Oriented Programming
Python supports object-oriented programming (OOP) with classes, objects, inheritance, and more.
1# Example: Class and Object
2class Person:
3 def __init__(self, name, age):
4 self.name = name
5 self.age = age
6
7 def display(self):
8 print(f"Name: {self.name}, Age: {self.age}")
9
10person = Person("Alice", 25)
11person.display()
Error Handling
Python uses try
, except
, and finally
blocks for error handling, allowing you to handle exceptions gracefully.
1# Example: Error handling
2try:
3 result = 10 / 0
4except ZeroDivisionError as e:
5 print(f"Error: {e}")
6finally:
7 print("Execution complete.")
File I/O
Python provides simple and powerful tools for reading from and writing to files.
1# Example: Reading from a file
2with open("example.txt", "r") as file:
3 for line in file:
4 print(line)
5
6# Example: Writing to a file
7with open("output.txt", "w") as file:
8 file.write("Hello, Python!")
Modules and Packages
Python's modular design allows you to organize code into reusable modules and packages.
1# Example: Importing a module
2import math
3
4result = math.sqrt(16)
5print(f"Square root: {result}") # 4.0