Introduction to Python

SPACE for next slide

Shift + SPACE for previous slide

Programming

Perception of programming

  1. Overwhelming because there are a lot of possibilities
    1. Choices
    2. Combinations
    3. New stuff every now and then
  2. Needs lot of practise just like Maths
  3. Fear of making mistakes
  4. Computers are dumb

How to make it easy?

  1. Be solution oriented because errors are inevitable
  2. Practise problems on exercism, HackerRank, etc.
  3. Build projects
  4. Discuss problems with experienced people
  5. Consume content that is related to programming

Basics before Python

Common Operating Systems

  • Darwin (macOS)
  • Linux (Ubuntu, Arch Linux, etc.)
  • Windows NT (Windows 10, Windows 11, etc.)

Since Linux and Darwin are based on the Unix operating system, they are commonly regarded as *nix.

Computers only understand 0s and 1s

ASCII

A standard that assigns a number to each character in English to enable information exchange between programs and across the network.

Examples:

  1. 0 - Null (empty)
  2. 46 - . (The punctuation)
  3. 48 - 0 (The number zero)
  4. 79 - O (The capital letter)
  5. 111 - o (The small letter)

Wikipedia reference

UTF-8

Like ASCII but enables more characters and languages.

Bits and bytes

  1. 1 bit can contain 0 or 1
  2. 2 bits can contain 00, 01, 10 or 11
  3. 8 bits = 1 byte
  4. 1024 bytes = 1 Kibibyte (KiB)
  5. 1024 bits = 1 Kibibit (Kib)
  6. 1000 bytes = 1 Kilobyte (KB)

Random Access Memory (RAM)

  1. Also called memory
  2. Operating system is in RAM
  3. Pixels are in RAM
  4. Programs are in RAM
  5. File processing needs RAM
  6. To process any data it must be in RAM
  7. Almost everything needs RAM to store data

RAM usage

Just like a house address there is an address for each byte in RAM. For a 4 GB RAM the highest address would be (4 ⨉ 109 - 1) i.e. 399999999 as the lowest address starts at 0.

A program uses these addresses internally for referencing a specific memory location which may span across multiple bytes.

Central Processing Unit (CPU)

  1. RAM read and write happens through CPU
  2. Network read and write happens through CPU
  3. All computing from the smallest to the largest happens on the CPU

CPU usage

The CPU only understands basic instructions like:

  1. 2 + 3?
  2. Is 56 less than 39?
  3. Jump to address 344 and start executing
  4. Get the value from RAM located at address 24
  5. Send these bytes to IP address 192.168.1.1

What are variables?

Variables are human readable names of certain memory locations that contains the required value.

Another perspective: We can use latitude and longitude to represent a location but we mostly use human readable names instead like New Delhi, London, etc. The value is the infrastructure, the people and the things present at the location.

Environment variables

These are special variables in an operating system which influence a program’s behaviour.

The most common example is the PATH variable which needs to change after installing a program like Python.

JAVA_HOME is also one of the requirements when doing something related to Java.

Program - Instructions for a computer

Program - Can be in any language

Program - Software

Program - Application

Program - Types

  1. Compiled
  2. Interpreted

Compiled programs

A program that needs to be compiled before execution is categorized as a compiled program.

There are special programs called compilers which turn the program into a file called executable which is made of 0s and 1s.

These executables need to be compiled for different platforms separately and may also depend on dll / so files to run properly.

Examples: firefox, anaconda, vlc, code, steam, emacs, etc.

Interpreted programs

Program is interpreted line by line in real-time by a special program called an interpreter which needs to be present on host system.

File path - Types

  1. Absolute path
    1. Starts with C:\ or Z:\ on Windows
    2. Starts with / on *nix systems
    3. Examples: /home/user/.bashrc, C:\Users\user\.bashrc
  2. Relative path
    1. Doesn’t start with / or C:\ or Z:\
    2. Examples: user/.bashrc, .bashrc, home/user/.bashrc

Python REPL i.e. interpreter

REPL - Read Evaluate Print Loop

  1. Read i.e. input from keyboard
  2. Evaluate i.e. run the line in Python
  3. Print i.e. show the results
  4. Loop i.e. go to step 1

Installing Python

You can install Python using one of the following ways:

  1. Official Python - Minimal and lightweight
  2. Anaconda distribution - Pre-installed with lots of libraries

Different ways of using Python

Way UI Extension Feedback Minimal
Python REPL
Ipython REPL
Script .py
Notebook .ipynb

Python REPL

Launch a terminal and type:

python

OR

python3

Now you can type code one line at a time and Python will execute it. To quit the REPL type exit() and press enter.

ipython REPL

ipython is the same Python under the hood but with lots of useful features like auto indentation and syntax highlighting. To install it type the following in a terminal:

python -m pip install ipython

Now run it by typing python -m ipython.

Jupyter notebook

A jupyter notebook is file having .ipynb extension and requires a separate environment to execute it. It also uses the the same Python under the hood to execute code. The best part about it is that documentation is a first class citizen with full markdown support and partial latex support.

Jupyter notebook - Anaconda (1st way)

Jupyter notebook - Text editor (2nd way)

You can find out how to enable jupyter notebook support from your text editor’s documentation or extension/plugin list.

Jupyter notebook - Direct installation (3rd way)

Install it using the following command:

python -m pip install jupyter

To start the server type python -m jupyter notebook in the terminal. This will print an URL which you can open in a web browser and start using it.

Why we wont start with pandas?

The reason

Because pandas feels like a different syntax and does lot of black magic under the hood which is hard to explain when you are just starting with Python.

Example

For example, you can write something like df[['date', 'language', 'column3']] in pandas which is easy to use but hard to reason about because you wont find any standard Python syntax that relates to this type of code. This sure is doable in normal Python but requires knowledge of classes and dunder methods.

Solution

Before moving to pandas I would suggest you to build a strong foundation in Python fundamentals to prevent confusion, lack of control and helplessness.

Python resources on Youtube

Important things to understand

  • Values, datatypes and Variables
  • Statements, comments and expressions
  • Code blocks, special statements and indentation
  • Scopes, functions and classes
  • Installing libraries in virtual environments

Values and Variables

What is a value?

  1. Value is data
  2. We need memory i.e. RAM to store a value
  3. Value is also called an object

Values have a type

Values are of different types which defines the rules of what you can do with it. Some examples are:

  • 2 an integer
  • "asd" a string
  • b"xyz" a sequence of bytes
  • print a function that refers to a block of code

Compatibility of different types of values

  • "2" + 3 gives error
  • "2" * 3 gives "222"
  • "a,b".split(",") gives ["a", "b"]
  • "a+b".split(+) gives error

In some cases you need to check and see what works while in other cases you need to read the documentation to understand the behaviour and compatibility with other datatypes.

What is a variable?

Variable is a human readable name that refers to a value

To set a value to a variable write the name on the LHS and the value or expression on RHS

Example:

var_name01 = 2
name = "Ram" * var_name01

Assigning a variable

  • Assigning a variable is nothing but setting a value to a variable
  • Assigning a variable for the first time can be termed as any of the following:
    • Defining a variable
    • Creating a variable
    • Declaring a variable
    • Bringing variable into existence

Why use a variable?

  • Improves readability of a program
  • Helps in abstracting out logic and creating custom functions
  • There is a garbage collector in Python which removes all values from memory that is not set to a variable. To prevent destruction of the value we set it to a variable.

Example 1

Lets write a program to convert 37℃ to farenheit

\[\frac{C}{5} = \frac{F - 32}{9}\]

Create variable with celsius value

celsius = 37

Transform the formula

To convert celsius to farenheit we need to transform the formula in such a way that F is on LHS and the rest is on RHS.

\[F = \frac{9C}{5} + 32\]

Translate to Python

F = (9 * C) / 5 + 32

It follows the BODMAS rule.

Write the code

celsius = 37
farenheit = (9 * celsius) / 5 + 32

But this wont show anything on the screen. To show the result we need to use the print function.

Write the print statements

celsius = 37
farenheit = (9 * celsius) / 5 + 32
print("Celsius:", celsius)
print("Farenheit:", farenheit)

Output:

Celsius: 37
Farenheit: 98.6

Statements

A line of code that can be executed in Python is called a statement.

Comments

Python will ignore anything written after hash (#) symbol. These are called comments and used mainly to document the code in a human readable language.

Example:

# This is a comment
x = 1  # I am invisible to Python

Expressions

A statement that returns / gives a result after running / execution is called an expression.

Expression - Examples

  1. 2 is an integer which returns 2
  2. "Hi there" is a string of characters which returns "Hi there"
  3. 3 + 2 is an arithmetic expression which returns 5
  4. 3 + 2 < 5 is a logical expression which returns False
  5. {"a": 1}.get("a") is a function call applied to a dictionary which returns 1

Expression - Usage

  • We generally use expressions to compute some result
  • Its the most fundamental part of a program
  • It always returns a value
  • The value returned can be stored in a variable

What is print?

print is a function that prints (shows) output on the console / terminal / screen and returns None i.e. if you were to capture it in a variable then the value stored in the variable would be None.

For example,

Outputs

Hi there
None

Indentation

Indentation is very important in Python. If indentation is incorrect your code wont run. It follows the following format:

special statement:
    code block

Make sure you give a colon (:) at the end of a special statement and each statement in the code block should be prefixed by one or more spaces.

What is a special statement?

Statements starting with the following keywords can be considered special statements:

  • if elif else
  • for and while
  • with
  • def and class
  • try except finally
  • match case

Note: Special statement is not a standard term. It is just used to explain the concept of indentation.

What is a code block?

A code block consists of one or more normal/special statements.

Code block - Single line

Example 1:

x = 1

Example 2:

print("Hello world")

Example 3:

2 if True else "Hi"

Code block - Multiple lines

x = 1
print(x)
x -= 1

Code block - Nested

x = "yes"
if "y" in x:
    b = "y" * len(x)
    if "e" in x:
        b += "es"
else:
    b = "n" + "o" * len(x)
print(b)

Output:

yyyes

if elif else - Invalid example 1

else:
    print("hi")

else and elif cannot be used in isolation and gives a syntax error when executed.

if elif else - Invalid example 2

else:
    print("Hi")
if 2 > 1:
    print("Hello")

else should be the last one in a group of if elif else.

if elif else - Invalid example 3

if:
    print("Hi")

A condition is required when writing an if statement.

if elif else - Example 1

if 2 < 3:
    print("Hello")

if in isolation can be used. If the condition is false then the block wont be executed / run.

if elif else - Example 2

if 3 < 3:
    print("Hi")
elif 2 < 3:
    print("Hello")

Here elif condition is checked when the if condition is false. When the elif condition is true then the corresponding block is executed. If both conditions are false none of them are executed.

if elif else - Example 3

if 3 < 3:
    print("Hi")
elif 2 == 3:
    print("Hello")
else:
    print("There")

Here the else block is executed only when all the conditions in if and elif are false. If any of the above conditions are true then the else block is not run.

if elif else - Example 4

x = 1
if x % 2 == 0:
    print("Even")
elif x < 0:
    print("Negative")
elif x - int(x) == 0:
    print("Integer")
else:
    print("Odd, Positive, Fraction")

We can write multiple elif but only one if and one else.

if elif else - Example 5

if 2 == 2:
    print("Hi")
if 3 < 2:
    print("Hello")
else:
    print("yo")

Here we have two set of independent if elif else blocks.

if elif else - Rules

  • Used for executing a part of program based on a condition.
  • if and elif blocks are executed only when the condition is true.
  • elif and else cannot be used without if.
  • else block is executed when all the conditions of if and elif are false.
  • Only one among a set of if elif else are executed whichever comes first.

if elif else - Example 6

if 1 == 1:
    print("Hi")
elif 2 == 2:
    print("Hello")

Output:

Hi

if elif else - Example 7

if 1 == 1:
    print("Hi")
if 2 == 2:
    print("Hello")

Output:

Hi
Hello

while

x = 1
while x < 5:
    print(x)
    x = x + 1

for

Used for iterating over a list of elements.

Example 1:

for i in [2, 1, 11]:
    print(i)

Example 2:

for i in range(2, 11, 2):
    print(i)

with

Used for automatic resource allocation and deallocation such as connecting to network, opening a file, etc.

with open("/path/to/some/file.txt") as f:
    print(f.read())

try except finally

try:
    print(some_unknown_var)
except NameError as error:
    print("An error occured")
finally:
    print("I will always be run")

Datatypes

Just like we have different file types for different purposes, we have different datatypes for different purposes.

Simple datatypes

Datatype Python name Example(s) Comment
Boolean bool True, False  
Integer int -3002, 0, 101  
Floating point float -30.2, 0, 0.39 Real numbers
Complex complex 3 + 2j j is √-1
String str ’abc’, “नमस्ते”  

Boolean datatype (bool)

Boolean values contain either 0 (False) or 1 (True)

bool - The or operator

The or operator returns False only when both values are False. For all other cases it returns True.

False or False = False
False or True  = True
True  or False = True
True  or True  = True

bool - The and operator

The and operator returns True only when both values are True. For all other cases it returns False.

False and False = False
False and True  = False
True  and False = False
True  and True  = True

bool - Writing conditions (part 1)

  1. 2 == 2 and 3 < 2
  2. True and False
  3. False

bool - Writing conditions (part 2)

  1. a, b = 2, 3
  2. a < b - 2 or a > 5
  3. 2 < 3 - 2 or 2 > 5
  4. 2 < 1 or False
  5. False or False
  6. False

bool - Writing if elif else statements

temp = 40
if temp > 45:
    print("Too hot to touch")
elif temp < 20:
    print("Too cold to touch")

The above elif could have been just if because the conditions are mutually exclusive i.e. temp can’t have a value that is both greater than 45 and lesser than 20. But it is better to use elif if you want to be extra careful to prevent printing both the messages.

String datatype (str)

Any data which contains text is considered a string. The text should be written between either one of ', ", ''', """.

str - Different ways

'Single line string'
"Single line string"
'''Multi
line
string'''
"""Multi
line
string"""

str - Example usage

"ab" + "2"      = 'ab2'
"ab" * 2        = 'abab'
"ab".isupper()  = False
"ABhI".lower()  = 'abhi'
"bc" in "a bcd" = True

str - Converting other datatypes to string

x = str(1 + 2) + "rd"
print(x)
3rd

str - Injecting values in a string:

a, b = 5, 4
x = "%s + %s = %s" % (a, b, a + b)
y = f"{a} + {b} = {a + b}"
print(x)
print(y)
5 + 4 = 9
5 + 4 = 9

Complex datatypes

Simple datatypes in isolation can’t solve all types of problems. Because of this we have datatypes like list, set and dict which are used to store a collection of values instead of single values.

Datatype (example 4)

To store data of arbitrary datatypes sequentially you can use the list datatype.

Example: ["abc", 1, [-0.3, 'a'], 52]

Datatype (example 5)

To store data of arbitrary hashable datatypes you can use the set datatype.

Example: {"abc", 1, (2,3)}

Datatype (example 6)

To store d

Text type (str)

'abcd'
"I'm"
"""multi
line
string"""
'''
another multi
line string
'''

Categories

Text Type str
Sequence Types list, tuple, range
Numeric Types int, float, complex
Mapping Type dict
Set Types set, frozenset
Boolean Type bool
Binary Types bytes, bytearray, memoryview
None Type NoneType

What is a function?

A function is a subprogram that is used to perform a predefined operation and return a value. It is mainly used to reduce development time by reducing lines of code and modularizing the code.

Basically, it is like a mathematical function but with much more features.

Types of functions

  • Library (pre-defined) functions
  • Custom (user-defined) functions

Function definition

A function definition is a subprogram (block of code) that is registered to a function name.

Parts of a function definition

  1. Keyword def
  2. Name of the function
  3. Parameters i.e. variables needed for the function to run
  4. Zero or more return statements
def function_name(parameter1, parameter2 ...):
    first line
    second line
    ...
    last line

Function call

After a function is defined, it can be called to perform the action it was defined for and return (give) a value.

Note: Returning value from a function is different from printing the value on the screen.

Return a value

Just like 3 + 2 returns 5, a function after execution reduces to a single value.

For example, sin(90°) is a function call that returns 1.

Functions that return nothing still return the value None in Python.

Function (example 1)

sin(90°)

  • Function name: sin
  • Parameter: 90°
  • Return value: 1

Python equivalent:

import math  # Importing a module / library
math.sin(math.pi / 2)  # Function call
# 90° = 𝛑/2

It was a pre-defined function. We just used it.

Function (example 2)

This is a user-defined function to calculate celsius to farenheit:

def to_farenheit(celsius):
    farenheit = (9 * celsius) / 5 + 32
    return farenheit

Then we can use it:

to_farenheit(25)  # Convert 25℃ to ℉

Function (example 3)

\[a = \frac {v - u} {t}\]

  • Function name: a (acceleration)
  • Parameters: v, u, t

Python equivalent:

def acceleration(v, u, t):# Function definition
    return (v - u) / t
acceleration(10, 5, 2)    # Function call

Function (example 4)

\[\sum_{i=1}^{10} i\]

  • Function name: \[\sum\] (summation)
  • Parameters: i = 0, 10, i
  • Return value: 55

Python equivalent:

def summation(s, e, f):
    return sum(f(i) for i in range(s, e+1))
summation(1, 10, lambda i: i)

input - Take text input from the keyboard

The input function captures input typed from a keyboard into a string.

\[ Keyboard \xrightarrow{input} \fbox{input()} \xrightarrow{return} String \]

Since it returns a string we can store it in a variable and use it in further computations.

Example 2

\[Keyboard \xrightarrow[3]{input} \fbox{Program} \xrightarrow[3rd]{output} Screen\]

Operators

Why are datatypes required?

Lets assume you want to do '2' + 3 which looks pretty straigtforward and should return 5.

But computers are dumb, they do nothing more and nothing less. It sees them as incompatible datatypes and thus fails to add them using the + operator.

Basically its based on the assumption that strings can contain characters other than numbers so it doesn’t make sense to add them directly. For example, someone could have written 'ab' + 3 which is not at all possible.

So, here Python doesn’t want to spend computing power to check if the string is a number and automatically adding itself.

import math - Import a module

  • This brings in additional functionality which is not available by default
  • Now we can use math.pi, math.cos(0), etc.

math.sin(math.pi / 2) - Calling functions

  1. import math first
  2. Shows output when running