What Is Python?: in Data Science
What Is Python?: in Data Science
In data science
Python is an easy-to-learn
programming language
with a long history.
Python History
Van Rossum publishes
1991 Python version 0.9.0
to alt.sources
Intuitive syntax
@atoti
Why is Python popular
in Data Science?
ir es
ra
Lib
Visua
liza
ML
ti
on
s
ng
si
es
Data pro c
* Above are examples of Python libraries available for use but not limited to these.
@atoti
How do we get libraries?
1. Using pip
Run command
3 in console
Import library
4 and start using
* Installation instruction can usually be found on the libraryʼs website or GitHub repository.
@atoti
How do we get libraries?
2. Using conda
1 Pre-requisite: Anaconda
or Miniconda installed
* Conda installs packages which may contain software written in any language.
** "conda-forge" channel is free for all to use.
@atoti
Virtual environment
- Windows myenv\Scripts\activate.bat
@atoti
Integrated development
environment (IDE)
vs Code editor
Interactive IDE
A set of tools that makes
sion
coding easier: text editor,
ten
Jup
yte
r ex compiler, build, debugging,
all
Inst
auto linting function,
auto-completion, etc
Install Jupyter package
Ins
tal
l Sp
yd
er-
no
teb
oo
k
Code editor
Text editors with power-
ful built-in features and
Extract codes from Jupyter note-
book to .py files for production: specialized functionalities
jupyter nbconvert --to script to simplify and accelerate
my_notebook.ipynb code editing process
* Above are examples of IDE and code editors for Python but not limited to these.
@atoti
Dependency management
https://docs.python.org/3/tutorial/venv.html
1. poetry init *
2. poetry add <package>
3. pyproject.toml file updated with dependency
4. sub-dependency version updated in poetry.lock
1. pyproject.toml present
2. poetry install
@atoti
Beginner’s Cheat Sheet
Base Type
Boolean
bool True, False
Type
[1]: b"""Line 1.
Line 2.
bytes immutable sequences of single bytes """
[1]: b'Line 1.\nLine 2. \n'
[1]: byte_arr=bytearray("""Line 1.
Line 2.""", "utf8")
print(byte_arr)
bytearray mutable counterpart to bytes objects bytearray(b'Line 1.\nLine 2.')
Binary [2]: del byte_arr[0:8]
Types print(byte_arr)
bytearray(b'Line 2.')
[1]: mv = memoryview(byte_arr)
print(bytes(mv[0:7]))
b'Line 2.'
Access the internal data of an object without
memoryview
copying, unlike bytes/str [2]: mv[6] = 33
print(byte_arr)
bytearray(b'Line 2!')
@atoti
Beginner’s Cheat Sheet
Base Type
Data
Category Description Examples
types
Container holding com-
ma-separated values
between square brackets. ["apple", "banana", "cherry"],
list Mutable. ['Friday', datetime.date(2022, 1, 14), 2022, 'Jan', 14]
date_dict = {
"day_of_week": "Friday",
"date": datetime.date(2022, 1, 14),
Ordered collection of data
Mapping "day": 14,
dict stored in key-value pairs.
Types "month": "Jan",
Key is unique.
"year": 2022
}
Unordered collection of
{"apple", "banana", "cherry"},
data that is mutable,
set {"Friday", datetime.date(2022, 1, 14), 2022, "Jan", 14}
iterable and has unique
elements.
Set Types
Freeze iterable objects,
frozen-
making them unchan- frozenset({"apple", "banana", "cherry"})
set
geable.
@atoti
Beginner’s Cheat Sheet
Type conversions
str → list "The quick brown fox jumped over the lazy dog".split( )
→ ['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
list(map(list, d. items()))
→ [('day', 14), ('month', 'Jan'), ('year', 2022)]
" ".join(['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog'])
list → string → 'The quick brown fox jumped over the lazy dog'
@atoti
Beginner’s Cheat Sheet
Strings
Code Results
print("Hello") Hello
Concatenation
Code Results
a = "Hello"
b = "world"
year = 2022 Hello world 2022
print(a + " " + b + " " + str(year))
# cast numeric value to str before concatenation
Formatting
Code Results
"{:>30}".format("right aligned")
or ' right aligned'
"right aligned".rjust(30)
@atoti
Beginner’s Cheat Sheet
Strings
Formatting
Code Results
"{:^30}".format("centered")
or ' centered '
"centered".center(30)
name = "Jane"
'Hello, Jane!'
f"Hello, {name}!"
import datetime
d = datetime.datetime(2022, 1, 14, 10, 15, 58)
'2022-01-14 10:15:58'
"{:%Y-%m-%d %H:%M:%S}".format(d)
Change case:
'Apple'
"apple".capitalize( ) # Converts the first character to upper case
'apple'
"APPLE".casefold( ) # Converts string into lower case
'apple'
"APPLE".lower( ) # Converts a string into lower case
'APPLE'
"apple".upper( ) #Converts a string into upper case
'aPPLE'
"Apple".swapcase( ) # Swaps cases, lower case becomes upper case and vice versa
'Apple'
"apple".title( ) # Converts the first character of each word to upper case
String functions
Code Results
@atoti
Beginner’s Cheat Sheet
Functional modules
Functions
Code Results
if myname:
print(f"I am {myname}.")
Hello, everybody!
greet( )
Hello, Jane!
greet("Jane")
Hello, Jane!
greet("Jane", "Thomas")
I am Thomas.
Exception handling
Code Results
try:
print("Hello " + 123) Error occurred: can only concatenate str (not "int") to str
except Exception as e: Gets printed no matter what
print(f"Error occurred: {str(e)}")
finally:
print("Gets printed no matter what")
num1 = 1
num2 = "1"
if num1 != num2: Exception: num1 (1) is not equal to num2 (1)
raise Exception(f"num1 ({num1}) is not equal to num2 ({num2})")
@atoti
Beginner’s Cheat Sheet
Functional modules
Modules/Names Import
Code Results
dt.today()
Iterative looping
Code Results
Do something
for f in fruits:
if f == "banana":
apple
break → exit from the loop
print(f)
@atoti
Beginner’s Cheat Sheet
Strings
Conditional
Code Results
else: → default action when conditions on the same level are not met
print("Kiwi not found in basket1 nor basket2.")
Read/Write files
Code Results
f.close()
f = open("file.txt", "r") r: read-only Existing content will be cleared! This will be ap-
pended to existing content!
print(f.read( ))
Check out https://www.atoti.io/ to find out how to create a BI analytics platform in Python.
Stay tuned to https://www.youtube.com/atoti for useful tutorials.
@atoti