Skip to main content

Introduction to Programming and Python Day-1

Updated 11 min readMarkdown

1. What is Programming?

Programming is the process of giving instructions to a computer so that it can perform a task.

A computer is a powerful machine, but it cannot think or work by itself. It needs clear instructions from humans.

Programming is mainly about solving problems and getting the expected output from the computer.

Example tasks done by programming:

  1. Calculating marks

  2. Creating a bill

  3. Storing student records

  4. Making websites

  5. Automating repeated work


2. What is a Program?

A program is a set of instructions written to perform a specific task.

For example, if we want a computer to calculate the total price of products, we write instructions for it. Those instructions together are called a program.

In simple words:

Program = instructions given to the computer


3. What is a Programming Language?

A programming language is a language used to write programs.

Computers understand machine language, which is based on 0 and 1. But writing in 0 and 1 is very difficult for humans.

So, programming languages are used to write instructions in a human-friendly way.

Examples of programming languages:

  1. Python

  2. C

  3. C++

  4. Java

  5. JavaScript


4. Program, Programming, and Programming Language

Term Meaning
Program Set of instructions
Programming Process of writing instructions
Programming Language Language used to write instructions

Example:

A calculator app is a program.

Creating that calculator app is programming.

Using Python to create it means Python is the programming language.


5. Why Do We Need Programming?

We need programming to make computers perform tasks automatically, quickly, and accurately.

Without programming, computers cannot do useful work by themselves.

Programming is used to:

  1. Solve problems

  2. Save time

  3. Reduce human errors

  4. Automate repeated tasks

  5. Build software, apps, and websites


6. What is Programming Paradigm?

A programming paradigm is a style or method of writing programs.

It means how we organize and write our code to solve a problem.

Main programming paradigms are:

  1. Procedural Programming

  2. Object-Oriented Programming

  3. Functional Programming


7. Procedural Programming

Procedural programming is a style of programming where instructions are written step by step.

It focuses on procedures or functions.

In simple words:

Do this first, then do that, then show the result.

It is useful for simple and step-by-step programs.

Example tasks:

  1. Calculating total marks

  2. Finding simple interest

  3. Printing a bill


8. Object-Oriented Programming

Object-Oriented Programming, or OOP, is a style of programming based on objects.

An object represents a real-world thing.

Examples of objects:

  1. Student

  2. Car

  3. Bank account

  4. Product

  5. Customer

Each object has properties and actions.

For example, a student can have:

Properties:

  • Name

  • Age

  • Roll number

Actions:

  • Study

  • Attend class

  • Give exam

OOP is useful for organizing large programs.


9. Functional Programming

Functional programming is a style of programming where problems are solved mainly using functions.

A function takes input, processes it, and gives output.

In simple words:

Input → Function → Output

Functional programming is useful when we want clean and predictable results.


10. What is Python?

Python is a high-level, general-purpose programming language.

It is used to create different types of programs and applications.

Python is popular because it is simple, readable, and beginner-friendly.

Python was created by Guido van Rossum and first released in 1991.


11. Why Python?

Python is used because:

  1. Easy to learn
    Python is simple and readable, so beginners can understand it easily.

  2. Beginner-friendly
    Students can focus more on logic instead of complex syntax.

  3. Used in many fields
    Python is used in web development, automation, data analysis, AI, and software development.

  4. Large library support
    Python has many ready-made libraries that help developers work faster.

  5. Strong community support
    Python has a large community, so learning materials and solutions are easily available.


12. Introduction to Python

Python is a programming language used to give instructions to a computer.

It is called a high-level language because it is easier for humans to read and write.

Python is also an interpreted language, which means Python runs code line by line.

Python files usually have the extension:

.py

Example:

main.py

13. Features of Python

Main features of Python are:

  1. Simple and easy to read

  2. Free and open-source

  3. Works on Windows, Linux, and macOS

  4. Has many libraries

  5. Supports different programming styles


14. Where is Python Used?

Python is used in many areas, such as:

  1. Web development

  2. Data analysis

  3. Artificial intelligence

  4. Automation

  5. Software development


Memory Management

1. What is Memory Management?

Memory management means how a programming language stores, uses, and removes data from computer memory while a program is running.

When we create variables, objects, lists, strings, or functions, they need space in memory.

Python manages most of this memory automatically, so the programmer does not need to manually allocate and delete memory like in some other languages.

2. Why Memory Management is Needed?

Memory management is needed because:

  1. Programs need memory to store data.

  2. Unused data should be removed from memory.

  3. Memory should be used efficiently.

  4. Programs should not become slow because of unnecessary memory usage.

  5. It helps avoid memory-related errors.

3. Stack Memory

Stack memory is used to store temporary information during program execution.

It mainly stores:

  1. Function calls

  2. Local variables

  3. References to objects

Stack works in a last in, first out order.

That means the function that is called last will finish first.

Example idea:

Function starts → memory is added to stack
Function ends → memory is removed from stack

Stack memory is fast and temporary.

4. Heap Memory

Heap memory is used to store objects and data created during program execution.

In Python, actual objects are stored in heap memory.

Examples of objects stored in heap:

  1. Numbers

  2. Strings

  3. Lists

  4. Dictionaries

  5. Custom objects

Heap memory is larger than stack memory and is used for data that may be needed for a longer time.

5. Garbage Collection

Garbage collection is the process of removing unused objects from memory.

When an object is no longer needed and no variable refers to it, Python automatically removes it from memory.

Example:

a = [1, 2, 3]
a = "Hello"

At first, a refers to the list [1, 2, 3].

Later, a refers to "Hello".

Now, if no other variable refers to [1, 2, 3], Python can remove that list from memory.

This automatic cleaning process is called garbage collection.

Python Installation and Virtual Environment Setup

1. Installing Python

Python must be installed before we can write and run Python programs.

To check whether Python is already installed, open terminal or command prompt and type:

python --version

or

python3 --version

If Python is installed, it will show the version.

Example:

Python 3.x.x

2. Installing Python on Windows

Step 1: Download Python

Go to the official Python website and download Python for Windows.

Step 2: Run the installer

Open the downloaded installer.

Very important:

Tick/Add: Add Python to PATH

Then click:

Install Now

Step 3: Check installation

Open Command Prompt and type:

python --version

Also check pip:

pip --version

pip is used to install Python packages.


3. Installing Python on macOS

macOS may already have Python installed, but it is better to install the latest Python 3 version.

Option 1: Install from official website

Download Python for macOS from the official Python website and install it.

Then check:

python3 --version

Check pip:

pip3 --version

Option 2: Install using Homebrew

If Homebrew is installed, use:

brew install python

Then check:

python3 --version

4. Installing Python on Linux

Most Linux systems already have Python installed.

Check using:

python3 --version

If Python is not installed, install it using the package manager.

Ubuntu/Debian

sudo apt update
sudo apt install python3 python3-pip python3-venv

Check installation:

python3 --version
pip3 --version

5. What is pip?

pip is the package manager for Python.

It is used to install external Python packages.

Example:

pip install django

or on macOS/Linux:

pip3 install django

Packages are ready-made tools or libraries that help us build projects faster.


6. What is a Virtual Environment?

A virtual environment is a separate workspace for a Python project.

It allows each project to have its own packages and dependencies.

In simple words:

Virtual environment = separate Python setup for one project

7. Why Do We Need a Virtual Environment?

We need a virtual environment because different projects may need different package versions.

Example:

Project A needs Django 4
Project B needs Django 5

If we install everything globally, there may be conflicts.

A virtual environment solves this problem by keeping packages separate for each project.


8. Benefits of Virtual Environment

  1. Keeps each project separate

  2. Avoids package version conflicts

  3. Keeps the main system clean

  4. Makes projects easier to share

  5. It is a professional Python practice


9. Creating a Project Folder

First, create a folder for your project.

Windows

mkdir my_project
cd my_project

macOS/Linux

mkdir my_project
cd my_project

10. Creating Virtual Environment

Windows

python -m venv venv

macOS/Linux

python3 -m venv venv

Here:

  • python -m venv means create a virtual environment.

  • venv is the name of the virtual environment folder.

You can use another name, but venv is commonly used.


11. Activating Virtual Environment

After creating the virtual environment, we need to activate it.

Windows

venv\Scripts\activate

macOS/Linux

source venv/bin/activate

After activation, you will see:

(venv)

Example:

(venv) my_project>

This means the virtual environment is active.


12. Installing Packages Inside Virtual Environment

After activating the virtual environment, install packages normally.

Example:

pip install requests

or:

pip install django

These packages will be installed only inside that project's virtual environment.

They will not affect other projects.


13. Checking Installed Packages

To see installed packages:

pip list

This shows the packages installed inside the active virtual environment.


14. Saving Packages

To save installed packages in a file:

pip freeze > requirements.txt

This creates a file named:

requirements.txt

It contains the list of packages used in the project.


15. Installing Packages from requirements.txt

When someone shares a Python project, they usually share a requirements.txt file.

To install all required packages:

pip install -r requirements.txt

16. Deactivating Virtual Environment

To close the virtual environment:

deactivate

After this, (venv) disappears from the terminal.


17. Common Python Project Setup Flow

Windows

mkdir my_project
cd my_project
python -m venv venv
venv\Scripts\activate
pip install package_name
pip freeze > requirements.txt
deactivate

macOS/Linux

mkdir my_project
cd my_project
python3 -m venv venv
source venv/bin/activate
pip install package_name
pip freeze > requirements.txt
deactivate

First Code in Python

After installing Python and setting up the virtual environment, the first program we usually write is:

print("Hello, World!")

163 views

No comments yet. Be the first to comment.