<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Feddy Mwanjumwa</title>
    <description>The latest articles on DEV Community by Feddy Mwanjumwa (@feddy_mwanjumwa_e4047cf0c).</description>
    <link>https://dev.to/feddy_mwanjumwa_e4047cf0c</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3952222%2F538fcc25-0f91-4386-8c6b-23ff856e7bf8.png</url>
      <title>DEV Community: Feddy Mwanjumwa</title>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/feddy_mwanjumwa_e4047cf0c"/>
    <language>en</language>
    <item>
      <title>Python OOP: How Classes and Objects Finally Started Making Sense</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 15:12:10 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-oop-how-classes-and-objects-finally-started-making-sense-52n4</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-oop-how-classes-and-objects-finally-started-making-sense-52n4</guid>
      <description>&lt;p&gt;Object-Oriented Programming, or OOP, was one of the Python topics that looked complicated before I actually used it.&lt;/p&gt;

&lt;p&gt;I kept seeing words like classes, objects, attributes, methods, and inheritance.&lt;/p&gt;

&lt;p&gt;At first, I was asking myself, "Why can't I just use functions and variables?"&lt;/p&gt;

&lt;p&gt;After practicing, I started to understand that OOP is mainly about organising related data and behaviour together.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is OOP?
&lt;/h2&gt;

&lt;p&gt;OOP stands for Object-Oriented Programming.&lt;/p&gt;

&lt;p&gt;The basic idea is to create objects that contain information and actions related to that object.&lt;/p&gt;

&lt;p&gt;For example, if I'm building a school system, a student has information such as a name, age, and course.&lt;/p&gt;

&lt;p&gt;A student can also perform actions, such as introducing themselves.&lt;/p&gt;

&lt;p&gt;OOP gives me a way to put all of that together.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classes
&lt;/h2&gt;

&lt;p&gt;A class is like a blueprint for creating objects.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Student:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pass&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Right now, the class doesn't do anything.&lt;/p&gt;

&lt;p&gt;I'm basically saying, "I want a blueprint called Student."&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an object
&lt;/h2&gt;

&lt;p&gt;An object is an actual instance of a class.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student1 = Student()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student2 = Student()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I have two Student objects created from the same class.&lt;/p&gt;

&lt;p&gt;This was one distinction I had to understand:&lt;/p&gt;

&lt;p&gt;Class → blueprint&lt;/p&gt;

&lt;p&gt;Object → actual thing created from the blueprint&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;init&lt;/strong&gt;()
&lt;/h2&gt;

&lt;p&gt;A class becomes much more useful when I give its objects some information.&lt;/p&gt;

&lt;p&gt;That's where &lt;code&gt;__init__()&lt;/code&gt; comes in.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Student:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def __init__(self, name, age):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.name = name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.age = age&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can create students like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student1 = Student("Feddy", 20)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student2 = Student("Amina", 21)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Each object gets its own name and age.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is self?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;self&lt;/code&gt; was probably one of the strangest things when I first saw OOP.&lt;/p&gt;

&lt;p&gt;I eventually understood that &lt;code&gt;self&lt;/code&gt; refers to the current object.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Student:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def __init__(self, name, age):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.name = name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.age = age&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When I create:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student1 = Student("Feddy", 20)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.name&lt;/code&gt; belongs to &lt;code&gt;student1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When I create:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student2 = Student("Amina", 21)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.name&lt;/code&gt; belongs to &lt;code&gt;student2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So the same class can create objects with different information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attributes
&lt;/h2&gt;

&lt;p&gt;Attributes are basically the data stored inside an object.&lt;/p&gt;

&lt;p&gt;In this example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.name = name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.age = age&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt; are attributes of the Student object.&lt;/p&gt;

&lt;p&gt;I can access them like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student1.name)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student1.age)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result could be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;20&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Methods
&lt;/h2&gt;

&lt;p&gt;Methods are functions that belong to a class.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Student:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def __init__(self, name, age):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.name = name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.age = age&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def introduce(self):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(f"My name is {self.name} and I am {self.age} years old.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can call the method:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student1.introduce()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;My name is Feddy and I am 20 years old.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was where OOP started becoming more practical for me.&lt;/p&gt;

&lt;p&gt;The object has data, and it also has behaviour.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing attributes
&lt;/h2&gt;

&lt;p&gt;Because objects can store data, I can also change that data.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student1.age = 21&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student1.age)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;21&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This can be useful when information changes while the program is running.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical example
&lt;/h2&gt;

&lt;p&gt;I can create a simple bank account class.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class BankAccount:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def __init__(self, owner, balance):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.owner = owner&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.balance = balance&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def deposit(self, amount):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.balance += amount&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def show_balance(self):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(f"{self.owner}'s balance: KSh {self.balance}")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can create an account:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;account = BankAccount("Feddy", 5000)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can deposit money:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;account.deposit(2000)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And check the balance:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;account.show_balance()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy's balance: KSh 7000&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This example helped me see why putting data and related functions inside one class can be useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Another important OOP concept is inheritance.&lt;/p&gt;

&lt;p&gt;Inheritance allows one class to get attributes and methods from another class.&lt;/p&gt;

&lt;p&gt;For example, I can create a general class:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Animal:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def speak(self):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Animal makes a sound")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then create another class based on it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Dog(Animal):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pass&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the Dog class automatically gets the &lt;code&gt;speak()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;I can do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dog = Dog()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dog.speak()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And get:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Animal makes a sound&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The Dog class inherited the method from Animal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overriding a method
&lt;/h2&gt;

&lt;p&gt;A child class can also change how an inherited method behaves.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Animal:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def speak(self):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Animal makes a sound")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Dog(Animal):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def speak(self):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Dog barks")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dog = Dog()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dog.speak()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Dog barks&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The Dog class has its own version of &lt;code&gt;speak()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is called method overriding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Another practical example
&lt;/h2&gt;

&lt;p&gt;Suppose I'm creating an employee system.&lt;/p&gt;

&lt;p&gt;I can start with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Employee:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def __init__(self, name, salary):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.name = name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self.salary = salary&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then I can create a manager class:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;class Manager(Employee):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def manage_team(self):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(f"{self.name} is managing the team.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;manager = Manager("Feddy", 120000)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can use the inherited information:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(manager.salary)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And also the method from Manager:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;manager.manage_team()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is where inheritance started making sense to me. I can reuse code from an existing class instead of starting everything from zero.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understood
&lt;/h2&gt;

&lt;p&gt;The main ideas I took from OOP are:&lt;/p&gt;

&lt;p&gt;Class → blueprint for creating objects.&lt;/p&gt;

&lt;p&gt;Object → an actual instance of a class.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;__init__()&lt;/code&gt; → sets up the object when it is created.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self&lt;/code&gt; → refers to the current object.&lt;/p&gt;

&lt;p&gt;Attributes → information stored in the object.&lt;/p&gt;

&lt;p&gt;Methods → actions the object can perform.&lt;/p&gt;

&lt;p&gt;Inheritance → allows one class to reuse another class's code.&lt;/p&gt;

&lt;p&gt;Once I understood these, OOP became much less intimidating.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;OOP took me longer to understand than some of the earlier Python topics.&lt;/p&gt;

&lt;p&gt;The syntax wasn't necessarily the hardest part. The difficult part was understanding why I would even want to organise my code this way.&lt;/p&gt;

&lt;p&gt;What finally helped was using realistic examples.&lt;/p&gt;

&lt;p&gt;A student has information and actions.&lt;/p&gt;

&lt;p&gt;A bank account has information and actions.&lt;/p&gt;

&lt;p&gt;An employee has information and actions.&lt;/p&gt;

&lt;p&gt;That's when I started seeing the point of OOP.&lt;/p&gt;

&lt;p&gt;Instead of having data and functions scattered everywhere, I can organise related things into objects.&lt;/p&gt;

&lt;p&gt;And that makes much more sense when a program starts getting bigger.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>learning</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Python Exception Handling: How I Learned to Handle Errors</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 13:46:55 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-exception-handling-how-i-learned-to-handle-errors-ac5</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-exception-handling-how-i-learned-to-handle-errors-ac5</guid>
      <description>&lt;p&gt;When I started writing Python programs, getting an error was frustrating.&lt;/p&gt;

&lt;p&gt;I would run my program and suddenly see a long error message in the terminal.&lt;/p&gt;

&lt;p&gt;At first, I just wanted the error to disappear.&lt;/p&gt;

&lt;p&gt;Then I realised that errors are actually useful because they tell me something went wrong.&lt;/p&gt;

&lt;p&gt;The next question was: how can I make my programs handle these situations without crashing?&lt;/p&gt;

&lt;p&gt;That's where exception handling came in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an exception?
&lt;/h2&gt;

&lt;p&gt;An exception is an error that happens while a program is running.&lt;/p&gt;

&lt;p&gt;For example, this will cause an error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;number = int(input("Enter a number: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If I enter:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;hello&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Python can't convert &lt;code&gt;"hello"&lt;/code&gt; into an integer, so the program crashes.&lt;/p&gt;

&lt;p&gt;Instead of letting that happen, I can handle the error.&lt;/p&gt;

&lt;h2&gt;
  
  
  try and except
&lt;/h2&gt;

&lt;p&gt;The basic structure is &lt;code&gt;try&lt;/code&gt; and &lt;code&gt;except&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;number = int(input("Enter a number: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(number)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;except ValueError:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Please enter a valid number.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now if I enter:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;hello&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I get:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Please enter a valid number.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The program doesn't suddenly stop.&lt;/p&gt;

&lt;p&gt;This was probably the first time exception handling really made sense to me.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical example
&lt;/h2&gt;

&lt;p&gt;Imagine I'm creating a simple division program.&lt;/p&gt;

&lt;p&gt;Normally:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;number = int(input("Enter a number: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;divisor = int(input("Enter the divisor: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;result = number / divisor&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(result)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But what happens if the user enters &lt;code&gt;0&lt;/code&gt; as the divisor?&lt;/p&gt;

&lt;p&gt;Python raises a &lt;code&gt;ZeroDivisionError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I can handle it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;number = int(input("Enter a number: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;divisor = int(input("Enter the divisor: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;result = number / divisor&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(result)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;except ZeroDivisionError:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("You cannot divide by zero.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the program can respond properly instead of crashing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling different errors
&lt;/h2&gt;

&lt;p&gt;A program can have more than one possible error.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;number = int(input("Enter a number: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;divisor = int(input("Enter the divisor: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(number / divisor)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;except ValueError:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Please enter numbers only.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;except ZeroDivisionError:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("You cannot divide by zero.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now Python can handle each problem differently.&lt;/p&gt;

&lt;p&gt;That helped me understand that &lt;code&gt;except&lt;/code&gt; isn't just about saying "something went wrong."&lt;/p&gt;

&lt;p&gt;I can tell Python what kind of problem I expect and what I want it to do about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  else
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;else&lt;/code&gt; runs when no exception occurs.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;number = int(input("Enter a number: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;except ValueError:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Invalid number.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;else:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(f"You entered {number}.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;p&gt;Correct input → &lt;code&gt;else&lt;/code&gt; runs.&lt;/p&gt;

&lt;p&gt;Invalid input → &lt;code&gt;except&lt;/code&gt; runs.&lt;/p&gt;

&lt;p&gt;I found this useful when I wanted to separate successful code from error-handling code.&lt;/p&gt;

&lt;h2&gt;
  
  
  finally
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;finally&lt;/code&gt; runs whether there is an error or not.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file = open("notes.txt", "r")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(file.read())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;except FileNotFoundError:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("The file does not exist.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;finally:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Finished attempting to read the file.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; block will always run.&lt;/p&gt;

&lt;p&gt;This is useful when there is something I need to do regardless of whether the operation succeeded.&lt;/p&gt;

&lt;h2&gt;
  
  
  A file example
&lt;/h2&gt;

&lt;p&gt;Since I had already learnt file handling, this was one of the easiest ways for me to understand exception handling.&lt;/p&gt;

&lt;p&gt;Imagine I try to open a file that doesn't exist:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("contacts.txt", "r") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(file.read())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;except FileNotFoundError:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Contacts file not found.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Instead of getting a long traceback, my program can give the user a clear message.&lt;/p&gt;

&lt;h2&gt;
  
  
  raise
&lt;/h2&gt;

&lt;p&gt;Sometimes I don't want to wait for Python to generate an error.&lt;/p&gt;

&lt;p&gt;I can create an exception myself using &lt;code&gt;raise&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, suppose I don't want negative ages:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age = int(input("Enter your age: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if age &amp;lt; 0:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;raise ValueError("Age cannot be negative.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(age)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This lets me create my own rule and stop the program when that rule is broken.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical validation example
&lt;/h2&gt;

&lt;p&gt;Suppose I'm creating a simple registration system.&lt;/p&gt;

&lt;p&gt;I want the user to be at least 18:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age = int(input("Enter your age: "))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if age &amp;lt; 18:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;raise ValueError("You must be at least 18 years old.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Registration successful.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I'm now checking for a condition that matters to my program rather than just relying on Python to find a technical error.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understood
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learnt is that exception handling isn't about hiding errors.&lt;/p&gt;

&lt;p&gt;It's about deciding how my program should respond when something unexpected happens.&lt;/p&gt;

&lt;p&gt;I started thinking about it like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;try&lt;/code&gt; → "Try this code."&lt;/p&gt;

&lt;p&gt;&lt;code&gt;except&lt;/code&gt; → "If this specific problem happens, handle it like this."&lt;/p&gt;

&lt;p&gt;&lt;code&gt;else&lt;/code&gt; → "If everything worked, do this."&lt;/p&gt;

&lt;p&gt;&lt;code&gt;finally&lt;/code&gt; → "Do this no matter what."&lt;/p&gt;

&lt;p&gt;&lt;code&gt;raise&lt;/code&gt; → "I want to create an error because this situation isn't acceptable."&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;At first, errors made me feel like my code was completely wrong.&lt;/p&gt;

&lt;p&gt;Now I see them differently.&lt;/p&gt;

&lt;p&gt;Some errors are simply situations my program needs to be prepared for.&lt;/p&gt;

&lt;p&gt;A user might enter the wrong type of data.&lt;/p&gt;

&lt;p&gt;A file might not exist.&lt;/p&gt;

&lt;p&gt;Someone might try to divide by zero.&lt;/p&gt;

&lt;p&gt;A value might break a rule in my program.&lt;/p&gt;

&lt;p&gt;Exception handling gives me a way to deal with those situations without making the whole program fall apart.&lt;/p&gt;

&lt;p&gt;That was an important shift for me: a good program isn't one where errors never happen. It's one that knows how to handle them.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Python File Handling: How I Learned to Save Data Outside My Program</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 13:46:11 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-file-handling-how-i-learned-to-save-data-outside-my-program-2nj9</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-file-handling-how-i-learned-to-save-data-outside-my-program-2nj9</guid>
      <description>&lt;p&gt;One thing I noticed while learning Python was that all the data I created disappeared when I stopped the program.&lt;/p&gt;

&lt;p&gt;For example, I could create a list of contacts:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;contacts = ["Amina", "Brian", "Feddy"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It would work while the program was running, but once I closed it, the data was gone.&lt;/p&gt;

&lt;p&gt;So I started wondering: how do I make Python save information?&lt;/p&gt;

&lt;p&gt;That's where file handling came in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is file handling?
&lt;/h2&gt;

&lt;p&gt;File handling is simply using Python to create, read, write, and update files.&lt;/p&gt;

&lt;p&gt;For example, I can create a text file called &lt;code&gt;notes.txt&lt;/code&gt; and use Python to store information in it.&lt;/p&gt;

&lt;p&gt;The function I use is &lt;code&gt;open()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Opening a file
&lt;/h2&gt;

&lt;p&gt;I can open a file like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file = open("notes.txt", "r")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;"r"&lt;/code&gt; means I'm opening the file for reading.&lt;/p&gt;

&lt;p&gt;I can then read the contents:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;content = file.read()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(content)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And when I'm finished:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file.close()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This works, but I quickly learnt that there is a better way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using with open()
&lt;/h2&gt;

&lt;p&gt;Instead of manually closing the file, I can use &lt;code&gt;with&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("notes.txt", "r") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;content = file.read()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(content)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Python automatically handles closing the file after I'm done.&lt;/p&gt;

&lt;p&gt;This is the approach I prefer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading a file
&lt;/h2&gt;

&lt;p&gt;There are a few ways to read a file.&lt;/p&gt;

&lt;h3&gt;
  
  
  read()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;read()&lt;/code&gt; gives me the entire file as one string.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("notes.txt", "r") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;content = file.read()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(content)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the file contains:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;I am learning Python.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I get that entire text back.&lt;/p&gt;

&lt;h2&gt;
  
  
  readline()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;readline()&lt;/code&gt; reads one line.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("notes.txt", "r") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;line = file.readline()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(line)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is useful when I only need the first line or want to process a file one line at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  readlines()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;readlines()&lt;/code&gt; reads all the lines and gives them to me as a list.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("notes.txt", "r") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lines = file.readlines()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(lines)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the file contains:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Bread&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Milk&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Sugar&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I get something similar to:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;["Bread\n", "Milk\n", "Sugar\n"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also loop directly through the file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("notes.txt", "r") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for line in file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(line.strip())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I like this approach when working with files containing many records.&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing to a file
&lt;/h2&gt;

&lt;p&gt;If I want Python to write something into a file, I use &lt;code&gt;"w"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("notes.txt", "w") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file.write("I am learning Python.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the text is saved inside &lt;code&gt;notes.txt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One thing I had to learn here is that &lt;code&gt;"w"&lt;/code&gt; can overwrite what is already in the file.&lt;/p&gt;

&lt;p&gt;So if I already had:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;I am learning Python.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and then ran:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("notes.txt", "w") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file.write("I am learning SQL.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the old text would be replaced.&lt;/p&gt;

&lt;h2&gt;
  
  
  Appending to a file
&lt;/h2&gt;

&lt;p&gt;What if I want to keep the old information and add something new?&lt;/p&gt;

&lt;p&gt;That's where &lt;code&gt;"a"&lt;/code&gt; comes in.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("notes.txt", "a") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file.write("\nI am also learning SQL.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the file contains both pieces of information.&lt;/p&gt;

&lt;p&gt;This made sense to me for things like logs, transactions, or contact books where I'm constantly adding new records.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical contact book
&lt;/h2&gt;

&lt;p&gt;One of the examples that helped me understand file handling was saving contacts.&lt;/p&gt;

&lt;p&gt;I can start with a dictionary:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;contacts = {"Amina": "0712345678", "Brian": "0723456789", "Feddy": "0734567890"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can save them to a file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("contacts.txt", "w") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for name, phone in contacts.items():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file.write(f"{name}: {phone}\n")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now my &lt;code&gt;contacts.txt&lt;/code&gt; file will contain:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Amina: 0712345678&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Brian: 0723456789&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy: 0734567890&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was the point where file handling started feeling like something I could actually use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding a new contact
&lt;/h2&gt;

&lt;p&gt;Later, I can add another contact without deleting the existing ones:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = "Faith"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;phone = "0745678901"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("contacts.txt", "a") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;file.write(f"{name}: {phone}\n")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now Faith is added to the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading the contacts back
&lt;/h2&gt;

&lt;p&gt;I can also open the file later and read the saved contacts:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("contacts.txt", "r") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for line in file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(line.strip())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is what really made the concept click for me.&lt;/p&gt;

&lt;p&gt;My program can save data, close, and later open the file and get the data back.&lt;/p&gt;

&lt;h2&gt;
  
  
  File modes
&lt;/h2&gt;

&lt;p&gt;The three modes I used most are:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"r"&lt;/code&gt; → read&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"w"&lt;/code&gt; → write&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"a"&lt;/code&gt; → append&lt;/p&gt;

&lt;p&gt;So I started thinking about them like this:&lt;/p&gt;

&lt;p&gt;I want to see what's already there → &lt;code&gt;"r"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I want to create or replace the contents → &lt;code&gt;"w"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I want to add something without deleting what's already there → &lt;code&gt;"a"&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking whether a file exists
&lt;/h2&gt;

&lt;p&gt;Sometimes I don't know whether a file already exists.&lt;/p&gt;

&lt;p&gt;Python provides the &lt;code&gt;os&lt;/code&gt; module for this.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import os&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if os.path.exists("contacts.txt"):&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("File exists")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;else:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("File does not exist")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This became useful when I started thinking about programs that need to work with files safely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working with lines
&lt;/h2&gt;

&lt;p&gt;Suppose I have a file containing product names:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Bread&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Milk&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Sugar&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can read each one and clean it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open("products.txt", "r") as file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for line in file:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;product = line.strip()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(product)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;strip()&lt;/code&gt; removes the newline character and any unnecessary spaces.&lt;/p&gt;

&lt;p&gt;This connects nicely with what I learnt about strings.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understood
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learnt about file handling is that it gives my Python programs a way to remember information after they stop running.&lt;/p&gt;

&lt;p&gt;The basic idea became:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;open()&lt;/code&gt; → access the file&lt;/p&gt;

&lt;p&gt;&lt;code&gt;read()&lt;/code&gt; → get information&lt;/p&gt;

&lt;p&gt;&lt;code&gt;write()&lt;/code&gt; → save or replace information&lt;/p&gt;

&lt;p&gt;&lt;code&gt;append()&lt;/code&gt; → add information&lt;/p&gt;

&lt;p&gt;&lt;code&gt;close()&lt;/code&gt; → close the file&lt;/p&gt;

&lt;p&gt;And using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;with open(...)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;makes the process much cleaner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Before learning file handling, my programs only remembered things while they were running.&lt;/p&gt;

&lt;p&gt;After learning it, I could save contacts, notes, products, and other information to a file and use that information later.&lt;/p&gt;

&lt;p&gt;That was a small but important step for me because Python started feeling less like a language for simple exercises and more like something I could use to build actual programs.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>python</category>
    </item>
    <item>
      <title>Python Strings: What I Learned About Working with Text</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 13:42:29 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-strings-what-i-learned-about-working-with-text-46gk</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-strings-what-i-learned-about-working-with-text-46gk</guid>
      <description>&lt;p&gt;Strings were one of the first Python concepts I came across.&lt;/p&gt;

&lt;p&gt;At first, I thought a string was just text inside quotation marks.&lt;/p&gt;

&lt;p&gt;And honestly, that's basically what it is.&lt;/p&gt;

&lt;p&gt;But once I started working with names, messages, passwords, and other text, I realised there was a lot more I could do with strings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a string
&lt;/h2&gt;

&lt;p&gt;A string is simply text.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = "Feddy"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;message = 'Welcome to Python'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Both single and double quotation marks work.&lt;/p&gt;

&lt;p&gt;I usually use double quotes, but the important thing is to be consistent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing characters
&lt;/h2&gt;

&lt;p&gt;Just like lists, strings are indexed.&lt;/p&gt;

&lt;p&gt;Python starts counting from &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = "Feddy"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name[0])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;F&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The second character is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name[1])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;e&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was useful because I realised I could work with individual characters instead of treating the whole string as one thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Negative indexing
&lt;/h2&gt;

&lt;p&gt;I can also count from the end.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = "Feddy"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name[-1])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;y&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name[-2])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;d&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found this especially useful when I wanted to get the last character without calculating the length of the string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slicing strings
&lt;/h2&gt;

&lt;p&gt;I can also take part of a string using slicing.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = "Feddy Mwanjumwa"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name[0:5])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The starting position is included, but the ending position is not.&lt;/p&gt;

&lt;p&gt;I can also get everything after a certain position:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name[6:])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Mwanjumwa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once I understood slicing with lists, slicing strings felt much easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Changing the case
&lt;/h2&gt;

&lt;p&gt;Python gives me several ways to change the case of text.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = "feddy mwanjumwa"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name.upper())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FEDDY MWANJUMWA&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name.lower())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;feddy mwanjumwa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And if I want the first letter of each word capitalised:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name.title())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy Mwanjumwa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found these methods useful when dealing with inconsistent text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing unwanted spaces
&lt;/h2&gt;

&lt;p&gt;Sometimes text has spaces that shouldn't be there.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = "   Feddy   "&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name.strip())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are also:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lstrip()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rstrip()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;which remove spaces from the left and right respectively.&lt;/p&gt;

&lt;p&gt;This became particularly useful when I started thinking about cleaning data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Replacing text
&lt;/h2&gt;

&lt;p&gt;I can replace part of a string using &lt;code&gt;replace()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;message = "I am learning Java"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;message = message.replace("Java", "Python")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the message becomes:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;I am learning Python&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is useful when I need to correct or change specific text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking whether text exists
&lt;/h2&gt;

&lt;p&gt;I can check whether a word or character exists inside a string using &lt;code&gt;in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;email = "feddy@gmail.com"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("@gmail.com" in email)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also use this in a condition:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if "@gmail.com" in email:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Gmail address")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This made strings much more useful when I started thinking about validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding text
&lt;/h2&gt;

&lt;p&gt;I can use &lt;code&gt;find()&lt;/code&gt; to get the position of a piece of text.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;email = "feddy@gmail.com"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(email.find("@"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So the &lt;code&gt;@&lt;/code&gt; is at index 5.&lt;/p&gt;

&lt;p&gt;I can use this when I need to know where something appears inside a string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Counting characters or words
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;count()&lt;/code&gt; tells me how many times something appears.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;message = "Python is easy and Python is useful"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(message.count("Python"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This can be useful when analysing or checking text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Splitting a string
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;split()&lt;/code&gt; turns a string into a list.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;names = "Feddy,Amina,Brian"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;names = names.split(",")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I get:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;["Feddy", "Amina", "Brian"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was an important one for me because it showed me how I could move from text into a list of individual values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Joining strings
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;join()&lt;/code&gt; does almost the opposite.&lt;/p&gt;

&lt;p&gt;Suppose I have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;names = ["Feddy", "Amina", "Brian"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can join them together:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;result = ", ".join(names)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy, Amina, Brian&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is useful when I have separate pieces of text that I want to combine into one string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking the beginning and end
&lt;/h2&gt;

&lt;p&gt;I can check whether a string starts or ends with certain text.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;filename = "report.pdf"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(filename.startswith("report"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(filename.endswith(".pdf"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;also gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This can be useful when working with filenames or user input.&lt;/p&gt;

&lt;h2&gt;
  
  
  String formatting with f-strings
&lt;/h2&gt;

&lt;p&gt;One of my favourite things about Python strings is f-strings.&lt;/p&gt;

&lt;p&gt;They make it easy to combine text with variables.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = "Feddy"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age = 20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(f"My name is {name} and I am {age} years old.")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;My name is Feddy and I am 20 years old.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found this much cleaner than constantly joining strings together with &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical example
&lt;/h2&gt;

&lt;p&gt;I can put several string concepts together in a simple program.&lt;/p&gt;

&lt;p&gt;Suppose a user enters their name with extra spaces and strange capitalisation:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = input("Enter your name: ")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can clean it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name = name.strip().title()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then display it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(f"Welcome, {name}!")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So if the user enters:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fEDDY mwanjumwa&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the program displays:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Welcome, Feddy Mwanjumwa!&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was the kind of example that helped me understand why string methods actually matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understood
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learnt is that strings aren't just text that I print on the screen.&lt;/p&gt;

&lt;p&gt;I can clean them, search them, split them, combine them, change their case, check them, and extract specific parts from them.&lt;/p&gt;

&lt;p&gt;The methods I started using most are:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;strip()&lt;/code&gt; → remove unwanted spaces&lt;/p&gt;

&lt;p&gt;&lt;code&gt;upper()&lt;/code&gt; → convert to uppercase&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lower()&lt;/code&gt; → convert to lowercase&lt;/p&gt;

&lt;p&gt;&lt;code&gt;title()&lt;/code&gt; → capitalise words&lt;/p&gt;

&lt;p&gt;&lt;code&gt;replace()&lt;/code&gt; → replace text&lt;/p&gt;

&lt;p&gt;&lt;code&gt;split()&lt;/code&gt; → turn text into a list&lt;/p&gt;

&lt;p&gt;&lt;code&gt;join()&lt;/code&gt; → combine text&lt;/p&gt;

&lt;p&gt;&lt;code&gt;find()&lt;/code&gt; → find the position of text&lt;/p&gt;

&lt;p&gt;&lt;code&gt;count()&lt;/code&gt; → count occurrences&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Strings seemed very basic when I first started Python.&lt;/p&gt;

&lt;p&gt;But once I started working with real text, I realised how often I needed to manipulate it.&lt;/p&gt;

&lt;p&gt;For me, strings really started making sense when I stopped thinking of them as just words inside quotation marks and started thinking of them as data that I could work with.&lt;/p&gt;

&lt;p&gt;That shift made a big difference in how I approached Python.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>python</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Python Sets: How I Learned to Work with Unique Values</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 13:39:57 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-sets-how-i-learned-to-work-with-unique-values-2dmb</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-sets-how-i-learned-to-work-with-unique-values-2dmb</guid>
      <description>&lt;p&gt;When I first came across sets in Python, I didn't immediately see why I needed them.&lt;/p&gt;

&lt;p&gt;I already had lists and dictionaries, so I wondered what sets were supposed to do differently.&lt;/p&gt;

&lt;p&gt;The thing that made sets click for me was one word , unique.&lt;/p&gt;

&lt;p&gt;A set is useful when I want to store values without duplicates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a set
&lt;/h2&gt;

&lt;p&gt;A set is created using curly brackets.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;numbers = {1, 2, 3, 4}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also create a set from a list:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;numbers = set([1, 2, 2, 3, 3, 4])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{1, 2, 3, 4}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The duplicate values are removed automatically.&lt;/p&gt;

&lt;p&gt;That was the first thing that made sets useful to me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why would I use a set?
&lt;/h2&gt;

&lt;p&gt;Imagine I have customer locations:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations = ["Nairobi", "Mombasa", "Nairobi", "Kisumu", "Mombasa"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If I want to know the unique locations, I can do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unique_locations = set(locations)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I get:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"Nairobi", "Mombasa", "Kisumu"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Instead of manually checking for duplicates, Python handles it for me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding items
&lt;/h2&gt;

&lt;p&gt;I can add an item using &lt;code&gt;add()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations = {"Nairobi", "Mombasa"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations.add("Kisumu")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the set contains:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"Nairobi", "Mombasa", "Kisumu"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If I try to add something that's already there:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations.add("Nairobi")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Python doesn't create another &lt;code&gt;"Nairobi"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That's because sets only keep unique values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing items
&lt;/h2&gt;

&lt;p&gt;I can remove an item using &lt;code&gt;remove()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations.remove("Mombasa")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also use &lt;code&gt;discard()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations.discard("Mombasa")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The difference is that &lt;code&gt;remove()&lt;/code&gt; raises an error if the item doesn't exist, while &lt;code&gt;discard()&lt;/code&gt; simply does nothing.&lt;/p&gt;

&lt;p&gt;I found &lt;code&gt;discard()&lt;/code&gt; useful when I wasn't sure whether a value was already in the set.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking if an item exists
&lt;/h2&gt;

&lt;p&gt;Just like with lists, I can use &lt;code&gt;in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations = {"Nairobi", "Mombasa", "Kisumu"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Nairobi" in locations)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is useful when I only need to know whether a value exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the length
&lt;/h2&gt;

&lt;p&gt;I can use &lt;code&gt;len()&lt;/code&gt; with a set:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations = {"Nairobi", "Mombasa", "Kisumu"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(len(locations))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This tells me how many unique values are in the set.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sets and loops
&lt;/h2&gt;

&lt;p&gt;I can loop through a set just like other Python collections.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;locations = {"Nairobi", "Mombasa", "Kisumu"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for location in locations:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(location)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Python goes through each unique value.&lt;/p&gt;

&lt;p&gt;One thing I had to understand is that sets don't work like lists when it comes to positions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sets don't use indexing
&lt;/h2&gt;

&lt;p&gt;With a list, I can do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;products = ["Bread", "Milk", "Sugar"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(products[0])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But I can't do that with a set:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;products = {"Bread", "Milk", "Sugar"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(products[0])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A set doesn't have positions that I can rely on.&lt;/p&gt;

&lt;p&gt;That is another reason I wouldn't use a set when the order of items matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Union
&lt;/h2&gt;

&lt;p&gt;Sets become really useful when comparing groups of data.&lt;/p&gt;

&lt;p&gt;Suppose I have customers from two branches:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;nairobi_customers = {"Amina", "Brian", "Feddy"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mombasa_customers = {"Brian", "Faith", "Kevin"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can combine all unique customers using &lt;code&gt;union()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;all_customers = nairobi_customers.union(mombasa_customers)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result contains:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"Amina", "Brian", "Feddy", "Faith", "Kevin"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Brian only appears once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Intersection
&lt;/h2&gt;

&lt;p&gt;What if I want to find customers who appear in both branches?&lt;/p&gt;

&lt;p&gt;I can use &lt;code&gt;intersection()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;common_customers = nairobi_customers.intersection(mombasa_customers)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"Brian"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was one of the most useful set operations for me because it makes comparing groups very easy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Difference
&lt;/h2&gt;

&lt;p&gt;I can also find values that exist in one set but not another.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;nairobi_only = nairobi_customers.difference(mombasa_customers)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"Amina", "Feddy"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;These are customers in the Nairobi set who are not in the Mombasa set.&lt;/p&gt;

&lt;h2&gt;
  
  
  Symmetric difference
&lt;/h2&gt;

&lt;p&gt;There is also &lt;code&gt;symmetric_difference()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It gives me values that are in either set, but not in both.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;different_customers = nairobi_customers.symmetric_difference(mombasa_customers)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"Amina", "Feddy", "Faith", "Kevin"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Brian isn't included because he appears in both sets.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical example
&lt;/h2&gt;

&lt;p&gt;Imagine I have two lists of students who attended different events.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;event_one = ["Feddy", "Amina", "Brian", "Faith"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;event_two = ["Brian", "Faith", "Kevin", "David"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can convert them to sets:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set_one = set(event_one)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set_two = set(event_two)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can find students who attended both events:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set_one.intersection(set_two)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Students who only attended the first event:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set_one.difference(set_two)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And everyone who attended at least one of the events:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;set_one.union(set_two)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is where I started seeing why sets exist.&lt;/p&gt;

&lt;p&gt;They're not just another way to store a bunch of values. They're really useful when I care about uniqueness and comparing groups.&lt;/p&gt;

&lt;h2&gt;
  
  
  Converting a set back to a list
&lt;/h2&gt;

&lt;p&gt;Sometimes I may need a list after removing duplicates.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;names = ["Feddy", "Amina", "Feddy", "Brian"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can remove duplicates:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unique_names = set(names)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And convert the result back to a list:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unique_names = list(set(names))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I have a list containing only the unique names.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understood
&lt;/h2&gt;

&lt;p&gt;The main thing I learnt about sets is that I shouldn't think of them as a replacement for lists.&lt;/p&gt;

&lt;p&gt;They solve a different problem.&lt;/p&gt;

&lt;p&gt;If I need ordered data, I think of a list.&lt;/p&gt;

&lt;p&gt;If I need unique data, I think of a set.&lt;/p&gt;

&lt;p&gt;If I need to compare two groups of values, sets become especially useful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sets were probably one of those Python topics that only made sense after I actually had a problem to solve.&lt;/p&gt;

&lt;p&gt;The moment I needed to remove duplicate data or compare two groups, sets suddenly became useful.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>webdev</category>
      <category>python</category>
    </item>
    <item>
      <title>Python Dictionaries: What I Learned About Key-Value Data</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 13:33:13 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-dictionaries-what-i-learned-about-key-value-data-18hp</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-dictionaries-what-i-learned-about-key-value-data-18hp</guid>
      <description>&lt;p&gt;After working with lists and tuples, I came across dictionaries.&lt;/p&gt;

&lt;p&gt;At first, dictionaries felt a little strange because I was used to accessing things by position, like &lt;code&gt;products[0]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With dictionaries, I had to use keys instead.&lt;/p&gt;

&lt;p&gt;That was the main idea I had to understand.&lt;/p&gt;

&lt;p&gt;A dictionary stores information as key-value pairs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a dictionary
&lt;/h2&gt;

&lt;p&gt;A dictionary uses curly brackets &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = {"name": "Feddy", "age": 20, "course": "Data Science"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"name"&lt;/code&gt; is the key.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"Feddy"&lt;/code&gt; is the value.&lt;/p&gt;

&lt;p&gt;The same applies to the other pieces of information.&lt;/p&gt;

&lt;p&gt;I started thinking of it as giving every piece of information a label.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing values
&lt;/h2&gt;

&lt;p&gt;Unlike a list, I don't use a position.&lt;/p&gt;

&lt;p&gt;I use the key:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = {"name": "Feddy", "age": 20, "course": "Data Science"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student["name"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also access the age:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student["age"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was the point where dictionaries started making sense to me.&lt;/p&gt;

&lt;p&gt;Instead of thinking, "Which position is the age in?", I just ask for &lt;code&gt;"age"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using get()
&lt;/h2&gt;

&lt;p&gt;There is another way to access a value:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student.get("name")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This also gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The difference becomes useful when the key doesn't exist.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student.get("phone"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Instead of raising an error, Python returns:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;None&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can even provide a default value:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student.get("phone", "No phone number"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I get:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;No phone number&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found this useful when working with data where some information might be missing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding new values
&lt;/h2&gt;

&lt;p&gt;Dictionaries are changeable.&lt;/p&gt;

&lt;p&gt;I can add a new key-value pair like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student["city"] = "Nairobi"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the dictionary contains the city as well.&lt;/p&gt;

&lt;p&gt;I don't have to recreate the entire dictionary just to add one more piece of information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating values
&lt;/h2&gt;

&lt;p&gt;I can also change an existing value.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student["age"] = 21&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the age is &lt;code&gt;21&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same syntax is used for both adding and updating.&lt;/p&gt;

&lt;p&gt;If the key already exists, the value changes.&lt;/p&gt;

&lt;p&gt;If it doesn't exist, Python adds it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing values
&lt;/h2&gt;

&lt;p&gt;There are a few ways to remove dictionary items.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;pop()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student.pop("city")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This removes the &lt;code&gt;"city"&lt;/code&gt; entry.&lt;/p&gt;

&lt;p&gt;I can also use &lt;code&gt;del&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;del student["age"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And if I want to remove everything:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student.clear()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The dictionary itself still exists, but there is nothing inside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking if a key exists
&lt;/h2&gt;

&lt;p&gt;I can check whether a key is in a dictionary using &lt;code&gt;in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"name" in student&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This returns:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also use it in a condition:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if "phone" in student:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Phone number exists")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This became useful when dealing with information that isn't always available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the number of items
&lt;/h2&gt;

&lt;p&gt;Just like with lists and tuples, I can use &lt;code&gt;len()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = {"name": "Feddy", "age": 20, "course": "Data Science"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(len(student))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are three key-value pairs in the dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting all keys
&lt;/h2&gt;

&lt;p&gt;I can get all the keys using &lt;code&gt;keys()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student.keys()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for key in student.keys():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(key)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This would give me:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;course&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I use this when I only care about the labels.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting all values
&lt;/h2&gt;

&lt;p&gt;I can get the values using &lt;code&gt;values()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for value in student.values():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Data Science&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting keys and values together
&lt;/h2&gt;

&lt;p&gt;This is one of the dictionary methods I use most.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;items()&lt;/code&gt; gives me both:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for key, value in student.items():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(f"{key}: {value}")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The output would be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name: Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age: 20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;course: Data Science&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is especially useful when I want to go through all the information in a dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looping through dictionaries
&lt;/h2&gt;

&lt;p&gt;I can loop through a dictionary in different ways.&lt;/p&gt;

&lt;p&gt;Just the keys:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for key in student:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(key)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Just the values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for value in student.values():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or both:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for key, value in student.items():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(key, value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found &lt;code&gt;.items()&lt;/code&gt; easiest when I wanted to actually work with the information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dictionary with numbers
&lt;/h2&gt;

&lt;p&gt;Dictionaries don't have to store text.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;prices = {"Bread": 80, "Milk": 120, "Sugar": 180}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can get the price of milk:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(prices["Milk"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;120&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also update it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;prices["Milk"] = 130&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the price is &lt;code&gt;130&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This made dictionaries feel useful for things like products and prices.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical inventory example
&lt;/h2&gt;

&lt;p&gt;Let's say I'm building a small shop inventory.&lt;/p&gt;

&lt;p&gt;I could have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;product = {"name": "Bread", "price": 80, "quantity": 20}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can access each piece of information:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(product["name"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(product["price"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(product["quantity"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also calculate the value of the stock:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stock_value = product["price"] * product["quantity"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(stock_value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1600&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the dictionary is storing information, while my code is using that information to do something useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested dictionaries
&lt;/h2&gt;

&lt;p&gt;This is where dictionaries become much more powerful.&lt;/p&gt;

&lt;p&gt;A dictionary can contain another dictionary.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;students = {&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"student1": {"name": "Feddy", "age": 20, "course": "Data Science"},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"student2": {"name": "Amina", "age": 21, "course": "Python"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can access Feddy's name using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(students["student1"]["name"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And Amina's course:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(students["student2"]["course"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Python&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At first this looked confusing because there are multiple brackets, but I realised I was simply going one dictionary level at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  A list of dictionaries
&lt;/h2&gt;

&lt;p&gt;I can also combine dictionaries with lists.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;students = [&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Feddy", "age": 20},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Amina", "age": 21},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Brian", "age": 19}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I have a list containing three dictionaries.&lt;/p&gt;

&lt;p&gt;I can loop through them:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for student in students:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student["name"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Amina&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Brian&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This structure started making a lot of sense to me because it looks much more like real data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dictionary comprehension
&lt;/h2&gt;

&lt;p&gt;Just like lists have comprehensions, dictionaries do too.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;squares = {number: number ** 2 for number in range(1, 6)}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found this easier to understand after becoming comfortable with normal loops.&lt;/p&gt;

&lt;p&gt;The same idea is happening, but Python lets me write it in a shorter form.&lt;/p&gt;

&lt;h2&gt;
  
  
  Copying dictionaries
&lt;/h2&gt;

&lt;p&gt;Just like lists, I need to be careful when copying dictionaries.&lt;/p&gt;

&lt;p&gt;If I do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student2 = student&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;both variables refer to the same dictionary.&lt;/p&gt;

&lt;p&gt;So changing one can affect the other.&lt;/p&gt;

&lt;p&gt;To create a separate copy, I can use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student2 = student.copy()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can modify &lt;code&gt;student2&lt;/code&gt; without changing the original dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understood about dictionaries
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learnt is that dictionaries are really useful when the information needs labels.&lt;/p&gt;

&lt;p&gt;With a list, I might have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = ["Feddy", 20, "Data Science"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But now I have to remember that index &lt;code&gt;0&lt;/code&gt; is the name, index &lt;code&gt;1&lt;/code&gt; is the age, and index &lt;code&gt;2&lt;/code&gt; is the course.&lt;/p&gt;

&lt;p&gt;With a dictionary:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = {"name": "Feddy", "age": 20, "course": "Data Science"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I don't have to remember positions.&lt;/p&gt;

&lt;p&gt;I just use the key I need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lists vs dictionaries
&lt;/h2&gt;

&lt;p&gt;This is how I now think about the difference.&lt;/p&gt;

&lt;p&gt;A list:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;products = ["Bread", "Milk", "Sugar"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is useful when I mainly care about the collection of items.&lt;/p&gt;

&lt;p&gt;A dictionary:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;product = {"name": "Bread", "price": 80, "quantity": 20}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is useful when I care about different pieces of information about one thing.&lt;/p&gt;

&lt;p&gt;And I can combine them:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;inventory = [&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Bread", "price": 80, "quantity": 20},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Milk", "price": 120, "quantity": 15},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Sugar", "price": 180, "quantity": 10}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That combination is something I expect to use a lot as I build bigger Python programs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Dictionaries took some getting used to because they are different from the position-based approach I was used to with lists.&lt;/p&gt;

&lt;p&gt;But once I understood the idea of keys and values, everything started falling into place.&lt;/p&gt;

&lt;p&gt;I can add information, update it, remove it, search for it, loop through it, and even put dictionaries inside other data structures.&lt;/p&gt;

&lt;p&gt;The biggest thing I took away is simple:&lt;/p&gt;

&lt;p&gt;A list helps me organise items.&lt;/p&gt;

&lt;p&gt;A dictionary helps me organise information about those items.&lt;/p&gt;

&lt;p&gt;That distinction has made choosing between the two much easier for me.&lt;/p&gt;

&lt;p&gt;After working with lists and tuples, I came across dictionaries.&lt;/p&gt;

&lt;p&gt;At first, dictionaries felt a little strange because I was used to accessing things by position, like &lt;code&gt;products[0]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With dictionaries, I had to use keys instead.&lt;/p&gt;

&lt;p&gt;That was the main idea I had to understand.&lt;/p&gt;

&lt;p&gt;A dictionary stores information as key-value pairs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a dictionary
&lt;/h2&gt;

&lt;p&gt;A dictionary uses curly brackets &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = {"name": "Feddy", "age": 20, "course": "Data Science"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"name"&lt;/code&gt; is the key.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"Feddy"&lt;/code&gt; is the value.&lt;/p&gt;

&lt;p&gt;The same applies to the other pieces of information.&lt;/p&gt;

&lt;p&gt;I started thinking of it as giving every piece of information a label.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing values
&lt;/h2&gt;

&lt;p&gt;Unlike a list, I don't use a position.&lt;/p&gt;

&lt;p&gt;I use the key:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = {"name": "Feddy", "age": 20, "course": "Data Science"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student["name"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also access the age:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student["age"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was the point where dictionaries started making sense to me.&lt;/p&gt;

&lt;p&gt;Instead of thinking, "Which position is the age in?", I just ask for &lt;code&gt;"age"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using get()
&lt;/h2&gt;

&lt;p&gt;There is another way to access a value:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student.get("name")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This also gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The difference becomes useful when the key doesn't exist.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student.get("phone"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Instead of raising an error, Python returns:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;None&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can even provide a default value:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student.get("phone", "No phone number"))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I get:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;No phone number&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found this useful when working with data where some information might be missing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding new values
&lt;/h2&gt;

&lt;p&gt;Dictionaries are changeable.&lt;/p&gt;

&lt;p&gt;I can add a new key-value pair like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student["city"] = "Nairobi"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the dictionary contains the city as well.&lt;/p&gt;

&lt;p&gt;I don't have to recreate the entire dictionary just to add one more piece of information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating values
&lt;/h2&gt;

&lt;p&gt;I can also change an existing value.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student["age"] = 21&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the age is &lt;code&gt;21&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same syntax is used for both adding and updating.&lt;/p&gt;

&lt;p&gt;If the key already exists, the value changes.&lt;/p&gt;

&lt;p&gt;If it doesn't exist, Python adds it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing values
&lt;/h2&gt;

&lt;p&gt;There are a few ways to remove dictionary items.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;pop()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student.pop("city")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This removes the &lt;code&gt;"city"&lt;/code&gt; entry.&lt;/p&gt;

&lt;p&gt;I can also use &lt;code&gt;del&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;del student["age"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And if I want to remove everything:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student.clear()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The dictionary itself still exists, but there is nothing inside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking if a key exists
&lt;/h2&gt;

&lt;p&gt;I can check whether a key is in a dictionary using &lt;code&gt;in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"name" in student&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This returns:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also use it in a condition:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if "phone" in student:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Phone number exists")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This became useful when dealing with information that isn't always available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the number of items
&lt;/h2&gt;

&lt;p&gt;Just like with lists and tuples, I can use &lt;code&gt;len()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = {"name": "Feddy", "age": 20, "course": "Data Science"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(len(student))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are three key-value pairs in the dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting all keys
&lt;/h2&gt;

&lt;p&gt;I can get all the keys using &lt;code&gt;keys()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student.keys()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for key in student.keys():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(key)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This would give me:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;course&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I use this when I only care about the labels.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting all values
&lt;/h2&gt;

&lt;p&gt;I can get the values using &lt;code&gt;values()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for value in student.values():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Data Science&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting keys and values together
&lt;/h2&gt;

&lt;p&gt;This is one of the dictionary methods I use most.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;items()&lt;/code&gt; gives me both:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for key, value in student.items():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(f"{key}: {value}")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The output would be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name: Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age: 20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;course: Data Science&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is especially useful when I want to go through all the information in a dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looping through dictionaries
&lt;/h2&gt;

&lt;p&gt;I can loop through a dictionary in different ways.&lt;/p&gt;

&lt;p&gt;Just the keys:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for key in student:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(key)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Just the values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for value in student.values():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or both:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for key, value in student.items():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(key, value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found &lt;code&gt;.items()&lt;/code&gt; easiest when I wanted to actually work with the information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dictionary with numbers
&lt;/h2&gt;

&lt;p&gt;Dictionaries don't have to store text.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;prices = {"Bread": 80, "Milk": 120, "Sugar": 180}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can get the price of milk:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(prices["Milk"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;120&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also update it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;prices["Milk"] = 130&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the price is &lt;code&gt;130&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This made dictionaries feel useful for things like products and prices.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical inventory example
&lt;/h2&gt;

&lt;p&gt;Let's say I'm building a small shop inventory.&lt;/p&gt;

&lt;p&gt;I could have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;product = {"name": "Bread", "price": 80, "quantity": 20}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can access each piece of information:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(product["name"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(product["price"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(product["quantity"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also calculate the value of the stock:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;stock_value = product["price"] * product["quantity"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(stock_value)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;1600&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the dictionary is storing information, while my code is using that information to do something useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nested dictionaries
&lt;/h2&gt;

&lt;p&gt;This is where dictionaries become much more powerful.&lt;/p&gt;

&lt;p&gt;A dictionary can contain another dictionary.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;students = {&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"student1": {"name": "Feddy", "age": 20, "course": "Data Science"},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"student2": {"name": "Amina", "age": 21, "course": "Python"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can access Feddy's name using:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(students["student1"]["name"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And Amina's course:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(students["student2"]["course"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Python&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At first this looked confusing because there are multiple brackets, but I realised I was simply going one dictionary level at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  A list of dictionaries
&lt;/h2&gt;

&lt;p&gt;I can also combine dictionaries with lists.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;students = [&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Feddy", "age": 20},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Amina", "age": 21},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Brian", "age": 19}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I have a list containing three dictionaries.&lt;/p&gt;

&lt;p&gt;I can loop through them:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for student in students:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(student["name"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Feddy&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Amina&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Brian&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This structure started making a lot of sense to me because it looks much more like real data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dictionary comprehension
&lt;/h2&gt;

&lt;p&gt;Just like lists have comprehensions, dictionaries do too.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;squares = {number: number ** 2 for number in range(1, 6)}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I found this easier to understand after becoming comfortable with normal loops.&lt;/p&gt;

&lt;p&gt;The same idea is happening, but Python lets me write it in a shorter form.&lt;/p&gt;

&lt;h2&gt;
  
  
  Copying dictionaries
&lt;/h2&gt;

&lt;p&gt;Just like lists, I need to be careful when copying dictionaries.&lt;/p&gt;

&lt;p&gt;If I do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student2 = student&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;both variables refer to the same dictionary.&lt;/p&gt;

&lt;p&gt;So changing one can affect the other.&lt;/p&gt;

&lt;p&gt;To create a separate copy, I can use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student2 = student.copy()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now I can modify &lt;code&gt;student2&lt;/code&gt; without changing the original dictionary.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understood about dictionaries
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learnt is that dictionaries are really useful when the information needs labels.&lt;/p&gt;

&lt;p&gt;With a list, I might have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = ["Feddy", 20, "Data Science"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But now I have to remember that index &lt;code&gt;0&lt;/code&gt; is the name, index &lt;code&gt;1&lt;/code&gt; is the age, and index &lt;code&gt;2&lt;/code&gt; is the course.&lt;/p&gt;

&lt;p&gt;With a dictionary:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = {"name": "Feddy", "age": 20, "course": "Data Science"}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I don't have to remember positions.&lt;/p&gt;

&lt;p&gt;I just use the key I need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lists vs dictionaries
&lt;/h2&gt;

&lt;p&gt;This is how I now think about the difference.&lt;/p&gt;

&lt;p&gt;A list:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;products = ["Bread", "Milk", "Sugar"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is useful when I mainly care about the collection of items.&lt;/p&gt;

&lt;p&gt;A dictionary:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;product = {"name": "Bread", "price": 80, "quantity": 20}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is useful when I care about different pieces of information about one thing.&lt;/p&gt;

&lt;p&gt;And I can combine them:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;inventory = [&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Bread", "price": 80, "quantity": 20},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Milk", "price": 120, "quantity": 15},&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{"name": "Sugar", "price": 180, "quantity": 10}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That combination is something I expect to use a lot as I build bigger Python programs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Dictionaries took some getting used to because they are different from the position-based approach I was used to with lists.&lt;/p&gt;

&lt;p&gt;But once I understood the idea of keys and values, everything started falling into place.&lt;/p&gt;

&lt;p&gt;I can add information, update it, remove it, search for it, loop through it, and even put dictionaries inside other data structures.&lt;/p&gt;

&lt;p&gt;The biggest thing I took away is simple:&lt;/p&gt;

&lt;p&gt;A list helps me organise items.&lt;/p&gt;

&lt;p&gt;A dictionary helps me organise information about those items.&lt;/p&gt;

&lt;p&gt;That distinction has made choosing between the two much easier for me.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>webdev</category>
      <category>python</category>
    </item>
    <item>
      <title>Python Tuples: What I Learned About Fixed Data</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 13:28:19 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-tuples-what-i-learned-about-fixed-data-3ph9</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-tuples-what-i-learned-about-fixed-data-3ph9</guid>
      <description>&lt;p&gt;After learning lists, I came across tuples.&lt;/p&gt;

&lt;p&gt;At first, I honestly wondered why Python needed tuples when lists already existed.&lt;/p&gt;

&lt;p&gt;They looked almost the same.&lt;/p&gt;

&lt;p&gt;For example, a list looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;products = ["Bread", "Milk", "Sugar"]&lt;/code&gt;&lt;br&gt;
And a tuple looks like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;products = ("Bread", "Milk", "Sugar")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The big difference I eventually understood is that lists can be changed, while tuples cannot.&lt;/p&gt;

&lt;p&gt;That one idea made tuples much easier to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a tuple
&lt;/h2&gt;

&lt;p&gt;A tuple is created using parentheses:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fruits = ("Apple", "Mango", "Banana")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also create a tuple with numbers:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;numbers = (10, 20, 30, 40)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or different types of values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;person = ("Feddy", 20, True)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Just like lists, tuples can store multiple values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Accessing items
&lt;/h2&gt;

&lt;p&gt;Tuples use indexing just like lists.&lt;/p&gt;

&lt;p&gt;Python starts from &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fruits = ("Apple", "Mango", "Banana")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(fruits[0])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Apple&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also use negative indexing:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(fruits[-1])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Banana&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So the way I access tuple items is almost exactly the same as with lists.&lt;/p&gt;

&lt;h2&gt;
  
  
  The main difference: tuples cannot be changed
&lt;/h2&gt;

&lt;p&gt;This is where tuples become different.&lt;/p&gt;

&lt;p&gt;Suppose I have:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;coordinates = (10, 20)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can't do this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;coordinates[0] = 50&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Python will give me an error because tuples are immutable.&lt;/p&gt;

&lt;p&gt;That simply means once the tuple is created, I cannot change its individual values.&lt;/p&gt;

&lt;p&gt;This was the main thing that helped me understand when to use a tuple.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why would I want data that cannot change?
&lt;/h2&gt;

&lt;p&gt;At first, I thought being unable to change the data was a disadvantage.&lt;/p&gt;

&lt;p&gt;Then I started thinking about information that should stay fixed.&lt;/p&gt;

&lt;p&gt;For example, coordinates:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;location = (-1.286389, 36.817223)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or the days of the week:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I don't want my program accidentally changing these values.&lt;/p&gt;

&lt;p&gt;That's where tuples make sense.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tuple methods
&lt;/h2&gt;

&lt;p&gt;Tuples don't have as many methods as lists because they cannot be modified.&lt;/p&gt;

&lt;p&gt;The two main ones I use are &lt;code&gt;count()&lt;/code&gt; and &lt;code&gt;index()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;numbers = (10, 20, 20, 30, 20)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can count how many times 20 appears:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(numbers.count(20))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also find the position of an item:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(numbers.index(30))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's basically it for the most common tuple-specific methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking if an item exists
&lt;/h2&gt;

&lt;p&gt;Just like with lists, I can use &lt;code&gt;in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;days = ("Monday", "Tuesday", "Wednesday")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("Monday" in days)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;True&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can also use it in a condition:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if "Monday" in days:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print("It's a weekday")&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the length
&lt;/h2&gt;

&lt;p&gt;I can use &lt;code&gt;len()&lt;/code&gt; with tuples too.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;coordinates = (10, 20)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(len(coordinates))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So functions such as &lt;code&gt;len()&lt;/code&gt; work with both lists and tuples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Slicing tuples
&lt;/h2&gt;

&lt;p&gt;Tuples can also be sliced.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;numbers = (10, 20, 30, 40, 50)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(numbers[1:4])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(20, 30, 40)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Just like with lists, the starting index is included and the ending index is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looping through a tuple
&lt;/h2&gt;

&lt;p&gt;I can use a &lt;code&gt;for&lt;/code&gt; loop with a tuple.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;days = ("Monday", "Tuesday", "Wednesday")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;for day in days:&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(day)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Python goes through the tuple one item at a time.&lt;/p&gt;

&lt;p&gt;This is useful when I have a fixed collection of values that I still need to process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tuple unpacking
&lt;/h2&gt;

&lt;p&gt;This was one of the more interesting things I learnt about tuples.&lt;/p&gt;

&lt;p&gt;I can assign the values of a tuple to separate variables.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = ("Feddy", 20, "Data Science")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can write:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name, age, course = student&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name&lt;/code&gt; is &lt;code&gt;"Feddy"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;age&lt;/code&gt; is &lt;code&gt;20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;course&lt;/code&gt; is &lt;code&gt;"Data Science"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can then do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(name)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(age)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(course)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is called tuple unpacking.&lt;/p&gt;

&lt;p&gt;Once I understood it, I started noticing how useful it could be when working with multiple values at once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning multiple values from a function
&lt;/h2&gt;

&lt;p&gt;Tuple unpacking also makes sense when working with functions.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def get_student():&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;return "Feddy", 20, "Data Science"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can then do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;name, age, course = get_student()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Python returns the three values together, and I can unpack them into separate variables.&lt;/p&gt;

&lt;p&gt;I found this interesting because I don't have to create three separate variables inside the function just to return them.&lt;/p&gt;

&lt;h2&gt;
  
  
  A tuple with one item
&lt;/h2&gt;

&lt;p&gt;There is one small thing that caught me out.&lt;/p&gt;

&lt;p&gt;This:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;numbers = (10)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is not actually a tuple.&lt;/p&gt;

&lt;p&gt;Python treats it as a normal number.&lt;/p&gt;

&lt;p&gt;For a single-item tuple, I need a comma:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;numbers = (10,)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That comma is important.&lt;/p&gt;

&lt;p&gt;This is one of those small Python details I only really remembered after using it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tuples can contain mutable objects
&lt;/h2&gt;

&lt;p&gt;The tuple itself cannot be changed, but it can contain something that can be changed.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student = ("Feddy", ["Python", "SQL"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can't replace &lt;code&gt;"Feddy"&lt;/code&gt; with another name.&lt;/p&gt;

&lt;p&gt;But the list inside the tuple can be changed:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student[1].append("Power BI")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now the tuple contains:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;("Feddy", ["Python", "SQL", "Power BI"])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This was a good reminder that immutable doesn't always mean absolutely everything inside can never change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lists vs tuples
&lt;/h2&gt;

&lt;p&gt;This became much clearer to me when I stopped thinking about syntax and started thinking about the data.&lt;/p&gt;

&lt;p&gt;A list:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;products = ["Bread", "Milk", "Sugar"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A tuple:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;shop_location = ("Nairobi", "Kenya")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The products might change.&lt;/p&gt;

&lt;p&gt;The location probably shouldn't.&lt;/p&gt;

&lt;p&gt;So I started thinking about it like this:&lt;/p&gt;

&lt;p&gt;List → data I may need to modify.&lt;/p&gt;

&lt;p&gt;Tuple → data I want to keep fixed.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical example
&lt;/h2&gt;

&lt;p&gt;Imagine I'm building a small application for a shop.&lt;/p&gt;

&lt;p&gt;I could store the shop location as:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;shop_location = ("Nairobi", "Kenya")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I can access the city:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(shop_location[0])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And the country:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;print(shop_location[1])&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But I don't want another part of the program accidentally changing the location.&lt;/p&gt;

&lt;p&gt;For products, I would probably use a list instead:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;products = ["Bread", "Milk", "Sugar"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;because products can be added or removed.&lt;/p&gt;

&lt;p&gt;This helped me understand that choosing between a list and a tuple isn't about which one is better.&lt;/p&gt;

&lt;p&gt;It's about what I want the data to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I understood
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learnt about tuples is that they are useful for data that shouldn't be changed.&lt;/p&gt;

&lt;p&gt;I can still:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Access items&lt;/li&gt;
&lt;li&gt; Slice them&lt;/li&gt;
&lt;li&gt; Loop through them &lt;/li&gt;
&lt;li&gt; Check for values&lt;/li&gt;
&lt;li&gt; Count values&lt;/li&gt;
&lt;li&gt; Find positions&lt;/li&gt;
&lt;li&gt; Unpack values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I can't simply change one of the items after creating the tuple.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;At first, tuples felt unnecessary because they looked so similar to lists.&lt;/p&gt;

&lt;p&gt;But once I understood they are immutable , their purpose became much clearer.&lt;/p&gt;

&lt;p&gt;I don't reach for a tuple because it has more features than a list.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
    </item>
    <item>
      <title>Python Data Structures: Lists</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 13:18:59 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-data-structures-lists-3cep</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-data-structures-lists-3cep</guid>
      <description>&lt;p&gt;When I started learning Python, I quickly realised that storing one value at a time wasn't going to be enough.&lt;/p&gt;

&lt;p&gt;I needed a way to store multiple things together.&lt;/p&gt;

&lt;p&gt;That's where lists came in.&lt;/p&gt;

&lt;p&gt;At first, a list looked very simple:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar", "Soap"]&lt;/p&gt;

&lt;p&gt;But there is actually quite a lot you can do with a list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a list&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A list is created using square brackets [].&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;names = ["Feddy", "Amina", "Brian"]&lt;/p&gt;

&lt;p&gt;A list can contain numbers:&lt;/p&gt;

&lt;p&gt;prices = [80, 120, 250, 300]&lt;/p&gt;

&lt;p&gt;It can also contain different data types:&lt;/p&gt;

&lt;p&gt;student = ["Feddy", 20, True, 85.5]&lt;/p&gt;

&lt;p&gt;Although Python allows mixed data types, I usually keep a list focused on similar kinds of data because it makes the code easier to understand.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Indexing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the first things I learnt about lists was indexing.&lt;/p&gt;

&lt;p&gt;Python starts counting from 0.&lt;/p&gt;

&lt;p&gt;So:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar", "Soap"]&lt;/p&gt;

&lt;p&gt;print(products[0]) &lt;/p&gt;

&lt;p&gt;gives: Bread&lt;/p&gt;

&lt;p&gt;And: print(products[2])&lt;/p&gt;

&lt;p&gt;gives:Sugar&lt;/p&gt;

&lt;p&gt;This was confusing at first because I naturally wanted the first item to be position 1. Eventually, I got used to Python starting at 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Negative indexing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python also lets me access items from the end of the list.&lt;/p&gt;

&lt;p&gt;print(products[-1])&lt;/p&gt;

&lt;p&gt;gives:Soap&lt;/p&gt;

&lt;p&gt;And: print(products[-2])&lt;/p&gt;

&lt;p&gt;gives: Sugar&lt;/p&gt;

&lt;p&gt;I find this useful when I want the last item without having to count how many items are in the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Changing items&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lists are mutable.&lt;/p&gt;

&lt;p&gt;In simple terms, that means I can change them after creating them.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar"]&lt;/p&gt;

&lt;p&gt;products[1] = "Rice"&lt;/p&gt;

&lt;p&gt;Now the list becomes:&lt;/p&gt;

&lt;p&gt;["Bread", "Rice", "Sugar"]&lt;/p&gt;

&lt;p&gt;This was one of the first moments where I understood that lists aren't fixed. I can keep modifying them as my program runs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Adding items&lt;/strong&gt;&lt;br&gt;
There are a few ways to add items.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.append()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;append() adds one item to the end.&lt;/p&gt;

&lt;p&gt;products.append("Salt")&lt;/p&gt;

&lt;p&gt;If the list was:&lt;/p&gt;

&lt;p&gt;["Bread", "Milk", "Sugar"]&lt;/p&gt;

&lt;p&gt;it becomes:&lt;/p&gt;

&lt;p&gt;["Bread", "Milk", "Sugar", "Salt"]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. insert()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;insert() lets me add an item at a specific position.&lt;/p&gt;

&lt;p&gt;products.insert(1, "Flour")&lt;/p&gt;

&lt;p&gt;Now Flour is placed at index 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. extend()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;extend() is useful when I want to add several items.&lt;/p&gt;

&lt;p&gt;products.extend(["Tea", "Coffee"])&lt;/p&gt;

&lt;p&gt;Instead of adding the items one at a time, Python adds both to the list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Removing items&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can also remove items in different ways.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. remove()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;remove() removes a specific value.&lt;/p&gt;

&lt;p&gt;products.remove("Milk")&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. pop()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;pop() removes an item based on its index.&lt;/p&gt;

&lt;p&gt;products.pop(1)&lt;/p&gt;

&lt;p&gt;If I don't give an index, it removes the last item:&lt;/p&gt;

&lt;p&gt;products.pop()&lt;/p&gt;

&lt;p&gt;One useful thing about pop() is that it can give me back the item that was removed.&lt;/p&gt;

&lt;p&gt;removed_product = products.pop()&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. del&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can also use del:&lt;/p&gt;

&lt;p&gt;del products[0]&lt;/p&gt;

&lt;p&gt;This removes the item at index 0.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. clear()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And if I want to remove everything:&lt;/p&gt;

&lt;p&gt;products.clear()&lt;/p&gt;

&lt;p&gt;The list still exists, but it is now empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checking if an item exists&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can check whether an item is inside a list using in.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar"]&lt;/p&gt;

&lt;p&gt;print("Milk" in products)&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;True&lt;/p&gt;

&lt;p&gt;I can also use it in a condition:&lt;/p&gt;

&lt;p&gt;if "Milk" in products:&lt;/p&gt;

&lt;p&gt;print("Milk is available")&lt;/p&gt;

&lt;p&gt;This made a lot of sense to me when I started thinking about things like inventory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finding the length&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;len() tells me how many items are in a list.&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar", "Soap"]&lt;/p&gt;

&lt;p&gt;print(len(products))&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;

&lt;p&gt;This is useful when I need to know how many items I'm working with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Slicing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Slicing lets me take part of a list instead of the whole thing.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar", "Soap", "Salt"]&lt;/p&gt;

&lt;p&gt;print(products[1:4])&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;["Milk", "Sugar", "Soap"]&lt;/p&gt;

&lt;p&gt;The important thing I learnt here is that the starting index is included, but the ending index is not.&lt;/p&gt;

&lt;p&gt;I can also get the first three items:&lt;/p&gt;

&lt;p&gt;print(products[:3])&lt;/p&gt;

&lt;p&gt;Or everything from index 2 onward:&lt;/p&gt;

&lt;p&gt;print(products[2:])&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sorting a list&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can sort numbers:&lt;/p&gt;

&lt;p&gt;prices = [500, 200, 800, 300]&lt;/p&gt;

&lt;p&gt;prices.sort()&lt;/p&gt;

&lt;p&gt;Now I get:&lt;/p&gt;

&lt;p&gt;[200, 300, 500, 800]&lt;/p&gt;

&lt;p&gt;For descending order:&lt;/p&gt;

&lt;p&gt;prices.sort(reverse=True)&lt;/p&gt;

&lt;p&gt;I can also sort text alphabetically:&lt;/p&gt;

&lt;p&gt;names = ["Brian", "Amina", "Feddy"]&lt;/p&gt;

&lt;p&gt;names.sort()&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;["Amina", "Brian", "Feddy"]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sorted() vs sort()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This was something I had to pay attention to.&lt;/p&gt;

&lt;p&gt;sort() changes the original list.&lt;/p&gt;

&lt;p&gt;sorted() creates a new sorted result.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;prices = [500, 200, 800, 300]&lt;/p&gt;

&lt;p&gt;new_prices = sorted(prices)&lt;/p&gt;

&lt;p&gt;Now new_prices contains the sorted values, while prices remains unchanged.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reversing a list&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can reverse a list using reverse().&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar", "Soap"]&lt;/p&gt;

&lt;p&gt;products.reverse()&lt;/p&gt;

&lt;p&gt;Now the order becomes:&lt;/p&gt;

&lt;p&gt;["Soap", "Sugar", "Milk", "Bread"]&lt;/p&gt;

&lt;p&gt;This does not sort the list. It simply reverses the current order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Looping through a list&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lists and loops go together really well.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar"]&lt;/p&gt;

&lt;p&gt;for product in products:&lt;/p&gt;

&lt;p&gt;print(product)&lt;/p&gt;

&lt;p&gt;Python takes each item from the list one at a time.&lt;/p&gt;

&lt;p&gt;This became one of the most useful things for me because I could perform an action on every item without writing the same code repeatedly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;enumerate()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes I want both the item and its position.&lt;/p&gt;

&lt;p&gt;That's where enumerate() helps.&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar"]&lt;/p&gt;

&lt;p&gt;for number, product in enumerate(products, start=1):&lt;/p&gt;

&lt;p&gt;print(number, product)&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;1 Bread&lt;/p&gt;

&lt;p&gt;2 Milk&lt;/p&gt;

&lt;p&gt;3 Sugar&lt;/p&gt;

&lt;p&gt;This is useful when displaying numbered lists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List concatenation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can combine two lists using +.&lt;/p&gt;

&lt;p&gt;fruits = ["Apple", "Mango"]&lt;/p&gt;

&lt;p&gt;vegetables = ["Carrot", "Spinach"]&lt;/p&gt;

&lt;p&gt;items = fruits + vegetables&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;["Apple", "Mango", "Carrot", "Spinach"]&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repeating a list&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can also repeat a list using *.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;numbers = [1, 2]&lt;/p&gt;

&lt;p&gt;print(numbers * 3)&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;[1, 2, 1, 2, 1, 2]&lt;/p&gt;

&lt;p&gt;I don't use this as often, but it's useful to know.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finding an item's position&lt;/strong&gt;&lt;br&gt;
If I know the value but want to find its position, I can use index().&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar"]&lt;/p&gt;

&lt;p&gt;print(products.index("Sugar"))&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Counting items&lt;/strong&gt;&lt;br&gt;
count() tells me how many times a value appears.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;numbers = [1, 2, 2, 3, 2, 4]&lt;/p&gt;

&lt;p&gt;print(numbers.count(2))&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;This is useful when duplicate values matter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;List comprehensions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once I became comfortable with normal loops, I came across list comprehensions.&lt;/p&gt;

&lt;p&gt;They are a shorter way of creating lists.&lt;/p&gt;

&lt;p&gt;For example, I can create a list of squares:&lt;/p&gt;

&lt;p&gt;numbers = [1, 2, 3, 4, 5]&lt;/p&gt;

&lt;p&gt;squares = [number ** 2 for number in numbers]&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;[1, 4, 9, 16, 25]&lt;/p&gt;

&lt;p&gt;I wouldn't recommend trying to learn comprehensions before understanding normal loops. For me, they made more sense after I understood what the longer version was doing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Filtering with a list comprehension&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can also use a condition.&lt;/p&gt;

&lt;p&gt;For example, suppose I only want numbers greater than 10:&lt;/p&gt;

&lt;p&gt;numbers = [5, 12, 8, 20, 15]&lt;/p&gt;

&lt;p&gt;large_numbers = [number for number in numbers if number &amp;gt; 10]&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;[12, 20, 15]&lt;/p&gt;

&lt;p&gt;This became useful when I wanted to create a new list from existing data based on a condition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nested lists&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A list can also contain other lists.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;students = [["Feddy", 20], ["Amina", 21], ["Brian", 19]]&lt;/p&gt;

&lt;p&gt;I can access Brian's name with:&lt;/p&gt;

&lt;p&gt;print(students[2][0])&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;Brian&lt;/p&gt;

&lt;p&gt;Nested lists are possible, although when the data becomes more detailed, I often find dictionaries easier to work with.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Copying lists&lt;/strong&gt;&lt;br&gt;
One thing that can be confusing is copying a list.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar"]&lt;/p&gt;

&lt;p&gt;new_products = products&lt;/p&gt;

&lt;p&gt;Both variables now refer to the same list.&lt;/p&gt;

&lt;p&gt;So changing one can affect the other.&lt;/p&gt;

&lt;p&gt;A safer way to create a separate copy is:&lt;/p&gt;

&lt;p&gt;new_products = products.copy()&lt;/p&gt;

&lt;p&gt;Now I can change new_products without changing the original products.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A practical inventory example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where lists started feeling like something I could actually use in a program.&lt;/p&gt;

&lt;p&gt;Imagine I have a simple shop inventory:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar", "Soap"]&lt;/p&gt;

&lt;p&gt;I can check whether something is available:&lt;/p&gt;

&lt;p&gt;if "Milk" in products:&lt;/p&gt;

&lt;p&gt;print("Milk is available")&lt;/p&gt;

&lt;p&gt;I can add a new product:&lt;/p&gt;

&lt;p&gt;products.append("Salt")&lt;/p&gt;

&lt;p&gt;I can remove a product:&lt;/p&gt;

&lt;p&gt;products.remove("Soap")&lt;/p&gt;

&lt;p&gt;And display everything:&lt;/p&gt;

&lt;p&gt;for product in products:&lt;/p&gt;

&lt;p&gt;print(product)&lt;/p&gt;

&lt;p&gt;Now I'm not just learning list methods individually. I'm using them together to solve an actual problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood about lists&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The biggest thing I learnt about lists is that they are more than just a place to store multiple values.&lt;/p&gt;

&lt;p&gt;I can add items, remove them, change them, sort them, search through them, loop through them, and even create new lists from existing ones.&lt;/p&gt;

&lt;p&gt;The way I now think about a list is simple:&lt;/p&gt;

&lt;p&gt;If I have a collection of items that I want to keep together and possibly change later, a list is usually a good choice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lists were one of the first Python concepts that made me feel like I could actually build something useful.&lt;/p&gt;

&lt;p&gt;At first I only knew how to create a list and access an item.&lt;/p&gt;

&lt;p&gt;Then I learnt how to modify lists, loop through them, search them, sort them, and eventually use list comprehensions.&lt;/p&gt;

&lt;p&gt;What made the difference was not memorising every method.&lt;/p&gt;

&lt;p&gt;It was actually using lists in small programs and seeing why each feature exists.&lt;/p&gt;

&lt;p&gt;That's when lists stopped being just another Python topic and became something I naturally reach for when I need to store a collection of things.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>python</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Python Functions: How I Learned to Reuse My Code</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 12:31:40 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-functions-how-i-learned-to-reuse-my-code-35d1</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-functions-how-i-learned-to-reuse-my-code-35d1</guid>
      <description>&lt;p&gt;When I started writing more Python, I noticed I was repeating the same pieces of code.&lt;/p&gt;

&lt;p&gt;At first, I didn't think much of it. But after a while, I realised there had to be a better way.&lt;/p&gt;

&lt;p&gt;That's when functions started making sense to me.&lt;/p&gt;

&lt;p&gt;A function lets me write a piece of code once and use it whenever I need it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Defining a function&lt;/strong&gt;&lt;br&gt;
I create a function using def.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;def greet():&lt;/p&gt;

&lt;p&gt;print("Hello, welcome!")&lt;/p&gt;

&lt;p&gt;Nothing happens yet because I have only defined the function.&lt;/p&gt;

&lt;p&gt;To actually run it, I call it:&lt;/p&gt;

&lt;p&gt;greet()&lt;/p&gt;

&lt;p&gt;This was one of the first things I understood about functions: defining a function and calling a function are two different things.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parameters and arguments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Functions become more useful when I can give them information.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;def greet(name):&lt;/p&gt;

&lt;p&gt;print(f"Hello, {name}!")&lt;/p&gt;

&lt;p&gt;Now I can pass a name:&lt;/p&gt;

&lt;p&gt;greet("Feddy")&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;Hello, Feddy!&lt;/p&gt;

&lt;p&gt;The name inside the function is the parameter, while "Feddy" is the argument I pass to it.&lt;/p&gt;

&lt;p&gt;That distinction took me a little practice to remember.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Multiple parameters&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can also pass more than one value.&lt;/p&gt;

&lt;p&gt;For example, I can calculate the total cost of a product:&lt;/p&gt;

&lt;p&gt;def calculate_total(price, quantity):&lt;/p&gt;

&lt;p&gt;total = price * quantity&lt;/p&gt;

&lt;p&gt;print(total)&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;p&gt;calculate_total(500, 3)&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;1500&lt;/p&gt;

&lt;p&gt;This is much better than writing the same calculation every time I need it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Return values&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This was another important part for me.&lt;/p&gt;

&lt;p&gt;A function can return a value instead of just printing it.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;def calculate_total(price, quantity):&lt;/p&gt;

&lt;p&gt;return price * quantity&lt;/p&gt;

&lt;p&gt;Now I can store the result:&lt;/p&gt;

&lt;p&gt;total = calculate_total(500, 3)&lt;/p&gt;

&lt;p&gt;print(total)&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;1500&lt;/p&gt;

&lt;p&gt;The difference between print() and return became much clearer to me here.&lt;/p&gt;

&lt;p&gt;print() displays something.&lt;/p&gt;

&lt;p&gt;return sends a value back so I can use it somewhere else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scope&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Scope is basically about where a variable can be accessed.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;name = "Feddy"&lt;/p&gt;

&lt;p&gt;def greet():&lt;/p&gt;

&lt;p&gt;name = "Amina"&lt;/p&gt;

&lt;p&gt;print(name)&lt;/p&gt;

&lt;p&gt;greet()&lt;/p&gt;

&lt;p&gt;print(name)&lt;/p&gt;

&lt;p&gt;Inside the function, the result is:&lt;/p&gt;

&lt;p&gt;Amina&lt;/p&gt;

&lt;p&gt;Outside the function, the result is:&lt;/p&gt;

&lt;p&gt;Feddy&lt;/p&gt;

&lt;p&gt;The name created inside the function is local to that function.&lt;/p&gt;

&lt;p&gt;Understanding this helped me avoid accidentally changing or depending on variables in the wrong place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A practical example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One simple example I used was calculating a net salary.&lt;/p&gt;

&lt;p&gt;Suppose someone's salary is KSh 85,000 and their deductions are KSh 4,500.&lt;/p&gt;

&lt;p&gt;I can create:&lt;/p&gt;

&lt;p&gt;def calculate_net_salary(salary, deductions):&lt;/p&gt;

&lt;p&gt;return salary - deductions&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;p&gt;net_salary = calculate_net_salary(85000, 4500)&lt;/p&gt;

&lt;p&gt;print(f"Net salary: KSh {net_salary}")&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;Net salary: KSh 80500&lt;/p&gt;

&lt;p&gt;Now I can use the same function for different employees without rewriting the calculation.&lt;/p&gt;

&lt;p&gt;Why functions became useful to me&lt;/p&gt;

&lt;p&gt;Before functions, I was thinking:&lt;/p&gt;

&lt;p&gt;"I need to write this calculation again."&lt;/p&gt;

&lt;p&gt;After learning functions, I started thinking:&lt;/p&gt;

&lt;p&gt;"I've already written this once. I'll just call the function."&lt;/p&gt;

&lt;p&gt;That small change made my code much cleaner.&lt;/p&gt;

&lt;p&gt;What I understood&lt;/p&gt;

&lt;p&gt;The way I now think about functions is pretty simple:&lt;/p&gt;

&lt;p&gt;def → create the function&lt;/p&gt;

&lt;p&gt;Parameters → information the function expects&lt;/p&gt;

&lt;p&gt;Arguments → values I actually pass&lt;/p&gt;

&lt;p&gt;return → send a result back&lt;/p&gt;

&lt;p&gt;Scope → where variables can be accessed&lt;/p&gt;

&lt;p&gt;Once these pieces made sense, functions stopped feeling complicated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Functions were one of the first things that made me feel like I was actually organising my code rather than just writing lines of Python.&lt;/p&gt;

&lt;p&gt;They helped me avoid repetition, break bigger programs into smaller parts, and reuse code whenever I needed it.&lt;/p&gt;

&lt;p&gt;For me, the biggest lesson was simple: write it once, then reuse it.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>python</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Python Loops: How I Learned to Repeat Tasks Without Repeating Code</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 12:21:47 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-loops-how-i-learned-to-repeat-tasks-without-repeating-code-456p</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-loops-how-i-learned-to-repeat-tasks-without-repeating-code-456p</guid>
      <description>&lt;p&gt;One of the first things I noticed when writing Python programs was that I sometimes wanted to do the same thing several times.&lt;/p&gt;

&lt;p&gt;I could write the same line of code again and again, but that obviously wasn't a good way to program.&lt;/p&gt;

&lt;p&gt;That's where loops came in.&lt;/p&gt;

&lt;p&gt;The idea is simple, tell Python to repeat something for me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;for loops&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I use a for loop when I want to go through a collection of items or repeat something a specific number of times.&lt;/p&gt;

&lt;p&gt;For example, suppose I have some products:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz52n9q5zvhtih5vjhqy9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz52n9q5zvhtih5vjhqy9.png" alt=" " width="358" height="112"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of writing four separate print() statements, Python goes through the list for me.&lt;/p&gt;

&lt;p&gt;That was one of the first things that made loops click.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using range()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can also use range() when I want to repeat something a certain number of times.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsp5xyha2yu7g2bi2ay16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsp5xyha2yu7g2bi2ay16.png" alt="12" width="205" height="71"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This prints:&lt;/p&gt;

&lt;p&gt;1&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;3&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;

&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;So range(1, 6) gives me numbers starting from 1, but stops before 6.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;while loops&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A while loop works a little differently.&lt;/p&gt;

&lt;p&gt;It keeps running as long as a condition is true.&lt;/p&gt;

&lt;p&gt;For example, I can keep asking for a PIN until the correct one is entered:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9jjo2mv0fkscf3g3trat.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9jjo2mv0fkscf3g3trat.png" alt="12" width="263" height="137"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The loop keeps asking until the condition becomes false.&lt;/p&gt;

&lt;p&gt;This helped me understand that while is useful when I don't necessarily know how many times something needs to repeat.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;break&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes I want to stop a loop completely.&lt;/p&gt;

&lt;p&gt;That's what break does.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvqxh4am1p56ye7m7njic.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvqxh4am1p56ye7m7njic.png" alt="13" width="206" height="126"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The loop stops when it reaches 6.&lt;/p&gt;

&lt;p&gt;So even though the loop was supposed to go up to 10, break tells Python:&lt;/p&gt;

&lt;p&gt;"Stop here."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;continue&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;continue is different from break.&lt;/p&gt;

&lt;p&gt;Instead of stopping the whole loop, it skips the current iteration and moves to the next one.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm7ig5s7u1bv6ha92cp27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fm7ig5s7u1bv6ha92cp27.png" alt="14" width="231" height="137"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output is:&lt;/p&gt;

&lt;p&gt;1&lt;/p&gt;

&lt;p&gt;2&lt;/p&gt;

&lt;p&gt;4&lt;/p&gt;

&lt;p&gt;5&lt;/p&gt;

&lt;p&gt;The number 3 is skipped, but the loop continues.&lt;/p&gt;

&lt;p&gt;That's the difference I had to remember:&lt;/p&gt;

&lt;p&gt;break → stop the loop&lt;/p&gt;

&lt;p&gt;continue → skip this round&lt;/p&gt;

&lt;p&gt;enumerate()&lt;/p&gt;

&lt;p&gt;I found enumerate() useful when I wanted both the item and its position in a list.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftb6tmffh0oglrxa9k0bh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ftb6tmffh0oglrxa9k0bh.png" alt="15" width="366" height="107"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;1 Bread&lt;/p&gt;

&lt;p&gt;2 Milk&lt;/p&gt;

&lt;p&gt;3 Sugar&lt;/p&gt;

&lt;p&gt;Without enumerate(), I would have to create and manage the counter myself.&lt;/p&gt;

&lt;p&gt;A practical example&lt;/p&gt;

&lt;p&gt;I can put some of these ideas together in a small shopping program.&lt;/p&gt;

&lt;p&gt;Suppose I have a list of products and want to display them with numbers:&lt;/p&gt;

&lt;p&gt;products = ["Bread", "Milk", "Sugar", "Soap"]&lt;/p&gt;

&lt;p&gt;for number, product in enumerate(products, start=1):&lt;/p&gt;

&lt;p&gt;print(f"{number}. {product}")&lt;/p&gt;

&lt;p&gt;Now Python handles the numbering for me.&lt;/p&gt;

&lt;p&gt;This is much cleaner than manually writing:&lt;/p&gt;

&lt;p&gt;print("1. Bread")&lt;/p&gt;

&lt;p&gt;print("2. Milk")&lt;/p&gt;

&lt;p&gt;print("3. Sugar")&lt;/p&gt;

&lt;p&gt;print("4. Soap")&lt;/p&gt;

&lt;p&gt;What I understood&lt;/p&gt;

&lt;p&gt;The main thing that finally made loops make sense to me was thinking about the question:&lt;/p&gt;

&lt;p&gt;"Do I need to repeat something?"&lt;/p&gt;

&lt;p&gt;If I want to go through items in a list, I can use a for loop.&lt;/p&gt;

&lt;p&gt;If I want something to keep running while a condition is true, I can use a while loop.&lt;/p&gt;

&lt;p&gt;If I want to stop the loop, I use break.&lt;/p&gt;

&lt;p&gt;If I want to skip one iteration, I use continue.&lt;/p&gt;

&lt;p&gt;And if I need the position of each item, enumerate() makes it easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Loops seemed confusing at first because there was a lot of new syntax.&lt;/p&gt;

&lt;p&gt;But once I saw that their main purpose is simply to avoid repeating the same code manually, they became much easier.&lt;/p&gt;

&lt;p&gt;Instead of telling Python the same thing ten times, I can write it once and let the loop handle the repetition.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>productivity</category>
      <category>python</category>
    </item>
    <item>
      <title>Python Operators and Conditionals: How I Learned to Make Decisions</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 11:41:19 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-operators-and-conditionals-how-i-learned-to-make-decisions-465i</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-operators-and-conditionals-how-i-learned-to-make-decisions-465i</guid>
      <description>&lt;p&gt;After learning variables and basic Python syntax, I wanted my programs to actually make decisions.&lt;/p&gt;

&lt;p&gt;For example, I didn't want Python to just calculate a total. I wanted it to decide whether a customer qualified for free delivery.&lt;/p&gt;

&lt;p&gt;That's when operators and conditionals started making sense to me.&lt;/p&gt;

&lt;p&gt;Arithmetic operators&lt;/p&gt;

&lt;p&gt;Arithmetic operators are used for calculations.&lt;/p&gt;

&lt;p&gt;The basic ones are +, -, &lt;em&gt;, /, %, and *&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For example, I can calculate the total cost of a shopping order:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhaima01wbiik4t4dc3to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhaima01wbiik4t4dc3to.png" alt="8" width="191" height="119"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result is 1000.&lt;/p&gt;

&lt;p&gt;The % operator gives the remainder:&lt;/p&gt;

&lt;p&gt;print(10 % 3)&lt;/p&gt;

&lt;p&gt;The result is 1.&lt;/p&gt;

&lt;p&gt;This became useful when I wanted to check things like whether a number was even or odd.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Comparison operators&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Comparison operators allow me to compare values.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;age = 20&lt;/p&gt;

&lt;p&gt;print(age &amp;gt;= 18)&lt;/p&gt;

&lt;p&gt;The result is True.&lt;/p&gt;

&lt;p&gt;Python can compare values using operators such as ==, !=, &amp;gt;, &amp;lt;, &amp;gt;=, and &amp;lt;=.&lt;/p&gt;

&lt;p&gt;At first, I confused = with ==.&lt;/p&gt;

&lt;p&gt;I eventually understood that = assigns a value, while == checks whether two values are equal.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;age = 20&lt;/p&gt;

&lt;p&gt;age == 20&lt;/p&gt;

&lt;p&gt;The first line stores 20 in age.&lt;/p&gt;

&lt;p&gt;The second checks whether age is equal to 20.&lt;/p&gt;

&lt;p&gt;Logical operators&lt;/p&gt;

&lt;p&gt;Sometimes one condition isn't enough.&lt;/p&gt;

&lt;p&gt;That's where and, or, and not come in.&lt;/p&gt;

&lt;p&gt;For example, suppose I want to check whether someone can access a system:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fe0yah2kt5cg9l6fvzg4k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fe0yah2kt5cg9l6fvzg4k.png" alt="9" width="271" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Both conditions have to be true for access to be granted.&lt;/p&gt;

&lt;p&gt;That's how I started understanding and both conditions must be satisfied.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if, elif and else&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is where things became more interesting for me.&lt;/p&gt;

&lt;p&gt;if lets Python check a condition.&lt;/p&gt;

&lt;p&gt;elif gives it another condition to check.&lt;/p&gt;

&lt;p&gt;else handles everything that doesn't match the earlier conditions.&lt;/p&gt;

&lt;p&gt;For example, I can use a student's score:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3y106ig1ptkenmq1kf65.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3y106ig1ptkenmq1kf65.png" alt="10" width="245" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the score is 76, Python prints Pass.&lt;/p&gt;

&lt;p&gt;The important thing I understood here is that Python checks the conditions from top to bottom.&lt;/p&gt;

&lt;p&gt;A practical example&lt;/p&gt;

&lt;p&gt;One of the examples that helped me understand conditionals was a simple shopping system.&lt;/p&gt;

&lt;p&gt;I can give customers free delivery when they spend KSh 2,000 or more:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyi2zmps01g1sjf7lv5gc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fyi2zmps01g1sjf7lv5gc.png" alt="11" width="301" height="160"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the total is 2,500, the customer gets free delivery.&lt;/p&gt;

&lt;p&gt;But if:&lt;/p&gt;

&lt;p&gt;total = 1500&lt;/p&gt;

&lt;p&gt;the result becomes:&lt;/p&gt;

&lt;p&gt;Delivery fee: KSh 200&lt;/p&gt;

&lt;p&gt;This made conditionals feel much more useful because I could see how they could be used in an actual program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Combining conditions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I can also create more specific rules.&lt;/p&gt;

&lt;p&gt;For example, suppose a customer gets a discount if they spend at least KSh 5,000 and they are a member:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frglbqvxjgoy5acvskk2e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frglbqvxjgoy5acvskk2e.png" alt="11" width="292" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now Python is checking two things before making a decision.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The biggest thing I learnt was that operators are what help Python compare or calculate things, while conditionals use those results to make decisions.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;total = price * quantity&lt;/p&gt;

&lt;p&gt;This calculates something.&lt;/p&gt;

&lt;p&gt;Then:&lt;/p&gt;

&lt;p&gt;if total &amp;gt;= 2000:&lt;/p&gt;

&lt;p&gt;uses that result to make a decision.&lt;/p&gt;

&lt;p&gt;That connection is what finally made conditionals click for me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At first, operators looked like a bunch of symbols I just needed to memorise.&lt;/p&gt;

&lt;p&gt;Then conditionals looked like another syntax rule to learn.&lt;/p&gt;

&lt;p&gt;But once I started using them together in practical examples, I realised they are really just tools for telling Python:&lt;/p&gt;

&lt;p&gt;"Calculate this."&lt;/p&gt;

&lt;p&gt;"Compare that."&lt;/p&gt;

&lt;p&gt;"If this happens, do this."&lt;/p&gt;

&lt;p&gt;"Otherwise, do something else."&lt;/p&gt;

&lt;p&gt;That was the point where Python started feeling less like memorising code and more like actually programming.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>python</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Python Basics: What I Understood When I Started Learning Python</title>
      <dc:creator>Feddy Mwanjumwa</dc:creator>
      <pubDate>Sun, 04 Oct 2026 11:23:51 +0000</pubDate>
      <link>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-basics-what-i-understood-when-i-started-learning-python-2oj7</link>
      <guid>https://dev.to/feddy_mwanjumwa_e4047cf0c/python-basics-what-i-understood-when-i-started-learning-python-2oj7</guid>
      <description>&lt;p&gt;When I started learning Python, I had almost no idea what I was looking at.&lt;/p&gt;

&lt;p&gt;I would see things like variables, strings, integers, and input() and wonder why there were so many names for what seemed like simple things.&lt;/p&gt;

&lt;p&gt;The more I practiced, the more I realised that the basics are actually pretty straightforward. The important part was understanding what each thing was doing rather than just memorising the syntax.&lt;/p&gt;

&lt;p&gt;Here are some of the basics that finally started making sense to me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variables&lt;/strong&gt;&lt;br&gt;
The first thing I learnt was that variables are basically names used to store values.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhe4hxglwx0le568nrgiy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fhe4hxglwx0le568nrgiy.png" alt="1" width="243" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now I don't have to write those values again every time. I can just use the variable:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwij3s1xyavr45bi11loo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fwij3s1xyavr45bi11loo.png" alt="2" width="147" height="52"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The way I started thinking about variables was like labels attached to information. Once that made sense, they became much easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data types&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Then I came across data types.&lt;/p&gt;

&lt;p&gt;Python has different types of values, and the type matters because Python needs to know what kind of data it is working with.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;name = "Feddy"&lt;/p&gt;

&lt;p&gt;This is a string because it is text.&lt;/p&gt;

&lt;p&gt;age = 20&lt;/p&gt;

&lt;p&gt;This is an integer because it is a whole number.&lt;/p&gt;

&lt;p&gt;price = 1500.50&lt;/p&gt;

&lt;p&gt;This is a float because it has decimal places.&lt;/p&gt;

&lt;p&gt;is_student = True&lt;/p&gt;

&lt;p&gt;This is a boolean because it is either True or False.&lt;/p&gt;

&lt;p&gt;I can also check the type of a value using:&lt;/p&gt;

&lt;p&gt;print(type(age))&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strings&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A string is simply text.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;name = "Feddy Mwanjumwa"&lt;/p&gt;

&lt;p&gt;I can combine strings:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fai3xn7twvutqcqyxzdq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fai3xn7twvutqcqyxzdq2.png" alt="3" width="249" height="133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This gives:&lt;/p&gt;

&lt;p&gt;Feddy Mwanjumwa&lt;/p&gt;

&lt;p&gt;I started noticing that strings are everywhere, especially when working with names, messages, and other text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integers and floats&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Integers are whole numbers:&lt;/p&gt;

&lt;p&gt;age = 20&lt;/p&gt;

&lt;p&gt;quantity = 5&lt;/p&gt;

&lt;p&gt;Floats are numbers with decimal values:&lt;/p&gt;

&lt;p&gt;price = 250.50&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcodm36ijlkiviktdkr1b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcodm36ijlkiviktdkr1b.png" alt="4" width="196" height="119"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result is:&lt;/p&gt;

&lt;p&gt;751.5&lt;/p&gt;

&lt;p&gt;This made more sense once I started using numbers for realistic things like prices and quantities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input&lt;/strong&gt;&lt;br&gt;
One of the things that made Python feel more interactive was input().&lt;/p&gt;

&lt;p&gt;Instead of giving the program all the information myself, I can allow the user to enter it.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;name = input("Enter your name: ")&lt;/p&gt;

&lt;p&gt;print("Hello", name)&lt;/p&gt;

&lt;p&gt;Now the program waits for the user to enter a name.&lt;/p&gt;

&lt;p&gt;I can also ask for a number:&lt;/p&gt;

&lt;p&gt;age = int(input("Enter your age: "))&lt;/p&gt;

&lt;p&gt;The int() is important here because input() gives me text by default.&lt;/p&gt;

&lt;p&gt;That was something I had to learn through practice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For output, Python mainly uses print().&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fc3vbm0wzvz8wegzzx5xf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fc3vbm0wzvz8wegzzx5xf.png" alt="5" width="248" height="141"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I can also use an f-string:&lt;/p&gt;

&lt;p&gt;print(f"My name is {name} and I am {age} years old.")&lt;/p&gt;

&lt;p&gt;I found f-strings much cleaner once I got used to them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Python syntax&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Python syntax is basically the way Python expects me to write my instructions.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fazokvsfpjce1q1xfzl51.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fazokvsfpjce1q1xfzl51.png" alt="6" width="210" height="137"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python reads these instructions from top to bottom.&lt;/p&gt;

&lt;p&gt;One thing I quickly noticed is that Python uses indentation to show blocks of code.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;if age &amp;gt;= 18:&lt;/p&gt;

&lt;p&gt;print("Adult")&lt;/p&gt;

&lt;p&gt;The indentation is part of the syntax. It isn't just for making the code look neat.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A small practical example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once I understood the basics, I could put them together into something more realistic.&lt;/p&gt;

&lt;p&gt;For example, a simple shopping calculation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F03nssu82lcodywopmrrg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F03nssu82lcodywopmrrg.png" alt="6" width="293" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The result would be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9rt95jb4gmucqx9mvebj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9rt95jb4gmucqx9mvebj.png" alt="7" width="379" height="83"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is where the different concepts started connecting for me.&lt;/p&gt;

&lt;p&gt;I had a string for the product, numbers for the price and quantity, a variable storing the calculation, and print() displaying the result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood from the basics&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The biggest thing I learnt is that Python isn't really about memorising a huge number of commands.&lt;/p&gt;

&lt;p&gt;I started looking at it more like giving the computer instructions.&lt;/p&gt;

&lt;p&gt;Store something in a variable.&lt;/p&gt;

&lt;p&gt;name = "Feddy"&lt;/p&gt;

&lt;p&gt;Ask the user for information.&lt;/p&gt;

&lt;p&gt;age = int(input("Enter your age: "))&lt;/p&gt;

&lt;p&gt;Do something with the information.&lt;/p&gt;

&lt;p&gt;next_year = age + 1&lt;/p&gt;

&lt;p&gt;Show the result.&lt;/p&gt;

&lt;p&gt;print(next_year)&lt;/p&gt;

&lt;p&gt;Once I started seeing Python this way, the basics became much less intimidating.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These concepts may seem very basic now, but they were important for me because everything else in Python builds on them.&lt;/p&gt;

&lt;p&gt;Variables, data types, input, output, and syntax were the first pieces I needed to understand before moving on to things like conditionals, loops, functions, and data structures.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>programming</category>
      <category>productivity</category>
      <category>python</category>
    </item>
  </channel>
</rss>
