Introduction to Python
Course: Introduction to Programming
(BT101CO)
Introduction
● Python is a high-level, general-purpose programming language.
● Created by Guido van Rossum, and released in 1991.
● Emphasizes code readability with the use of significant indentation.
● simple to use, packed with features and supported by a wide range of libraries and frameworks.
● Beginner-friendly.
● Python's core philosophy is summarized in the Zen of Python (PEP 20) written by Tim Peters,
which includes aphorisms such as these:
○ Explicit is better than implicit. —> Make your code clear about what it’s doing
○ Simple is better than complex. —> Prefer solutions that are straightforward. Add complexity only when absolutely
necessary.
○ Readability counts. —>Write code others can easily understand at a glance.
○ Special cases aren't special enough to break the rules. —> Even unusual situations shouldn’t encourage you to bypass
good coding principles.
○ Although practicality beats purity. —> sometimes, real-world needs require bending rules — just don’t overdo it.
○ Errors should never pass silently. —>Don’t ignore errors. Let them raise exceptions.
○ Unless explicitly silenced. —> if you must silence errors, do it intentionally (e.g., using try/except).
○ There should be one-- and preferably only one --obvious way to do it. —> Python tries to avoid giving too many ways
to do the same thing. Code should guide you toward the clear solution.
Reason for Python’s Popularity
● Easy to Learn and Read.
○ Fewer symbols
○ Simple structure
○ Very beginner-friendly
● Extremely Versatile - can be used in almost every field.
○ Web development (Django, Flask)
○ Data science & machine learning (NumPy, Pandas, TensorFlow, PyTorch)
○ Automation & scripting
○ Scientific computing
● Abundance of Libraries and Frameworks:
○ Extensive collection of modules and packages covers everything from file I/O to regular expressions.
● Data Science and Analytics:
○ Python is synonymous with data science.
○ Libraries like NumPy, Pandas, and Matplotlib provide powerful tools for data manipulation, analysis, and
visualization.
● Machine Learning and AI:
○ Python is the language of choice for many machine learning practitioners.
○ TensorFlow and PyTorch, two prominent machine learning frameworks, are both Python-based,
solidifying its position in the AI landscape.
History of Python
● Creation and Early Development (Late 1980s – Early 1990s)
○ Creator: Guido van Rossum, a Dutch programmer.
○ Year Started: 1989 (Christmas holidays!)
○ Motivation:
■ Guido wanted a language that was easy to read, simple, and expressive.
■ Influenced by ABC language (which he worked on) and Modula-3.
○ First Public Release: Python 0.9.0 in February 1991
○ Already had:
■ Functions
■ Exception handling
■ Core data types (str, list, dict)
■ Modules (a file containing Python definitions and statements.)
● Python 1.x (1991 – 2000)
○ includes several releases, starting with Python 1.0 and culminating in Python 1.6.
○ Python 1.0 released: January 1994
○ Key features:
■ Functions, modules, exceptions
■ Basic standard library
■ ‘map’, ‘filter’, ‘reduce’ functions
○ Python slowly gained popularity in academia and research.
● Python 2.x (2000 – 2010)
○ Python 2.0 released: October 2000
○ Key features:
■ List comprehensions
■ Garbage collection system
■ Unicode support (limited in early versions)
○ Python 2.7 (final 2.x release) in 2010
○ Popularity exploded for:
■ Web development (Django, Flask)
■ Scripting & automation
■ Data processing
○ Python 2 reached end-of-life: January 1, 2020
● Python 3.x (2008 – Present)
● Python 3.0 released: December 2008
● Major redesign to fix inconsistencies in Python 2
● Key changes:
○ Print became a function: print()
○ Strings are Unicode by default
○ Integer division returns float by default
○ Removal of old modules / backward-incompatible changes
● Python 3.10 / 3.11 (2022–2023)
○ Modern features:
■ Pattern matching (match-case)
■ Faster execution (Python 3.11)
■ Better type hinting, async features
● Python 3.14
○ Performance optimizations
○ Improved error messages
● Current Popularity: #1 language for data science, AI/ML, web development,
automation, and education
Downloading and Installing Python
● Download Python
○ Open browser and visit [Link]/downloads.
○ Select the latest Python 3 version.
○ Click on the version to download
● Run the installer
○ Windows:
■ Open the downloaded .exe file.
■ Important: Check the box “Add Python to PATH”.
● This ensures you can run Python from the command line.
■ Click Install Now.
■ Wait for installation to complete and click Close.
○ Mac
■ Open the downloaded .pkg file.
■ Follow the installer prompts.
■ Python will be installed in /usr/local/bin/python3
○ Linux:
■ Python usually comes pre-installed on most distributions.
■ To install/update:
■ sudo apt update
■ sudo apt install python3
● Verify Installation
○ Open Command Prompt or Terminal.
○ Type:
■ python --version OR python3 --version
● Install pip (Python package manager)
○ Pip usually comes with modern Python versions.
○ Check by typing: pip --version
○ If it’s missing, download python [Link] from [Link]
○ Run:
■ python [Link]
● Test Python
○ Open Python interactive shell: python
○ Type: print("Hello, Python!")
○ If it prints Hello, Python!, everything works perfectly.
Running a python file
1. From the Command Line (Terminal/Command Prompt)
a. Save your Python code: Create a file with a .py extension (e.g., my_script.py).
b. Open your terminal or command prompt.
c. Navigate to the directory: where your Python script is saved using the cd command (e.g., cd
C:\Users\YourUser\Documents\PythonScripts).
d. Execute the script: Type python my_script.py and press Enter. On Windows, you might also use
py my_script.py.
2. Using an Integrated Development Environment (IDE) or Code Editor:
a. Install a suitable IDE or code editor: Popular choices include VS Code, PyCharm, or even
Python's built-in IDLE.
b. Open your Python file: within the editor.
c. Most editors provide a "Run" button or option: (often a play icon) to execute the current script.
For example, in VS Code with the Python extension, you can select "Run Python File in
Terminal."
Integrated Development Environment
● software application that provides a comprehensive set of tools to help
developers write, compile, and debug code more efficiently.
● combines essential functions like a text editor, build automation tools, and a
debugger into a single graphical user interface (GUI), streamlining the entire
software development workflow.
● increase programmer productivity by consolidating these tools into one cohesive
environment.
● Examples: PyCharm, Visual Studio Code, JupyterLab and so on.
Python Documentation
● refers to the comprehensive collection of resources and information that
describes the Python programming language, its features, libraries, and how to
use them.
● Provides guidance for developers to understand and effectively utilize python.
● Official documentation: [Link]
Comparing Python
● Python, a high-level, interpreted programming language, offers distinct characteristics
when compared to other languages.
● Simplicity and Readability:
○ Python's syntax emphasizes readability, often requiring fewer lines of code than languages like Java or
C++ for similar tasks. This makes it easier to learn and maintain.
● Versatility and Application Domains:
○ Python is highly versatile, excelling in web development (Django, Flask), data science and machine
learning (NumPy, pandas, scikit-learn, TensorFlow), automation, scripting, and scientific computing.
○ Languages like JavaScript primarily focus on front-end web development, while R is specialized for
statistical computing.
● Performance:
○ As an interpreted language, Python can be slower than compiled languages like C++ or Java for
computationally intensive tasks.
○ However, Python's extensive libraries, many of which are written in C or C++, can mitigate
performance concerns in specific areas.
● Dynamic Typing vs. Static Typing:
○ Python is dynamically typed, meaning variable types are checked at runtime. This offers flexibility but
can potentially introduce runtime errors.
○ Statically typed languages like Java or C++ perform type checking during compilation, catching errors
earlier in the development cycle.
Specific Comparisons
● Python vs. Java:
○ Python offers faster development due to conciseness, while Java excels in performance and
enterprise-level application scalability.
● Python vs. C++:
○ Python prioritizes rapid development and ease of use, while C++ provides low-level
control and superior performance for resource-intensive applications.
● Python vs. JavaScript:
○ Python is widely used for back-end, data science, and AI, while JavaScript dominates
front-end web development.
● Python vs. Ruby:
○ Both offer ease of use, but Python has a stronger foothold in scientific computing and
machine learning, while Ruby, particularly with Rails, is popular for web development.
● Python vs Rust:
○ Rust excels in scenarios demanding high performance, memory safety, and efficient
concurrency, often at the cost of a steeper learning curve and slower initial development
whereas.
○ Python prioritizes ease of use, rapid development, and a rich ecosystem, making it a
strong choice for applications where these factors are more critical than raw performance.
Programming Foundations
Algorithms
● An algorithm is a well-defined, step-by-step procedure or set of instructions used to
solve a specific computational problem or accomplish a particular task.
● Algorithms can be expressed in various forms, including:
○ Plain English or natural language: A descriptive, step-by-step explanation of the process.
○ Pseudocode: A high-level description of an algorithm that combines elements of natural language with
programming language-like constructs, but is not executable code.
○ Flowcharts: Diagrammatic representations that use symbols and arrows to illustrate the flow of control
and operations.
● Key characteristics:
○ Finiteness:
■ An algorithm must terminate after a finite number of steps.
○ Definiteness:
■ Each step in an algorithm must be clearly and unambiguously defined, leaving no room for
interpretation.
○ Input:
■ An algorithm takes zero or more inputs, which are the quantities supplied to it initially.
○ Output:
■ An algorithm produces one or more outputs, which are the results of the computation.
○ Effectiveness:
■ Each step of an algorithm must be sufficiently basic that it can be carried out, at least in principle,
by a human using only pencil and paper.
Example:
Algorithm to add 3 numbers and print their sum:
1. START
2. Declare 3 integer variables num1, num2, and num3.
3. Take the three numbers, to be added, as inputs in variables num1, num2, and
num3 respectively.
4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.
6. Print the value of the variable sum
7. END
Example: Finding a Word in a Dictionary
1. Start
2. Open the dictionary.
3. Look at the first letter of the word.
4. Turn pages to the section corresponding to that letter.
5. Scan through the words in alphabetical order.
6. Stop when you find the desired word.
7. Read its meaning.
8. End
Flowcharts
● Flowcharts are the visual representations of an algorithm or a process.
● It uses symbols/shapes like arrows, rectangles, and diamonds to properly
explain the sequence of steps involved in the algorithm or process.
● Rules For Creating a Flowchart
○ Rule 1: Flowchart opening statement must be ‘start’ keyword.
○ Rule 2: Flowchart ending statement must be ‘end’ keyword.
○ Rule 3: All symbols in the flowchart must be connected with an arrow line.
○ Rule 4: Each decision point should have two or more distinct outcomes.
○ Rule 5: Flow should generally move from top to bottom or left to right.
Symbols used in Flowchart Designs
Example: Flowchart to input two numbers from the
user and display the largest of two numbers.
● Start: The process begins with the Start
symbol, indicating the start of the program.
● Input num1: The first number, represented
as num1, is entered.
● Input num2: The second number,
represented as num2, is entered.
● Decision (num1 > num2): A decision point
checks if num1 is greater than num2.
○ If True, the process moves to the next step
where num1 will be displayed.
○ If False, the process moves to display num2.
● Stop: The process ends with the Stop
symbol, signaling the conclusion of the
program.
Pseudocode
● A Pseudocode is defined as a step-by-step description of an algorithm.
● Pseudocode does not use any programming language in its representation
instead it uses the simple English language text as it is intended for human
understanding rather than machine reading.
● Pseudocode is the intermediate state between an idea and its
implementation(code) in a high-level language.
● How to write pseudocode:
○ One statement per line: Each step should be on its own line for clarity.
○ Use indentation: Indent code blocks for conditional statements and loops to show which lines of
code are part of that structure.
○ Use clear keywords: Use simple, descriptive keywords like START, END, INPUT, OUTPUT,
IF, THEN, ELSE, FOR, and WHILE.
○ Assign values with an arrow: Use an arrow (\(\leftarrow \)) or an equals sign to show value
assignment, such as \(x\leftarrow 5\) or \(x=5\).
○ End nested blocks: Explicitly end nested structures with a corresponding keyword, like END IF
or END WHILE.
Example: Pseudocode to input two numbers from the user and display the largest of
two numbers.
Assignment
1. Define Python in your own words. Why is Python programming so popular in 2025?
2. Explain the major milestones in the history and evolution of Python programming.
3. What is an Algorithm? Explain its main characteristics.
4. What is a flowchart? What are the symbols used in a flowchart?
5. Explain pseudocode. List down the guidelines to be followed while writing a
pseudocode.
6. Write step-by-step algorithms for the following problems:
○ Write an algorithm to add two numbers.
○ Write an algorithm to find the largest of three numbers.
○ Write an algorithm to check whether a number is even or odd.
○ Write an algorithm to calculate the area of a rectangle.
○ A program to check eligibility for voting (age ≥ 18).
Also draw flowchart and write pseudocode for the above tasks.