DEV Community

Cover image for Python 101: Your Complete Beginner's Guide from Setup to Running Programs
David Gitonga
David Gitonga

Posted on

Python 101: Your Complete Beginner's Guide from Setup to Running Programs

Introduction

Starting Python as a beginner can feel overwhelming, especially when you encounter unfamiliar terms such as variables, data types, dictionaries, operators and string methods. At first, it may seem like programming is about memorising commands and writing complicated lines of code. However, as I have discovered during this week's Python practice, learning becomes easier when you understand what each instruction does and why you are using it.

Python is a programming language widely used in areas such as data analytics, artificial intelligence, automation and software development. One of the things that makes it beginner-friendly is its readable syntax. You can write instructions that are relatively close to ordinary English, making it easier to understand what your program is doing.

In this guide, I will share my findings as I explore Python fundamentals, set up my development environment and build simple programs. The practical examples include a vegetable shop assistant and an M-Pesa transaction message reader.

1. Setting up your Python environment

Before writing Python programs, you need a suitable development environment.

The first step is installing Python from the official website: https://www.python.org/downloads/.

After installation, you need a code editor. I use Visual Studio Code (VS Code), which provides an environment for writing, editing and running Python programs.

To prepare VS Code:

  1. Download and install VS Code.
  2. Open the Extensions section.
  3. Search for and install the Python extension by Microsoft.
  4. Create a folder for your Python projects.
  5. Open that folder in VS Code.
  6. Create a new file with the .py extension.

You can confirm that Python is installed by opening the VS Code terminal and running:

python --version

If Python is correctly installed and available in your system path, the command should display its installed version.

2. Understanding Python files: .py and .ipynb

During my learning, I encountered two common ways of working with Python: Python scripts and Jupyter Notebooks.

A .py file contains Python instructions that can be executed as a script. For example, I created shop.py for my vegetable shop assistant and practice.py for arithmetic and string manipulation exercises.

An .ipynb file is a Jupyter Notebook. Unlike a normal Python script, it allows you to divide your code into separate cells and execute each cell independently.

This makes notebooks useful when experimenting, testing individual commands and exploring data.

The main difference is how you execute your code. A Python script normally runs from beginning to end, while a notebook allows you to execute selected cells.

Both use Python. The choice depends on the type of work you are doing.

3. Understanding variables and data types

One of the first concepts I learned was variables.

A variable is a named location used to store a value that can be accessed later in a program.

For example, in my vegetable shop program, I stored the prices of different products in variables.

sukuma = 20
tomatoes = 10
onions = 15
potatoes = 30
carrots = 25
Enter fullscreen mode Exit fullscreen mode

Instead of repeatedly typing the prices, I can refer to them using their variable names.

Python also works with different data types. Some of the fundamental types include:

  • Integer (int): Whole numbers such as 20, 100 and 500.
  • Float (float): Decimal numbers such as 12.50 and 3.14.
  • String (str): Text enclosed in quotation marks.
  • Boolean (bool): Logical values, either True or False.

Understanding data types is important because Python handles different kinds of information differently. For example, you cannot perform ordinary numerical addition on a string without first converting it into a suitable numeric type.

4. Input and output in Python

A program becomes more useful when it can receive information from the user and respond to it.

Python uses print() to display information and input() to collect information.

For example:

customer_name = input("What is your name? ")
print(f"Karibu, {customer_name}!")
Enter fullscreen mode Exit fullscreen mode

The input() function waits for the user to enter information. That information is stored in the variable customer_name.

The print() function displays the greeting.

I also learned about f-strings. By placing the letter f before a string, I can insert variables directly into the text using curly brackets.

This makes displaying information easier and more readable.

One important discovery is that input() returns a string by default. If I want to collect a quantity and perform calculations with it, I need to convert it into an integer using int().

5. Understanding dictionaries

Dictionaries provide a way of storing related information using key-value pairs.

For example, instead of creating separate variables for every vegetable price, I can organise them into one dictionary.

prices = {
    "sukuma": 20,
    "tomatoes": 10,
    "onions": 15,
    "potatoes": 30,
    "carrots": 25
}
Enter fullscreen mode Exit fullscreen mode

Here, the vegetable names are keys, while their prices are values.

I can retrieve a price by using its key:

price = prices["sukuma"]
print(price)
Enter fullscreen mode Exit fullscreen mode

The result is 20.

This was particularly useful when developing the Mama Mboga Shop Assistant. A customer can enter the name of a vegetable, and the program can use that name to retrieve its price from the dictionary.

Dictionaries help organise related information without creating too many individual variables.

6. Python arithmetic operators

Arithmetic operators allow Python to perform mathematical calculations.

During the assignment, I worked with six operators:

Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
// Floor division
% Modulus or remainder
** Exponentiation

For example, if a boda boda trip costs KES 150 and a customer makes three trips:

trip_cost = 150
number_of_trips = 3

total = trip_cost * number_of_trips
print(total)
Enter fullscreen mode Exit fullscreen mode

The result is 450.

I also learned that / and // behave differently. Normal division can produce a decimal, while floor division returns the result rounded down to the nearest whole number.

The modulus operator % returns the remainder after division. This is useful when calculating how many items remain after packing them into groups.

Another important concept is operator precedence. Python follows mathematical rules when deciding which operation to perform first. Parentheses can be used to control the order of calculations.

7. Working with strings and string methods

Strings are used to store text. However, working with text involves more than simply displaying it.

Sometimes information contains unnecessary spaces, inconsistent capitalisation or text that needs to be extracted.

Python provides several built-in string methods to handle these situations.

Some of the methods I practised include:

Method Purpose
.strip() Removes spaces from the beginning and end
.title() Capitalises the beginning of each word
.lower() Converts text to lowercase
.find() Finds the position of a substring
.count() Counts occurrences of text
.replace() Replaces one piece of text with another
.split() Divides a string into a list
.join() Combines strings
.isdigit() Checks whether characters are digits
.endswith() Checks how a string ends

For example, consider a name with inconsistent capitalisation and extra spaces:

name = " kAMAU wanjiru "

clean_name = name.strip().title()
print(clean_name)
Enter fullscreen mode Exit fullscreen mode

The result is:

Kamau Wanjiru

This demonstrates how two methods can be chained together to clean text in a single instruction.

I also learned about string slicing. Slicing allows you to extract a section of a string using its positions.

For example:

email = "amina.otieno@luxdevhq.com"

position = email.find("@")
username = email[:position]
Enter fullscreen mode Exit fullscreen mode

The variable username now contains amina.otieno.

This is useful when working with emails, names, phone numbers and other text-based information.

8. Mini project: M-Pesa transaction message reader

One of the most practical exercises this week was developing an M-Pesa message reader.

The idea was to take an ordinary transaction confirmation message and extract useful information from it.

The message contained a transaction code, amount sent, recipient's name, new account balance and transaction cost.

Instead of manually reading the entire message, I used string methods and slicing to extract the required information.

The process involved several steps:

  1. Finding the transaction code by locating the first space.
  2. Finding the amount by locating the Ksh label and the word sent.
  3. Removing the comma from the amount.
  4. Converting the extracted amount into a number using float().
  5. Extracting the recipient's name using identifiable text markers.
  6. Extracting the new balance and transaction cost.
  7. Calculating the previous balance and transaction cost percentage.
  8. Displaying everything in a readable summary.

For example, the previous balance can be calculated using:

before = balance + amount + cost
Enter fullscreen mode Exit fullscreen mode

The transaction cost percentage can be calculated using:

percentage = round((cost / amount) * 100, 2)
Enter fullscreen mode Exit fullscreen mode

This project helped me understand that a string can contain several pieces of information that can be separated, cleaned and converted into useful data.

I also explored using input() so that the user could paste a transaction message instead of relying on a message permanently stored in the program.

The exercise demonstrated an important principle: data can begin as unstructured text and be transformed into information that is easier to understand and analyse.

9. Challenges and lessons learned

One of my main challenges was understanding how Python processes information, especially when working with strings and converting data types.

I also learned that assigning a new value to a variable does not automatically repeat the operations that were previously performed on it. For example, collecting a new M-Pesa message at the bottom of a script does not automatically rerun the extraction and calculation instructions above it.

The input must be collected before the processing instructions execute.

Another important lesson was understanding errors rather than simply trying random changes. An error message often points to the line where something went wrong and provides information that helps identify the problem.

Most importantly, I discovered that understanding the logic behind a program is more valuable than memorising its code.

10. Conclusion

This week's Python practice introduced me to the foundations of programming, from variables and data types to dictionaries, arithmetic operators and string manipulation.

The two practical exercises helped me apply these concepts to everyday situations. The vegetable shop assistant demonstrated how Python can receive input, retrieve information and calculate totals. The M-Pesa reader demonstrated how text can be extracted, cleaned and transformed into useful information.

As I continue learning Python, my next goal is to become more confident in writing programs independently, understanding errors and eventually using Python for data analytics and automation.

Explore the practical code and screenshots in my GitHub repository:

[https://github.com/gitosh-star/PYTHON-ACTION]

Top comments (0)