PMDS508L - Python Programming
Module – 2 – Introduction to Python
Prepared by: Dr. Nandhini S, SAS
Topics covered:
• Indentation, Variables, Basice Data types, Operators and their
precedence
• Expressions
• Buil-in functions
• Importing from Libraries
Indentation:
• Python uses indentation to define blocks of code.
• Indentation is compulsory and usually 4 spaces.
Example:1
x = 25
if x > 15:
print("x is greater than 15")
if x % 2 == 1:
print("x is odd")
else:
print("x is even")
else:
print("x is 10 or less")
Variables:
• A variable stores a value in a named memory location.
• In Python no need to declare variable types, since it is dynamically taken
by Python.
Basic Data types:
Type Description Example
int Integer numbers x=20
float Decimal numbers Var=99.99
complex Complex numbers Z=3-4j
bool Boolean (True/False) flag = True
str String/Text Msg=”Hello”
list Mutable sequence x = [1, 2, 3]
tuple Immutable sequence y = (1, 2, 3)
set Unordered unique elements s = {1, 2, 3}
dict Key-value mapping d = {"a": 1}
Operators and their Precedence:
1. ()
2. **
3. +x, -x
4. *, /, //, %
5. +, -
6. Comparisons
7. not, and, or
Example:2
a = 10, b = 5, c = 2.
Evaluate: a + b * c ** 2 > 25 and not (a % c == 0 or b // c < 3)
Output: False
Example:3
x = 4, y = 3, z = 2
Evaluate: (x + y > z * 2) or not (x & y == 2) and x! = y
Output: True
Expressions:
An expression is a combination of values, variables, and operators that
evaluates to a result.
Mutability:
• Value of the object can be changed after creation. (Mutable)
• Value of the object cannot be changed once created. (Immutable)
Type Mutable
int No
float No
bool No
str No
tuple No
list Yes
dict Yes
set Yes
Example:4
x = 100
print(id(x)) # Prints the memory address
x = x +25
print(x)
print(id(x)) # Prints different memory address
Example:5
list1 = [3,4,5,6]
list1[0] = 2 # Modify element
[Link](7) # Add element
print(list1) # Output: [2,3,4,5,6,7]
Built-in Functions:
Function Purpose Example
print() Output to screen print("Hello World")
type() Check data type type(9.999)
len() Length of a len("India")
sequence
input() Accept user input input("Enter the
Name: ")
int(), str() Type conversion int("555")
abs() Absolute value abs(-77)
round() Rounding round(3.1756, 2)
max(), min() Max/Min of max(100,222, 23)
elements
sum() Sum of elements sum([11, 20, 3])
Importing from Libraries:
Example:6
# Import entire module
import math
print([Link](25))
# Import specific function
from math import pi, pow
print(pow(2, 3))
# Import with alias
import numpy as np
arr = [Link]([1, 2, 3])
print([Link](arr))
Python Libraries – Overview
Some Standard Libraries (Built-in)
• math, random, datetime, os, sys, statistics, json
Third-party Libraries (need pip install)
• numpy – numerical arrays
• pandas – data analysis
• matplotlib, seaborn – visualization
• scikit-learn – machine learning
• requests – HTTP requests
• flask, django – web development
How to Explore Libraries:
• List of Standard libraries: [Link]
• List of Third-party packages: [Link]
• View Installed packages: Run ”pip list” in terminal
• View all importable modules: Use `help("modules") in Python shell
==================== END ====================