Introduction To Python
Introduction To Python
to Python
A Guide for Marketers, Developers,
and Data Analysts
1
An Introduction to Python
Introduction
Python has quickly become the
most-searched programming
language on Stack Overflow and the
#1 language on the TIOBE index,
surpassing C and Java in 2022.
2
An Introduction to Python
Build your optimized
website on the HubSpot CMS.
Why do the work when HubSpot can do it for you? Use HubSpt’s
CMS to build your website without being a programming expert.
Get started
3
An Introduction to Python
Table of Contents
CHAPTER ONE
What is Python? 4
CHAPTER TWO
Methods & Functions 12
CHAPTER THREE
Coding Standards and Programming Best Practices 20
CHAPTER FOUR
Advanced Python 22
CHAPTER FIVE
Resources for Future Learning 24
CLOSING 26
4
An Introduction to Python
CHAPTER ONE
What is Python?
Python is one of the fastest growing, most multipurpose
Python was named after a Monty Python sketch. According to the TIOBE Index, which ranks
programming language popularity, Python is
Source: Python Institute
the top programming language as of February
2022. So, why has Python grown as the top
choice for developers, engineers, and data
“Python works for everything. It’s a one-size- scientists, as well as an increasing number of
non-programmers?
fits-all, industry agnostic, flexible language
that allows teams to capture, enrich, and store
data, automate processes, and utilize machine
learning, among other things. Organizations
depend on Python to function.”
5
An Introduction to Python
Python is:
• Open source, with a strong community of developers
(Pythonistas and Pythoneers) and extensive library of code (PyPI)
• Free
• Flexible
• Collaborative
• Trusted
Python was built on the principles that code should be simple over
complex, and anyone should be able to understand it. It reads like English,
and is easier to debug than compiled languages. Because of this and
the vast capabilities that this programming language holds, Python has
become a fan favorite in the developer community.
6
An Introduction to Python
What is Python most commonly used for?
Python is mainly used for data science, data visualization, machine
learning, and website and app development. Top platforms like
YouTube, Pinterest, Google, Industrial Light and Magic (special “The flexibility of being able to
effects company originally founded by George Lucas), and program custom code when
Instagram are powered by Python plus Django (a Python-based
web framework).
you have large amounts of
data is invaluable. Python
Python can be used to develop websites and apps, to clean, analyze,
and model data, to build games and services, to automate processes, combines a low technical
and to create machine learning algorithms. It’s also an easy language cost of entry while providing
to learn, making it a top choice for teaching software engineering.
a near limitless set of data
Many data experts and engineers use Python when they’ve maxed
processing capabilities.”
out the functionality of Excel or Google Sheets’ pivot tables, or it’s
simply too much data for those programs to execute. Python has
proven a more adaptable solution than industry-specific competitors Joe Camilo Ph.D.,
Engineering Director at
like MATLAB or Perl due to its ease of use, transferability between
CoVar, a machine learning
applications, and lower costs. The platform is free and upkeep is less company.
expensive and specialized than more complex tools.
7
An Introduction to Python
Python for Digital Marketing Setting Up and Installing Python
In digital marketing, Python is a powerful To get started using Python, download the latest version of Python to
data processing tool that picks up where your computer right from the Python website, or use a web-based code
pivot tables and business intelligence tools editor like Codepen. It’s important to have the latest version to avoid
leave off. Since it’s open source, you can bugs and have your programs run as smoothly as possible. Python is
connect any API to Python using web hooks. an interpreted language, so you write your Python code files in a text
With Python, you can create programs editor, then execute them using an interpreter.
that automate email responses, data and
Most people who use Python use an integrated development
reporting, data cleanups, and other manual
environment (IDE), which includes both the text editor and the
tasks. You can pull new trends and patterns
interpreter, displayed in a graphical user interface (GUI), like PyCharm,
from large data sets and even connect Python
VS Code, or Sublime, and these vary by industry or preferred
with your HubSpot account.
programming language. Most are free and they include features like
checking code for errors, color coding, customizable UI, plugins,
improved code quality, and other collaboration and developer tools.
8
An Introduction to Python
Once you’ve downloaded PyCharm or
another text editor or have Codepen open,
you’re ready to start writing Python code.
Python has their own helpful guide to getting
started that includes resources and additional
documentation based on your level of
programming experience.
9
An Introduction to Python
Syntax and Format in Python
Python was designed to read like English and Once you’ve opened a Python Then, when you run the
operates using white space formatting or spaces file, basic Python syntax looks program, the output, which
or tabs that denote objects and classes. Python like this, and is entered into the appears in the terminal
programs execute from top to bottom, line by code editor: window, will be:
line, making it efficient to debug since you can
troubleshoot an issue and stop the code in the
print("Testing this code!") Testing this code!
middle, rather than having to stop the entire
program like in a compiled language like C++.
10
An Introduction to Python
PRO TIP
11
An Introduction to Python
CHAPTER TWO
PYTHON GLOSSARY
13
An Introduction to Python
PYTHON GLOSSARY
14
An Introduction to Python
PYTHON GLOSSARY
15
An Introduction to Python
Object-Oriented Programming Basics
The four main elements of OOPs are encapsulation,
abstraction, inheritance, and polymorphism. Object-
oriented programming was designed to simplify the code
development process, minimizing the need to copy and
paste code.
Encapsulation
Encapsulation is when a group of similar variables and
functions are combined into one object, by defining them
in a group like a class. Encapsulation helps protect and
secure data and prevents code from being altered by
others. To make a given element private in Python, add an An example of functional programming (above)
underscore before it in the code, like this: versus OOP (below) from Programming with Mosh.
_private,
Abstraction
Or, to perform name mangling, add a double underscore
before an attribute, like this: Abstraction is when you are able to block off certain
functions and methods from the rest of the code.
__private.
Abstraction is helpful in coding because it:
16
An Introduction to Python
Inheritance Polymorphism
Inheritance is the principle of object-oriented Polymorphism gives OOPs flexibility — rather than applying
programming that represents the streamlined, a method in the same way to each element or object,
simple nature of the language. It helps to reduce methods are applied to individual objects and can be run
redundancy by applying a set of properties and in different ways based on the type of object.
methods to multiple objects. Rather than having
This replaces more complex code called a switch-case
to repeat those properties and methods every
statement in other programming languages. In Python,
time, they can inherit the information, reducing the
you can write one line of code with a method added on to
overall amount of code.
achieve the same result.
• Related variables and functions are • Functions operate on data, variables and
combined into objects functions are separate
17
An Introduction to Python
Using Functions and Methods in Python
Functions are elements of code that produce a result and The output for the .upper method in the above
can pass parameters (data), which are noted in parentheses example is “INTRO TO PYTHON”.
following the function. An example of a function is:
The difference between methods and functions in Python
is that methods are associated with a class and dependent
def test_function(): on that class, but functions are independent. Python has a
print("Hello world") number of built-in functions, including common go-tos like
print(), range(), and list().
18
An Introduction to Python
Leveraging PyPI
The Python Package Index
PyPI is the centralized database of Python packages
maintained by the community — it’s a place to upload or
access packages for different use cases, industries, and
data needs. With more than 350,000 projects, you’re
bound to find something that helps with your next project.
NumPy
A scientific computing package that includes
mathematical functions, random number generators,
linear algebra routines, and more.
19
An Introduction to Python
CHAPTER THREE
Here’s what distinguishes good Python programming from bad programming, and makes a
Pythonista out of a developer. When in doubt — make it readable.
20
An Introduction to Python
Poor Python Formatting vs. Good Python Formatting Examples
Python code should be readable, simple, and always use 2 Keep code structure as simple as possible. While
less code when possible. Here are some examples of clunky both statements in the below example achieve the
strings and functions and how they can be simplified. same outcome, the one denoted as #pythonic is more
straightforward and considered “better” Python code.
1 Only use one statement per line. Don’t combine
disjointed statements, like if <complex comparison>
and <other complex comparison> in the example
below, on the same line.
Source: Quora
21
An Introduction to Python
CHAPTER FOUR
Advanced Python
Python enables developers to connect complex data sets, to automate processes,
and to visualize data. Here are more Python elements and functions that enable more
advanced programs.
Explore W3’s Python Tutorials for detailed practice examples and additional exercises.
Dictionary Set
A collection of key-value pairs that A built-in Python data type that stores
match values in an associative array. multiple items in an unordered list in a testset = ["dog", "cat", "lizard"]
22
An Introduction to Python
Command Line + Command Line Arguments
The command line is where you invoke Python and can add options like -c (command), -m (module-name), scripts,
and arguments. The interpreter reads the command line within the environment to learn which settings to use.
Command-line arguments are additional information within the line of a program after the program.py on the
command line.
x = range(4, 24, 2)
For Loops for x in x:
Executes a set of functions pets = ["dog", "cat", "lizard"] print(n
or statements for items in for x in pets:
sets, or strings.
Tuples
An ordered list that is unchangeable and stores
multiple items in one variable. Allows duplicate values,
List Methods and is indexed, starting with the first item as [0].
pets = ["dog", "cat", "lizard"]
Object-based functions that
pets.append("parakeet")
are applied to lists.
thistuple = ("dog", "cat", "lizard, "cat", "lizard")
print(thistuple)
23
An Introduction to Python
CHAPTER FIVE
1 Industry Publications
• The HubSpot Website Blog
• We Are Developers
• Code2040 • PyCharm
• VS Code
• Sublime
• PyDev
24
An Introduction to Python
4 Frameworks 5 Developer Forums + Communities
Frameworks are platforms for developing software, No matter how long you’ve been coding, every
websites, and applications that simplify the developer will at some point or another run into
development process and help programmers something they need help with. Chances are,
speed up their processes in a standardized way. someone else has had the same issue. Rather
than wasting hours of your life attempting to
Types of frameworks include full stack frameworks,
troubleshoot solo, turn to the Python developer
micro frameworks, and asynchronous frameworks.
community and see if a Pythonista or a
Popular frameworks for Python include: Pythoneer can lend a helping hand.
Django • Python Forum
A web app framework “for perfectionists” that can be
• Stack Overflow
used for larger, more complex apps and websites.
• Reddit: r/python
Masonite
A web development app designed for software and • Reddit: r/learnpython
used for SaaS.
• Women Who Code: Python
CherryPy
• Black Girls Code
A “minimalist Python web framework” that reduces
source code size and speeds up development. • Black Tech Nation
Hug
A Python API framework that “drastically simplifies
API development over multiple interfaces,” and is
the “fastest and most modern way to create APIs
on Python3”.
25
An Introduction to Python
Closing
Power your website
Python has quickly become one of the most
popular, versatile programming languages used
by developers. And just as Guido van Rossum
imagined when he first developed it, the more
with what it needs to
people who contribute to the community, the grow better
richer and better the language becomes.
We hope this guide helped to introduce you to The HubSpot Website Blog has all the latest
the vast capabilities that Python has to offer and tips, best practices, and inspiration for building
inspired you to automate processes, explore websites that help businesses grow.
data, and leverage the PyPI database to rethink
the way you work. Whether you’re a marketer, developer, designer, or
strategist, you need to know the most up-to-date
information about building and optimizing your website.
26
An Introduction to Python