Lecture 3: Introduction to Python Programming
Python
● Python is popular both for data science &
general software development
● Mastering the language fundamentals is critical
● Learn through practice:
○ See some examples & learn the rules
○ Try out variants of those examples yourself
○ Write new code that solves new problems
Python
● What is Python?
● Key Features of Python
● Python Syntax Basics
● Data Types and Variables
● Control Structures (Loops and Conditionals)
● Functions in Python
● Python Libraries and Frameworks
● Getting Started with Python
What is Python
● Definition: Python is a high-level, interpreted, object-oriented
programming language programming language.
● Incorporates modules, exceptions, dynamic typing, very high level
dynamic data types, and classes.
● Emphasize readability and simplicity.
● Supports multiple programming paradigms including object-
oriented programming, procedural.
● Portable: runs on many Unix variants including Linux and macOS,
and on Windows.
Key Features of Python
● Easy to learn and use.
● Versatile (web development, data science, AI, etc.).
● Large and active community, plenty of resources,
tutorials, and libraries.
● High demand in the job market.
● Cross-platform compatibility.
Python Syntax Basics
● Use whitespace indentation to define code blocks instead of curly
braces {}
● Hello World program:
● Python uses indentation to define blocks of code (4 spaces or
a tab)
● Comments:
Data Types and Variables
● Python is dynamically typed, don’t need to declare the
type of a variable
● Variable Naming Rules:
○ Must start with a letter or underscore _.
○ Can only contain letters, numbers, and underscores.
○ Case-sensitive (variableOne and variableone are different).
Basic Data Types
● Integers (int): Whole numbers, e.g., 10, -5.
● Floats (float): Decimal numbers, e.g., 3.14, -0.001.
● Strings (str): Text, e.g., "Hello", 'Python'.
● Booleans (bool): True or False.
● List, Dict: collection data type.
● Array datatypes (Numpy)
These are objects in Python.
Python supports various operators for performing
operations on variables and values.
Arrays
● Is a collection type in Python
○ Can contain strings or other types of values
○ All elements in an array must be of the same type
● Functions on array
○ Numpy library provides a handful collection of functions
operating on arrays: sum, exp, log, sqrt, sort
○ See more: https://numpy.org/doc/stable/user/index.html
Ranges
A range is an array of consecutive numbers
● np.arange(end):
An array of increasing integers from 0 up to end
● np.arange(start, end):
An array of increasing integers from start up to end
● np.arange(start, end, step):
A range with step between consecutive values
The range always includes start but excludes end
Control Structures
● Python uses if, elif, and else for conditional statement.
● Python supports for and while loops for loop.
Functions
● Functions are defined using the def keyword.
Classes
● Python supports OOP.
Example program
Python Libraries and Frameworks
● Data Science:
○ NumPy: Numerical computing.
○ Pandas: Data manipulation and analysis.
● Visualization:
○ Matplotlib, Seaborn.
● Machine Learning:
○ Scikit-learn: Machine learning algorithms.
○ TensorFlow/PyTorch: Deep learning frameworks.
○ Ollama/LangChain/Transformers: LLM, Generative AI
● Web Development:
○ Flask/FastAPI: Lightweight web framework.
○ Django: Full-stack web framework.
Getting Started with Python
● Install Python: python.org.
● Use an IDE: VS Code, PyCharm, Jupyter Notebook.
● Online platforms: Google Colab.
● Write your first program!
Recommended:
- Python environment: Miniconda/Conda
- https://docs.anaconda.com/miniconda/
- IDE: VS Code
- https://code.visualstudio.com/