PYTHON BASICS
Introduction
Programming languages provide structured instructions to computers. Generally, programming languages are categorized based on the abstraction level and purpose. Essentially, programming languages are used to serve specific roles across the technology space. For the purposes of this article, the focus is python programming language, a High-Level, General-Purpose Programming Language.
As mentioned above, different programming languages serve specific purposes. However, as for Python, it a general- purpose language and a leader in Artificial Intelligence, Machine Learning, Data Analysis, and Web Scripting. This article explores some of the important aspects of Python programming, and explains its application in data analysis.
To understand python, and why it is one of the most used & recognized programming language, we will first focus on several important terms:
Object-oriented programming language — Focuses on objects that contain data and code. As for Python, it uses classes and objects to organize complex structure
High-Level Programming Language — Easy to read, write, and maintain. The main feature is that it is abstracted from hardware details, and thus is portable across different comp architectures. Python PL is designed to be readable and compact, which allows developers to write programs with fewer lines. Being interpreted and dynamic, the code is executed line-by-line.
General — purpose programming language — This basically refers to the fact that a programming language is not specialized for one task. Python has an extensive standard library and extremely supportive communities, which helps contribute to its versatility. This makes it possible for it to be suitable for various applications. Python can be used for:
- Web- applications development
- Artificial Intelligence (AI)
- Scientific computing
- Data analysis
- Software development
- Cyber security
- Automation & scripting, and so on
Python Basics
On this section, the focus will be on how to install Python on your computer, variables and data types, basic operators, loops, functions, lists, tuples, and dictionaries.
- Syntax — clear and easy to understand — resembling natural languages (High-Level aspect)
Example — print(“My first Python code”) — Compared to other programming languages, Python only uses a few lines of code to accomplish a task
- Indentation — Unlike other languages that use curly brackets ({}) to define a code block, Python uses indentation (4 spaces).
- Variables — Variables are created when you assign value to them — You do not need a command to declare them unless you want to change the type.
- Comments — We use # for single line comments
- Data types — includes integers, floats, strings, lists, tuples, and dictionaries
- Control Structures — Supports conditional statements (If, Else), Loops (For, While), and functions (def)
- Libraries — Python has extensive standard libraries and numerous third-party modules available via PYPI (PYthon Package Index).
Examples:
- NumPy — used for numerical computing
- Pandas — Used for data manipulation and analysis
- Matplotib — Used for data visualization
- Seaborn — Used for advanced visualization
- SciPy — used for scientific visualization
- Scikit-Learn — Used for machine learning
- Jupyter Notebook — Used for Interactive analysis
Installing Python
The first step is installing Python on your computer. As mentioned herein, Python works across all platforms. Many computers come with python preinstalled, which one can check using the following command line:
If Python is not pre-installed on your computer, you can download it for free from their official website.
Once you have installed python, you will need to write and execute Python code using an IDE (Integrated Development Environment) such as VS Code, Pycharm, jupyter notebook, Google colab, and so on.
Variables in Python
In computer programming, variables refer to a named storage location in a computer’s memory. It holds data that can be referenced and modified during execution of a program. It has a label, also referred to as the variable name or identifier, and contents, which are values or actual data stored inside the variable (number, text, list, etc.). It also has data type, which specifies the kind of data that the variable can hold (e.g. integers, strings, Booleans etc.).
Rules for naming variables in Python
- Must start with a letter (A-Z, a-z) or an underscore (_)
- Cannot start with a number
- Can only contain letters, numbers, and underscores
- Are case-sensitive — Name and name are different
- Python keywords cannot be used as variable names
PEP 8 — Python Naming conventions & Code Standards
There are certain rules that are designed to improve readability and consistency of Python code. The following are some of the naming guidelines for Python:
- Indentation — use 4 spaces per indentation level
- Maximum line length — a maximum of 79 characters
- Imports — Always at the top of the file in the following order
~ Standard library imports
~ Related third-party imports
~ Local application/library-specific imports
- Naming conventions
~ Functions and Variables — lowercase with underscores
Get Justuswagura’s stories in your inbox
Join Medium for free to get updates from this writer.
~ Classes — Capitalize Words e.e. CapWords
~ Constants — All caps with underscores
- Whitespaces — use spaces around operators — avoid extraneous whitespaces
- Blank lines — use only around top-level functions and class definitions
Why adhere to PEP 8?
It makes it easier for you and others to maintain and debug a code, months or years after it was written. Very vital in professional development environments and open-source contributions.
Data Types in Python
Data types classify items to determine what operations can be performed on them. They define the kind of data a variable can hold, and the operations that can be performed on it.
Numeric Data Types
A numeric data type holds a numeric value. It can be an integer, a floating number, as well as a complex number.
- Integers — contains positive or negative whole numbers
- Float — a real number with a floating-point representation — specified by a decimal point
- Complex number — represented by a real part and an imaginary part (e.g. 2+3j)
Sequence Data Types
This is an ordered collection of items, which can be of similar or different data types. They make it possible to store multiple values in an organized and efficient fashion
- Strings — Python strings are arrays of bytes representing Unicode characters. The Strings are create using single quotes or double quotes.
- List data type — similar to arrays in other programming languages. They are ordered and mutable collection of items. The items in a list need not be of the same data type. Lists are created by placing a sequence inside a square-brackets [].
- Tuple Data type — This is an ordered collection pf Python objects. Unlike lists, tuples are immutable: they cannot be modified. To modify a tuple, you convert it to string, modify it then convert it back to a tuple.
Tuples are created by placing a sequence of values separated by a ‘comma’ (,) with or without the use of parentheses for grouping data sequence. As it is the case with lists, Tuples can contain any number of elements and of any datatype (like strings, integers, lists, etc.).
Boolean Data Type
Python Boolean data types returns one of the two in-built values, True or False.
Dictionary Data Type
This is a collection of data values, that store information like a map. Python dictionaries holds key-value pairs, where each key maps to a value. This allows for efficient access and retrieval of data. Each key-value pair in a dictionary is separated by a full colon (:).
Python Input/Output
Python allows users to interact with it by taking input from them and displaying an output on the screen. The input() function takes unput from the user. Unless you define it, by default, the input from the user is stored as a string.
The print() is used to display values or messages — print “Hello, World!” returns the Output: Hello, World
Python Operators
These are symbols used to perform operations on values and variables, such as calculations, comparisons, and logical checks
- Arithmetic Operators — These perform basic mathematical operations, which include +, -, *, /, //, %, ** .
Python Loops
1. For loops in python iterate over a sequence such as a list, string or range of numbers.
Python Functions
Python functions are a set of re4usable code that performs a specific task. Instead of repeating the same thing several times, functions help make the code modular, reusable, readable as well as easier to debug.
Example
Python in Data Analytics
Data science is an interdisciplinary subject that incorporates everything necessary to create and prepare data, manipulate, filter and cleanse data, and to analyze data. It brings together all the techniques needed to extract and gain information and insight from data. It is an umbrella that incorporates data analysis, statistics, machine learning, and other related scientific fields for us to understand and analyze data. Using specialized libraries such as Pandas, Matplotlib, NumPy, Matplotlib, SciPy, Scikit-Learn and so on, we can streamline the entire data Lifecyle.
- Data Manipulation: Libraries such as Pandas allow for efficient data handling & analysis.
- Statistical Analysis: SciPy and StatsModels provide tools for statistical computations.
- Data Visualization: Matplotlib and Seaborn facilitate the creation of informative visual representations of data.
- Machine Learning: Scikit-learn and TensorFlow enable the development of predictive models and algorithms.
- Integration: Easily integrates with databases, web applications, and other programming languages.
Data Analysis Libraries
- Pandas — Uses DataFrames (tabular structures) to clean, filter, merge, and pivot datasets.
- NumPy: Provides high-performance support for large, multi-dimensional arrays and matrices, along with essential mathematical functions.
- Matplotlib & Seaborn: Used for creating static, animated, and interactive visualizations like line charts, histograms, and heatmaps.
- SciPy: Used for more advanced scientific computing, including optimization, integration, and statistical tests.
- Scikit-learn: Used for building predictive models and implementing machine learning algorithms
Conclusion
Python’s versatility and robust ecosystem make it a leading choice for data analytics. Also, its growing community and continuous development ensure ongoing relevance and innovation in the field. Thus, it is strongly recommended for both beginners and experienced professionals in data analytics.






