Python
Programming
By: Ambrose Njeru [BSc, Msc] 1
Introduction
❖Python is an object-oriented programming language
created by Guido van Rossum in 1989 and released in
1991.
❖It is used for:
❖Web development (server-side),
❖Software development,
❖Mathematics,
❖System scripting,
❖Machine Learning,
❖Data science
By: Ambrose Njeru [BSc, Msc] 2
What Can Python Do?
❖Python can be used on a server to create web
applications.
❖Python can be used alongside software to create
workflows.
❖Python can connect to database systems. It can also
read and modify files.
❖Python can be used to handle big data and perform
complex mathematics.
❖Python can be used for rapid prototyping, or for
production-ready software development.
By: Ambrose Njeru [BSc, Msc] 3
Why Python?
❖Python works on different platforms (Windows, Mac,
Linux, Raspberry Pi, etc).
❖Python has a simple syntax like the English language.
❖Python has syntax that allows developers to write
programs with fewer lines than some other
programming languages.
❖Python runs on an interpreter system, meaning that
code can be executed as soon as it is written. This
means that prototyping can be very quick.
❖Python can be treated in a procedural way, an object-
oriented way or a functional way.
By: Ambrose Njeru [BSc, Msc] 4
Python Syntax Compared to
Other Programming Languages
❖Python was designed for readability and has some
similarities to the English language with influence
from mathematics.
❖Python uses new lines to complete a command, as
opposed to other programming languages which
often use semicolons or parentheses.
❖Python relies on indentation, using whitespace, to
define scope; such as the scope of loops, functions
and classes. Other programming languages often use
curly-brackets for this purpose.
By: Ambrose Njeru [BSc, Msc] 5
Installation of Python IDE
❖We are using PyCharm IDE, which is a cross-
platform editor developed by JetBrains.
❖Pycharm provides all the tools you need for
productive Python development.
❖We start by installing Python and then the PyCharm
IDE.
By: Ambrose Njeru [BSc, Msc] 6
Installation of Python IDE
1. Download and install Python from the official
website of Python
[Link] and choose
the latest version.
By: Ambrose Njeru [BSc, Msc] 7
Installation of Python IDE
2. Run the .exe file downloaded to install Python by
clicking Install Now.
By: Ambrose Njeru [BSc, Msc] 8
Installation of Python IDE
3. When it finishes, a screen saying the Setup was
successful will be displayed. Click on “Close”.
By: Ambrose Njeru [BSc, Msc] 9
Installation of Python IDE
4. After installing Python, then install Pycharm IDE.
Download PyCharm by visiting the website
[Link]
and Click the “DOWNLOAD” link under the
Community Section.
By: Ambrose Njeru [BSc, Msc] 10
Installation of Python IDE
5. Run the exe for install PyCharm.
By: Ambrose Njeru [BSc, Msc] 11
Installation of Python IDE
6. Keep the selected JetBrains and click on “Install”.
By: Ambrose Njeru [BSc, Msc] 12
Installation of Python IDE
7. Once installation finished, you can go ahead and
run it by clicking the “Run PyCharm Community
Edition” box first and click “Finish”.
By: Ambrose Njeru [BSc, Msc] 13
Python Programming
❖In Pycharm IDE, create a new project and uncheck
create a [Link] welcome script.
❖You then click on your project and create a new
Python file.
print ("Python Programming in NLP")
By: Ambrose Njeru [BSc, Msc] 14
Python Indentation
❖Indentation refers to the spaces at the beginning of a
code line.
❖Where in other programming languages the
indentation in code is for readability only, the
indentation in Python is very important.
❖Python uses indentation to indicate a block of code.
❖Python will give you an error if you skip the
indentation:
❖Example
if 5 > 2:
print("Five is greater than two!")
By: Ambrose Njeru [BSc, Msc] 15
Python Variables & Comments
❖Variables are created when you assign a value to it:
❖Example
x=5
y = "Hello, World!"
❖Python has no command for declaring a variable.
❖Comments start with a #, and Python will render the
rest of the line as a comment.
#This is a comment written in
#more than just one line
print("Hello, World!")
By: Ambrose Njeru [BSc, Msc] 16
Python Datatypes
❖Python has the following data types built-in by
default, in these categories:
Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
By: Ambrose Njeru [BSc, Msc] 17
Python Datatypes
❖You can get the data type of any object by using the
type() function:
x=5
print(type(x))
❖In Python, the data type is set when you assign a
value to a variable.
By: Ambrose Njeru [BSc, Msc] 18
Python Classes/Objects
❖Python is an object-oriented programming
language.
❖Almost everything in Python is an object, with its
properties and methods.
❖A Class is like an object constructor, or a "blueprint"
for creating objects.
class MyClass:
x=5
p1 = MyClass()
print(p1.x)
By: Ambrose Njeru [BSc, Msc] 19
Python User Input
❖Python allows for user input by use of the input()
method.
username = input("Enter username:")
print("Username is: " + username)
By: Ambrose Njeru [BSc, Msc] 20
NumPy Library
❖NumPy is a Python library used for working with
arrays.
❖NumPy is short for "Numerical Python".
❖The array object in NumPy is called ndarray, it
provides a lot of supporting functions that make
working with ndarray very easy.
❖Arrays are very frequently used in data science and
machine learning, where speed and resources are
very important.
By: Ambrose Njeru [BSc, Msc] 21
Installing NumPy in PyCharm
❖To install NumPy on PyCharm, click on File and go
to the Settings.
❖Under Settings, choose your Python project and
select Python Interpreter.
❖Then, search for the NumPy package and click
Install Package.
By: Ambrose Njeru [BSc, Msc] 22
NumPy Library
❖Once NumPy is installed, import it in your
applications by using the import keyword.
import numpy
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
❖NumPy can be imported under the np alias.
❖alias: In Python is an alternate name for referring
to the same thing.
❖An alias is created with the as keyword while
importing.
By: Ambrose Njeru [BSc, Msc] 23
NumPy Library
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr)
❖Array indexing is the same as accessing an array
element. You can access an array element by referring
to its index number.
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
print(arr[2])
By: Ambrose Njeru [BSc, Msc] 24
NLP Python Libraries
1. NLTK (Natural Language Toolkit):
❖Is used as an education and research tool but can be
used to build exciting programs due to its ease of use.
❖Features:
❖Tokenization.
❖Part Of Speech tagging (POS).
❖Named Entity Recognition (NER).
❖Classification.
❖Sentiment analysis.
❖Packages of chatbots.
By: Ambrose Njeru [BSc, Msc] 25
NLP Python Libraries
2. spaCy:
❖Is an open-source natural language processing Python
library designed to be fast and production-ready. spaCy
focuses on providing software for production usage.
❖Features:
❖Tokenization.
❖Part Of Speech tagging (POS).
❖Named Entity Recognition (NER).
❖Sentiment analysis.
❖Dependency parsing.
❖Word vectors.
By: Ambrose Njeru [BSc, Msc] 26
NLP Python Libraries
3. Gensim:
❖NLP Python framework generally used in topic
modeling and similarity detection. It is not a general-
purpose NLP library, but it handles tasks assigned to
it very well.
❖Features:
❖Latent semantic analysis.
❖Non-negative matrix factorization.
❖TF-IDF.
By: Ambrose Njeru [BSc, Msc] 27
NLP Python Libraries
4. TextBlob:
❖TextBlob is a Python library designed for processing
textual data.
❖Features:
❖Part-of-Speech tagging.
❖Noun phrase extraction.
❖Sentiment analysis.
❖Classification.
❖Language translation.
❖Parsing.
❖Wordnet integration.
By: Ambrose Njeru [BSc, Msc] 28