By Nfon Andrew Tatah – CTO, Skye8 Company LTD
Python has rapidly become one of the most popular and versatile programming languages in the world due to its simplicity, readability, and wide range of applications from web development and automation to data science and AI. For developers at Skye8 and beyond, mastering Python fundamentals is critical to building reliable and scalable software solutions.
This comprehensive guide covers Python basics in depth, including syntax, data types, control flow, functions, object-oriented programming, file handling, package management, virtual environments, and project structuring.
1. Getting Started: Installing Python and Setting Up
Installing Python
-
Download the latest Python version from python.org.
-
Follow the installer steps for your OS (Windows, macOS, Linux).
-
Verify installation:
python --version
# or
python3 --version
Running Python
-
Interactive shell: Run
pythonorpython3in your terminal to open the REPL (Read-Eval-Print Loop). -
Script execution: Write code in a
.pyfile and run it using:
python my_script.py
2. Variables and Data Types
Python uses dynamic typing, so you don’t declare variable types explicitly.
# Variable assignment
name = "Andy" # String
age = 30 # Integer
height = 1.75 # Float
is_cto = True # Boolean
complex_num = 3 + 4j # Complex number
print(name, age, height, is_cto, complex_num)
Common Data Types
| Type | Description | Example |
|---|---|---|
int |
Integer numbers | 42 |
float |
Floating-point numbers | 3.14 |
str |
Text strings | "Hello, Skye8!" |
bool |
Boolean values | True, False |
list |
Ordered mutable collection | [1, 2, 3] |
tuple |
Ordered immutable collection | (1, 2, 3) |
set |
Unordered unique elements | {1, 2, 3} |
dict |
Key-value mappings | {"name": "Andy", "role": "CTO"} |
3. Strings and String Operations
Strings can be enclosed in single, double, or triple quotes.
greeting = "Hello"
name = 'Andy'
multiline = """This
is a
multiline string"""
# Concatenation
message = greeting + ", " + name + "!"
print(message) # Output: Hello, Andy!
# String formatting (f-strings)
age = 30
print(f"{name} is {age} years old.")
4. Collections: Lists, Tuples, Sets, Dictionaries
Lists (mutable)
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits[1]) # banana
Tuples (immutable)
coordinates = (10, 20)
print(coordinates[0]) # 10
Sets (unique unordered)
numbers = {1, 2, 2, 3}
print(numbers) # {1, 2, 3}
Dictionaries (key-value)
person = {"name": "Andy", "role": "CTO"}
print(person["name"]) # Andy
5. Control Flow: Conditionals and Loops
Conditionals
age = 20
if age >= 18:
print("Adult")
elif age > 12:
print("Teenager")
else:
print("Child")
For Loops
for fruit in fruits:
print(fruit)
While Loops
count = 0
while count < 3:
print(count)
count += 1
6. Functions
Functions are reusable blocks of code.
def greet(name):
return f"Hello, {name}!"
print(greet("Andy")) # Hello, Andy!
Function with default parameters
def greet(name="Guest"):
print(f"Welcome, {name}!")
greet() # Welcome, Guest!
greet("Andy") # Welcome, Andy!
Lambda functions (anonymous)
square = lambda x: x * x
print(square(5)) # 25
7. Object-Oriented Programming (OOP)
Defining a class and creating an object
class Employee:
def __init__(self, name, role):
self.name = name
self.role = role
def describe(self):
return f"{self.name} works as {self.role}"
employee = Employee("Andy", "CTO")
print(employee.describe())
Inheritance
class Manager(Employee):
def __init__(self, name, role, department):
super().__init__(name, role)
self.department = department
def describe(self):
return f"{self.name} manages {self.department} department"
manager = Manager("Alice", "Manager", "Development")
print(manager.describe())
8. Error Handling with try-except
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")
finally:
print("Execution completed.")
9. File Input/Output (I/O)
Writing to a file
with open("hello.txt", "w") as file:
file.write("Hello, Skye8!")
Reading from a file
with open("hello.txt", "r") as file:
content = file.read()
print(content)
10. Python Package Management: pip and Virtual Environments
Installing packages with pip
pip install requests
Listing installed packages
pip list
Freezing dependencies
pip freeze > requirements.txt
Installing dependencies from requirements.txt
pip install -r requirements.txt
Using Virtual Environments
Isolate dependencies per project:
python3 -m venv venv # Create virtual environment
source venv/bin/activate # Activate (Linux/macOS)
venv\Scripts\activate # Activate (Windows)
11. Writing a Simple Python Project
Suggested project structure for a beginner:
my_python_project/
├── app.py # Main application script
├── utils.py # Utility functions
├── requirements.txt # External dependencies
└── README.md # Project description
12. Writing and Running Tests
Python includes the unittest module for testing.
# test_utils.py
import unittest
from utils import add
class TestUtils(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
if __name__ == "__main__":
unittest.main()
Run tests:
python test_utils.py
Mastering Python basics from variables and data structures to OOP, file handling, and package management — forms the foundation for effective software development. At Skye8, these fundamentals empower our teams to write clean, maintainable, and scalable code that drives innovation across domains.
Comments (0)
Leave a Comment
Be the first to comment on this article!