section: “cyber-shujaa” categories: [“cyber-shujaa”] title: “Python Basics” date: 2025-06-11

PYTHON BASICS

1.0  Introduction

This module focuses on the basics of python programming such as:

·        Variables

·        Loops

·        Functions

·        Data Structures

·        If statements

·        Files

By the end one will have enough knowledge about python to build simple applications and how to work with files.

1.2 Hello World

This module was mainly about how to use the print statements in python.

1.3 Mathematical Operators

Covered mathematical operators and their usage in python + - * /  %.

It also covered comparison operators such as:

1.4 Variables and Data Types

Variables allow you to store and update data in a computer program. You have a variable name and store data to that name.

Data Types, which is the type of data being stored in a variable. You can store text, or numbers, and many other types. The data types to know are:

  • String - Used for combinations of characters, such as letters or symbols
  • Integer - Whole numbers
  • Float - Numbers that contain decimal points or for fractions
  • Boolean - Used for data that is restricted to True or False options
  • List - Series of different data types stored in a collection

1.5 Logical and Boolean Operators

Logical operators allow assignment and comparisons to be made and are used in conditional testing (such as if statements).

Boolean operators are used to connect and compare relationships between statements. Like an if statement, conditions can be true or false.

1.6 Introduction to If Statements

Using “if statements” allows programs to make decisions. They let a program make a decision based on a condition if true or false.

1.7 Loops

In programming, loops allow programs to iterate and perform actions a number of times. There are two types of loops, for and while loops.

Question

On the code editor, click back on the “script.py” tab and code a loop that outputs every number from 0 to 50.

1.8 Introduction to Functions

A function is a block of code that can be called at different places in your program.

There are some key components we can note from this function:

·        The def keyword indicates the beginning of a function.

·        Following the function name is a pair of parentheses () that hold input values, data that we can pass into the function

·        A colon : marks the end of the function header.

1.9 Files

In Python, you can read and write from files.

To open the file, we use the built-in open() function, and the “r” parameter stands for “read” and is used as we’re reading the contents of the file. The variable has a read() method for reading the contents of the file. You can also use the readlines() method and loop over each line in the file; useful if you have a list where each item is on a new line. In the example above, the file is in the same folder as the Python script; if it were elsewhere, you would need to specify the full path of the file.

You can also create and write files. If you’re writing to an existing file, you open the file first and use the “a” in the open function after the filename call (which stands for append). If you’re writing to a new file, you use “w” (write) instead of “a”.

2.0 Imports

In Python, we can import libraries, which are a collection of files that contain functions. Think of importing a library as importing functions you can use that have been already written for you.

We import other libraries using the import keyword. Then in Python, we use that import’s library name to reference its functions. In the example above, we import datetime, then access the .now() method by calling library_name.method_name().

2.1 Conclusion

Python is versatile programming language and can be used in a lot of use cases from this module I was able to gain a lot of knowledge pertaining to different uses cases of python from loops to working with files and how to create conditional logic.