Intro_to_Python_Libraries_&_Modules.c4cdd7173ec260b8cbc4
Intro_to_Python_Libraries_&_Modules.c4cdd7173ec260b8cbc4
Libraries &
modules
SJU ACM STUDENT CHAPTER
before we start…
important Reminders:
• This lab will involve the use of python and visual Studio Code
(VSCode/VSC), both throughout the presentation and the
code-along lab at the end.
• If you do not have python and/or vscode set up on your machine,
the following will be a guide on how to set up python and vscode.
Python/vscode set up
• Step one:
○ Check if you have python on your machine
■ windows: press win+R, type in cmd, and command prompt should popup up. type in ‘py
—version’
■ Mac os: launch terminal, type in ‘python3 —version’
○ If not on your machine, proceed to step two
• Step two:
○ visit python.org/downloads, click ‘download python 3.12.2 (this is the latest major release)
○ Follow all on-screen instructions on python setup wizard
■ maintain all default settings
Python/vscode set up cont.
• Step three
○ visit code.visualstudio.com
■ windows: click download button
■ macos: click the arrow next to download button, and download mac os stable release
• Step four:
○ follow on-screen instructions on vscode setup wizard
■ notes:
● you will get an option to create a desktop icon, i recommend you check that box
● once the installation process finishes, ensure to check the box to launch vscode
Python/vscode set up cont.
• Step five
○ once inside vscode, customize your experience to your liking (this can be changed later)
■ for now, ensure you select the color theme you want
• Step six:
○ on the left hand side, there is a menu with seven icons (five on the top half, two on the bottom half)
○ click on the last icon in the top half of the menu (extensions)
• step seven:
○ Once inside extensions, you will search for two things that you need to download:
■ python (should be in the popular section)
■ Code Runner (helps you run your code)
○ install both, and you should be set!
Primer on python
Primer on python
• Python is a high-level, general-purpose programming language.
○ It is dynamically-typed and garbage-collected.
○ Known for clear syntax, readability, flexibility, and
indentation.
○ Supports multiple programming paradigms:
■ Structured, oop and functional programming
• Fast Facts:
○ Creator: GUido van rossum
○ First release: february 20th, 1991
○ latest release: python 3.12.2 (february 6th, 2024)
Primer on python cont.
• Python is known for its comprehensive standard library.
○ As a result, Python is said to be a “batteries included”
language.
○ it provides broad functionality, with a vast number of
modules and packages that support a variety of tasks.
○ Eliminates need for external dependencies.
○ Included with every python installation!
• Examples:
○ text processing, data types, math, file/directory access, etc.
Basics of modules and
libraries
What Are modules?
• A module in python is a file containing python definitions and
statements.
• Can define functions, classes, and variables that can be used in
other python files.
• Typically used to break down large programs and tasks into
small and manageable files.
• Two types of modules:
○ user-defined modules
○ built-in modules
user-defined modules
● How to create your own module:
○ First, create a new file, and name it
appropriately. Ensure it ends in .py.
○ Next, define a function, class, variable, etc.
○ Then, in a new file, use the ‘import’ keyword on
the first line of code.
■ you are able to rename the module
whatever you want in the new file.
○ Finally, access the function(s) you desire using
the following syntax:
■ module_name.function_name
Built-in modules
● Python comes equipped with a library of standard modules.
○ over 200 modules in the library
○ Some are built into the interpreter, others are not.
○ Most modules are available to users, but some are designed for specific
operating systems/platforms/software.
■ winreg, Winsound: Designed for windows
■ posix, pwd, syslog: designed for unix/linux
Examples of most-often used modules
● Os:
○ provides way to use os-dependent functionality, e.g.
navigating file system
● Sys
○ used to manipulate the python runtime environment
● datetime
○ supplies classes for manipulating dates & time,
essential for time-based ops
● math
○ offers access to mathematical functions
● random
○ provides tools for random selections
What are Libraries?
● In short, a library in python is an umbrella term that refers to a
reusable chunk of code.
○ Typically consists of a collection of related modules and
packages
○ In other words, libraries are a collection of packages.
● Libraries help devs share reusable code with the community.
○ This eliminates the need to write code from scratch, which is
very useful for large-scale projects!
○ This is done by creating a set of functions that are related to
the same area.
● Fun fact:
○ There currently are over 137,000 python libraries!
■ rookie numbers!
Popular python libraries
● Data Analysis & Science:
○ Pandas & Numpy: Data structures, data analysis, math functions
● Machine Learning & Ai:
○ Tensorflow & Pytorch: flexibility, speed, efficiency in ML & AI
Development
● Data visualization:
○ matplotlib & seaborn: high-level interfaces for visualizing and
drawing statistical graphics
● Scientific computing:
○ Scipy & sumpy: Science computing, algorithms, & mathematical
tools
● Networking & Internet:
○ requests & Beautiful soup: Http library, web scraping
What are packages?
● packages in python are directories of a collection of modules
○ Packages allow for hierarchical structuring of the module
namespace.
○ In the same way we organize our files into folders and
subfolders on a hard drive, we can organize modules into
packages and subpackages.
● Each package must include an initialization file for it to be
considered a package.
○ file name: ‘__init__.py’
● Fun fact:
○ there are over 497,000 python packages, according to
pypistats.org (as of 2/14/2024)
■ ALL-pro numbers!
popular packages
● Numpy/pandas
○ Previously mentioned; are package/library hybrids
● pytest
○ Provides a variety of modules to test new code.
■ incl. small unit/complex functional tests
● Boto3/botocore
○ these are part of the amazon web services software development
kit (AWS SDK).
○ makes it easy to integrate python with aws services!
● pip
○ The recommended tool for installing python packages
● Setuptools
○ helps to easily download, build, install, upgrade, and uninstall
python packages.
○ requires a few dependencies for it to work
what are frameworks?
● Frameworks in python are a special collection of modules and
packages that help programmers fast track the development process.
○ Usually more complex than libraries
○ Contain packages that perform specific operations
■ Basic flow & architecture of application
● Analogy of house construction:
○ python frameworks provide you with all the essential building
blocks of construction
■ Foundation, walls, windows, roof, etc.
○ Then, devs build the application around the foundation and add
furniture, appliances, etc.
● Three types of frameworks:
○ full-stack
○ micro
○ asynchronous
types of frameworks
● Full-stack:
○ have all web dev requirements
■ form generators, form validation, template layouts
■ Core features of any full-stack framework
● Micro
○ Require lots of code and additional requirements to be added
manually
■ doesn’t provide the specific tools provided by full-stack
frameworks
● Asynchronous
○ supports high concurrency
■ large set of concurrent connections
○ uses asyncio library to run the process
popular frameworks
● Flask
○ A micro framework through which devs can build a solid web app
foundation from
■ lightweight/modular design, readily adaptable
● Django
○ A Full-stack framework, known for helping develop rich web apps
■ built-in libraries, free-to-use features, database support
● bottle
○ A micro framework, creates a single source file for every developed app
■ Api dev, no dependencies, built-in https server, database support
● Cherrypy
○ Open-source, OOP, micro framework
■ one of the oldest python Frameworks
■ runs on android, flexibility, robust configuration system, etc
● falcon
○ A micro framework used to build web apis
■ needs dependencies, used widely at linkedin, openstack, etc.
■ upfront exception handling, highly optimized code base, extensible
Knowing the difference
● Packages and libraries are often used
interchangeably. However, it is important to
understand the difference between them.
● Generally speaking:
○ A package is a collection of modules
○ A library is a collection of packages
● All of the terms discussed so far (Function,
module, package, library, and framework) are
ultimately cogs working together, hand-in-hand,
to create an application.
source: Swathi arun,
https://medium.com/pythoneers/6-must-know-words-in-python-ac87ab420ab7
lab time!
presenting: python turtle!
● python turtle is a standard library designed to make learning
programming concepts enjoyable.
○ Ideal for beginners
○ Uses virtual turtle that can be controlled to draw shapes
and complex patterns
● It is an implementation of the eponymous geometric drawing
tools developed for the logo programming language.
○ developed by Wally feurzeig, seymour papert, and cynthia
solomon in 1967
● to use turtle:
○ ensure it is installed on your machine (use pip install
PythonTurtle)
what will we be doing?
● We will be drawing a heart for valentine’s day!
○ Make sure to follow along to find out how you can make
this:
Python resources
python resources
● pypi
○ https://pypi.org/
● pypi Stats
○ https://pypistats.org/
● Python documentation
○ https://docs.python.org/3/
● Difference Between Python Modules, Packages, Libraries, and Frameworks - kateryna koidan
○ https://learnpython.com/blog/python-modules-packages-libraries-frameworks/
Thank you!