0% found this document useful (0 votes)
3 views

Python Workshop

The document outlines the basics of Python programming, covering topics such as the Python interpreter, data types, control statements, functions, and modules. It also delves into Python data structures, exception handling, file handling, and libraries like NumPy and Pandas. Additionally, it provides examples of various mathematical functions and input/output operations in Python.

Uploaded by

arrowakashcoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Workshop

The document outlines the basics of Python programming, covering topics such as the Python interpreter, data types, control statements, functions, and modules. It also delves into Python data structures, exception handling, file handling, and libraries like NumPy and Pandas. Additionally, it provides examples of various mathematical functions and input/output operations in Python.

Uploaded by

arrowakashcoc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

UNIT I: LANGUAGE BASICS

1.​ Python Interpreter and Interactive Mode


○​ Using the Python Interpreter
2.​ Tokens
○​ Lexical Analysis: Tokens
3.​ Data Types: Numbers and Math Functions
○​ Numeric Types
○​ Math Functions
4.​ Input and Output Operations
○​ Input and Output​
5.​ Comments
○​ Comments in Python
6.​ Reserved Words
○​ Keywords
7.​ Indentation
○​ Indentation Rules
8.​ Operators and Expressions
○​ Expressions and Operators
9.​ Precedence and Associativity
○​ Operator Precedence
10.​Type Conversion
○​ Type Conversion Functions
11.​Debugging and Common Errors in Python
○​ Python Debugger (pdb)

UNIT II: CONTROL STATEMENTS, FUNCTIONS, AND MODULES


1.​ Conditional Statements
○​ if, if-else, nested if
2.​ Loops
○​ for Loop
○​ while Loop
3.​ Break, Continue, and Pass Statements
○​ Break and Continue
○​ Pass Statement
4.​ Functions
○​ Function Definitions
5.​ Variable Scope and Lifetime
○​ Scopes and Namespaces
6.​ Return Statement
○​ Return Statement
7.​ Lambda Functions
○​ Lambda Expressions
8.​ Recursion
○​ Recursion
9.​ Modules and Packages
○​ Modules
○​ Packages

UNIT III: PYTHON DATA STRUCTURES


1.​ Strings
○​ String Methods
2.​ Lists
○​ List Data Structure
3.​ Tuples
○​ Tuples and Sequences
4.​ Sets
○​ Set Data Structure
5.​ Dictionaries
○​ Dictionaries

UNIT IV: EXCEPTION AND FILE HANDLING


1.​ Exceptions
○​ Errors and Exceptions
○​ Handling Exceptions
2.​ File Handling
○​ Reading and Writing Files

UNIT V: NUMPY AND PANDAS


1.​ NumPy
○​ NumPy Documentation
2.​ Pandas
○​ Pandas Documentation
🏷️
Language introduction

🏷️
Python is an interpreter language.

🏷️
Python is a high-level, general-purpose programming language.
Its design philosophy emphasizes code readability with the use of significant

🏷️
indentation.

🏷️
Python is dynamically typed and garbage-collected.
It supports multiple programming paradigms, including structured, object-oriented
and functional programming.

compiler vs interpreter

compiler interpreter

c, java is compiler based language python is interpreter based language

1st compile fully then execute the code line by line compile and execute.

1.​ Basic Math Functions

Function Description Example

math.ceil(x) Rounds up to the nearest integer math.ceil(4.2) → 5

math.floor(x) Rounds down to the nearest math.floor(4.8) → 4


integer

math.trunc(x) Removes decimal part, keeps math.trunc(4.99) → 4


integer part

math.fabs(x) Absolute value (always positive) math.fabs(-7.5) → 7.5

math.fmod(x, y) Remainder of x / y (like % but math.fmod(10, 3) → 1.0


supports float)

math.remainder(x, IEEE remainder of x / y math.remainder(10, 4) →


y) -2.0

2.​ Power & Exponential Functions

Function Description Example


math.sqrt(x) Square root of x math.sqrt(25) → 5.0

math.pow(x, x raised to power y math.pow(2, 3) → 8.0


y)

math.exp(x) Exponential function e^x math.exp(2) → 7.389

math.expm1( e^x - 1, more precise for small math.expm1(0.0001) →


x) values 0.000100005

3.​ Logarithm Functions

Function Description Example

math.log(x) Natural logarithm (ln x, base math.log(10) → 2.302


e)

math.log10(x Logarithm base 10 math.log10(100) → 2.0


)

math.log2(x) Logarithm base 2 math.log2(8) → 3.0

math.log1p(x ln(1 + x), precise for small x math.log1p(0.0001) →


) 0.000099995

4.​ Trigonometric Functions

Function Description Example

math.sin(x) Sine of x (in radians) math.sin(math.pi/2) →


1.0

math.cos(x) Cosine of x (in radians) math.cos(0) → 1.0

math.tan(x) Tangent of x (in radians) math.tan(math.pi/4) →


1.0

math.asin(x) Inverse sine (returns radians) math.asin(1) → 1.5708

math.acos(x) Inverse cosine (returns radians) math.acos(0) → 1.5708

math.atan(x) Inverse tangent (returns radians) math.atan(1) → 0.7854

math.atan2(y, x) Arctangent of y/x, considers math.atan2(1, 1) →


quadrant 0.7854
5.​ Angle Conversion Functions

Function Description Example

math.degrees( Convert radians to math.degrees(math.pi) →


x) degrees 180.0

math.radians(x Convert degrees to math.radians(180) →


) radians 3.14159

6.​ Hyperbolic Functions

Function Description Example

math.sinh(x) Hyperbolic sine math.sinh(0) → 0.0

math.cosh(x) Hyperbolic cosine math.cosh(0) → 1.0

math.tanh(x) Hyperbolic tangent math.tanh(0) → 0.0

math.asinh(x Inverse hyperbolic sine math.asinh(1) → 0.8814


)

math.acosh( Inverse hyperbolic math.acosh(1) → 0.0


x) cosine

math.atanh(x Inverse hyperbolic math.atanh(0.5) →


) tangent 0.5493

7.​ Factorial & Combinatorics

Function Description Example

math.factorial(x) Factorial of x (x!) math.factorial(5) →


120

math.comb(n, k) Number of ways to choose k from n math.comb(5, 2) → 10


(nCk)

math.perm(n, k) Number of permutations (nPk) math.perm(5, 2) → 20

8.​ GCD, LCM, and Modular Arithmetic

Function Description Example

math.gcd(x, Greatest Common Divisor math.gcd(24, 36) →


y) (GCD) 12
math.lcm(x, y) Least Common Multiple (LCM) math.lcm(4, 6) → 12

9.​ Checking Numbers (Infinity, NaN, etc.)

Function Description Example

math.isfinite(x) Checks if x is finite math.isfinite(5.0) → True

math.isinf(x) Checks if x is infinite math.isinf(math.inf) → True

math.isnan(x) Checks if x is NaN (Not a math.isnan(float('nan')) → True


Number)

10.​Special Constants

Constan Value
t

math.pi 3.1415926535897
93

math.e 2.7182818284590
45

math.tau 6.2831853071795
86

math.inf Positive Infinity

math.na Not a Number


n (NaN)

1. Parentheses, Lists, Dictionaries, and Sets


Operator: (expressions...), [expressions...], {key: value...}, {expressions...}
Use: Binding values, list, dict, and set creation.

Example:
a = (1, 2, 3) # Tuple
b = [1, 2, 3] # List
c = {1, 2, 3} # Set
d = {"name": "Alice", "age": 25} # Dictionary
print(a, b, c, d)
Example:
weird_dict = {True: "Yes", 1: "One", 1.0: "Float One", False: "No", 0: "Zero"}
print(weird_dict)
# Output: {True: 'Float One', False: 'Zero'}
# Why? Because True, 1, and 1.0 are considered the same key in a dictionary!

2. Indexing, Slicing, Function Calls, Attribute Reference


Operator: x[index], x[index:index], x(arguments...), x.attribute

Example:
lst = [10, 20, 30, 40]
print(lst[2]) # 30
print(lst[1:3]) # [20, 30]

Example:
def fun():
return (lambda x: x * 2)

double = fun()
print(double(5)) # 10

3. Await Expression (await x)

Example:
import asyncio
async def my_coroutine():
print("Start")
await asyncio.sleep(1)
print("End")

asyncio.run(my_coroutine())

Example:
import asyncio
async def infinite():
while True:
print("Still running...")
await asyncio.sleep(2)
asyncio.run(infinite())

4. Exponentiation (**)
Example:
print(2 ** 3) # 8
Example:
print(2 ** 3 ** 2) # 512
# Because it evaluates as 2 ** (3 ** 2) = 2 ** 9 = 512!

5. Unary Operators (+x, -x, ~x)


Example:
x=5
print(+x, -x, ~x) # 5 -5 -6

Example:
print(~0) # -1
print(~-1) # 0
# Why? Because ~x is equivalent to -(x+1)!

6. Multiplication, Division, Floor Division, Modulus (*, @, /, //, %)


Example:
print(10 * 3) # 30
print(10 / 3) # 3.3333...
print(10 // 3) # 3
print(10 % 3) # 1

Example:
print(3 * "Ha!") # Ha!Ha!Ha!
print("Python" * 2) # PythonPython

7. Addition and Subtraction (+, -)


Example:
print(5 + 3) # 8
print(5 - 3) # 2

Example:
print("5" + "3") # 53 (String Concatenation!)
print([1, 2] + [3, 4]) # [1, 2, 3, 4] (List Concatenation!)

8. Bitwise Shifts (<<, >>)


Example:
print(4 << 1) # 8 (Shift left: 4 * 2)
print(4 >> 1) # 2 (Shift right: 4 / 2)

Example:
print(1024 >> 10) # 1 (Dividing by 1024 using bit shifting!)
9. Bitwise Operators (&, ^, |)
Example:
print(5 & 3) # 1 (Bitwise AND)
print(5 | 3) # 7 (Bitwise OR)
print(5 ^ 3) # 6 (Bitwise XOR)

Example:
print(bin(5 & 3), bin(5 | 3), bin(5 ^ 3))
# 0b1 0b111 0b110 (Binary Representation!)

10. Comparisons and Membership Tests (in, not in, is, is not, ==, !=, >, <, etc.)
Example:
print(5 > 3) # True
print("a" in "apple") # True

Example:
x = [1, 2, 3]
y=x
print(x is y) # True (Same reference)
print(x == y) # True (Same value)

11. Boolean Logic (not, and, or)


Example:
print(not False) # True
print(True and False) # False
print(True or False) # True

Example:
print([] or "Python") # Python (Because empty list is False!)
print([] and "Python") # [] (Because False and anything is False)

12. Conditional Expression (if – else)


Example:
x=5
print("Even" if x % 2 == 0 else "Odd") # Odd

Example:
print("Python"[::2] if len("Python") > 5 else "Short!")
# Output: Pto (Skipping every 2nd character!)

13. Lambda Expressions (lambda)


Example:
double = lambda x: x * 2
print(double(5)) # 10

Example:
print((lambda x, y: x + y)("Hello, ", "World!"))
# Hello, World!

14. Assignment Expression (:=)


Example:
if (n := len("Hello")) > 3:
print(n) # 5

Example:
n=5
print((m := n * 2) + m)
# Output: 20 (Assigns and uses `m` in the same expression!)

input and print function

Input
Formats that can be asked in problem solving
basic single values

input: 1
code: int(input())

input: 12.12
code: float(input())

input: Hello world


code: input()

multiple values in single line


input format

example 1: 1 2 3 4 5
example 2: 12 212 23
example 3: 1.23 2.12 43.3 43.3
example 4: hari jai krish mani

# example 1:
# input: 1 2 3 4 5
li = list(map(int,input().split()))
# example 2:
# input: 12 212 23
li = list(map(int,input().split()))
# example 3:
# input: 1.23 2.12 43.3 43.3
li = list(map(float,input().split()))
# example 4:
# input: hari jai krish mani
li = input().split()

# input: 12 23 34 53 53
# code example 1
li = list(map(int,input().split()))
# code example 2
li = []
temp = input().split()
for item in temp:
li.append(int(item))
print(li) # [12, 23, 34, 53, 53]
# code example 3
li = [int(x) for x in input().split()]
print(li) # [12, 23, 34, 53, 53]

combining n and list items

input:
5
11 2 23 4 56 6

# code example
n = int(input())
li = list(map(int,input().split()))
print(n) # 5
print(li) # [11, 2, 23, 4, 56, 6]

matrix input example

input:
34
11 22 33 44
10 20 30 40
17 17 17 17

# code example
temp = list(map(int,input().split()))
rc = temp[0]
cc = temp[1]
matrix = []
for i in range(rc):
row = list(map(int,input().split()))
matrix.append(row)

Print

# print(*args, sep=' ', end='\n')


# *args
# which means you can pass any number of values.
# sep
# string inserted between values, default a space.
# end
# string appended after the last value, default a newline.

example 1

li = ['samsung', 'iphone','nokia','oppo','vivo']
print(*li,sep=" and ")
# samsung and iphone and nokia and oppo and vivo

example 2

li = [1,2,3,4]
n = len(li)
for i in range(n):
print(li[i],end=",")
# 1,2,3,4,

Data types
Data types
/ \
Numeric string datatype
/ \
int str
float
bool
complex

In python, we don't have following datatypes


- char
- double
- the size of the datatype is dependent on the
virtual memory of the system.

int
+ve, =ve, 0 are called int
size/range is dependent on machine
# some times you want to store money information in code
amount = 1,00,000 # 1 lakh
print(amount) # (1,0,0)

# you can make above code readable by using underscore.


amount = 1_00_000
print(amount) # 100000
# NOTES:
# - only point is, use underscore(_) in-between the digits
# - not in front or rear of digits.

>>> 123 # valid


>>> _123 # invalid
>>> 1_23 # valid
>>> 12_3 # valid

# underscore, ["_"] can be used anywhere it is supported


# but it is only our wish to make use of it in a meaningful
# way
# - we use it to represent huge number for readability
you can represent binary, octal, hexadecimal in integer
# you can use both small and capital letters ( b, B, x, X, o, O )
a = 0b1010 # (or) 0B1010
print(a) # 10
b = 0o10 # (or) 0O10
print(b) # 8
c = 0xA # (or) 0XA
print(c) # 10

>>> 00001
# above code is invalid,
# because, in int, prefix 0 is already used for number representation.
# we can't use like above code. it will through error.

# print function will always by default return the decimal value.


float
float data will only follow "decimal representation"
0b10.10 is wrong becase, here this is meaning less,
and in python you can use 0b, 0B, 0x, 0X, 0o, 0O in integer type only.
1e3 mean 1*10**3, 1 multiple of 10 to power of 3
float will follow only "decimal representation"
we can represent, binary, octal, hexadecimal in float
Hence >>> a = 00001.32 is a valid code.
# rare examples
# below is actually type conversion
>>> float("inf")
inf
>>> float("-inf")
-inf
>>> float("-inf") * 10
-inf
>>> float("nan")
nan
>>> float("infinity")
inf

# in python we can represent infinite like this.


boolean
boolean is a subset of integer
It has 2 vales
Those are True and False,
Where first letter T and F is capital.
Internally True has value 1
and False has value 0
complex
This is all about complex numbers which comes under subject math
examples of valid complex numbers
5+3j
5-3j
3j
5+3J
3.5+3J
3.5+6J
0b1010 + 21j
0x13 + 0x13J
a = 7 + 9j
print(a.real) # 7.0
print(a.imag) # 9.0
# - complex internally uses `float`.
# - Eg,
>>> S= 100 + 3j
>>> S.real
100.0 # which is a float
>>> S.imag
3.0 # which is a float
>>> type(S.real)
<class 'float'>
v1 = 4 + 5j
v2 = 6 + 3j
print(v1 + v2)
print(v1 - v2)
print(v1 * v2)
print(v1 / v2)
print(v1 % v2) # ERROR

in python there is no data type like 'char' ( character )


Because in c, cpp we have a data type called 'char'
In python we can create/represent string in following ways.
pair of single quotes '
pair of double quotes "
triple quotes
pair of 3 single quotes '''
pair of 3 double quotes """
Before proceeding, learn 2 function ord() and chr()

chr() function can convert ordinance to character


ord() function can convert character to ordinance.
Here ordinance is nothing but asciii character.
>>> ord("A") # 65
>>> ord('A') # 65
>>> ord("ABC") # ERROR
# so you can send only string of length 1.
# chr() function is used to convert an integer to it's
# character based on the unicode.
>>> chr(65) # 'A'
>>> chr(97) # 'a'
>>> chr(0x101) # 'ā'
# range allowed in chr function is as below
# 0 to 1114111
>>> chr(-1) # ERROR
# negative values are not allowed.
raw string
raw string are the one.
where it will not take escape sequences characters.
>>> s1 = 'hello \n world`
>>> print(s1)
hello
world
>>>
# above is the output.

## now i want to store \n as characters


# instead of escape sequence.
# to do that we will use raw string.
# - simple add r or R in front of strings.
>>> s1 = r"hello \n world"
>>> print(s1)
hello \n world
usual string
# valid ways to create a string in python
a = 'h'
a = "h"
a = "hello"
a = "" # empty string
a = " " # a single space in string
a = '''line 1
line 2
line 3
line 4
''' # we use triple quotes to store string in that can
# be written in multiple lines
a = "don't" # outside - " and inside - '
a = 'I will never "give up"' # outside - ' and inside - "
a = 'hai\nhello\twhat\'\"\\' # these are escape sequences

REPL
REPL stands for Read-Eval-Print Loop. It is an interactive programming environment
that takes user inputs (one at a time), evaluates them, and returns the result to the
user. This process is repeated in a loop, hence the name "Read-Eval-Print Loop."

comparison operators/relational operator


types

Equality == !=
Relational > < >= <=
Membership in not in
Identity is is not
always above operators will return result in True/False
== Equal To
!= Not Equal To
> Greater Than
< Less Than
>= Greater Than or Equal To
<= Lesser Than or Equal To

in # checks if given item in sequence


not in # checks if given item not in sequence
is # check if left content id is same as right id
is not # check if left content id is not same as right id

NOTE : # we cannot do comparison on complex types.

Lexicographical comparison
# this means, you can compare strings in python
# How this is working ?
# - It will compare each characters ASCII value
print( 'abc' == 'abc' ) # True
print( 'abc' == 'bbc' ) # False
print( 'abc' <= 'bbc' ) # True

>>> 'h' <= 'A' # False


# if s1 > s2 is True, then s1 should come after s2
# if s1 > s2 is False, then s1 should come before s2

"hello" > "hell" # True


"hell" > "hello" # False
"hello" > "hello" # False
"hello" >= "hello" # True
"cab" >= "bac" >= "abc" # True
" " >= " " # True
>>> 6 >= 3 <=1 == 1 <=5 >= True >=0 <= 8
False
# we go from left to right
# every argument is evaluated only once
# if truth hood is decided as "False" then rest of expression is not executed.
#
membership operator
in, not in => works on iteration in strings.
>>> s1 = "welcome"
>>> print("co" in s1)
True
>>> "oc" in s1
False
>>> 'c' in s1
True
>>> 'com' in s1
True
>>> 'on' in s1
False
>>> s1 = "welcome to everyone"
>>> 'to' in s1
True
>>> 'too' in s1
False
>>> 'is' not in s1
True
>>> 'one' in s1
True
>>> 'T' in s1
False
>>> a = 100
>>> 1 in a
# because int is not an iterable.
# iterables example,
# list, strings, tuple

MUTABLE AND IMMUTABLE


Immutable Objects are of in-built datatypes like int, bool, string, Unicode, and tuple.
In simple words, an immutable object can’t be changed after it is created.

Mutable Objects are of type Python list, Python dict, or Python set. Custom classes
are generally mutable.

The content in the object cannot be changed.

if we are trying to change, then a new object will be created with that contents.

note:

creating n no of objects with the same content is a stupid activity. hence to save
memory, we create only one object and all references all pointing to same object.
if one reference is trying to change the content.in the object, other objects will have
impact.
To overcome this problem, content in the object should be modified. This concept is
called immutable.
If any person is trying to change the content in the immutable object, then a new
object is created with new content.

In python, everything is a object (i,e) every variable


Here we have a function called id() => hashcode(address)
which provides the address of the variable.
is

is operator is used to check if a variable are pointing to same object.


is is used to compare reference (or) addresses (or) hashcode.
is​ ===
Meant for reference comparison​ Meant for content comparison
It is used to check both references (or) pointing to same object or not​ It is used
to check both the content or value is same or not
# for int
# only following range will have same id by default
# range : -5 to 256
>>> a = 10
>>> b = 10
>>> a == b
True
>>> a is b
True
>>> id(a), id(b)
(140730277624536, 140730277624536)
----------------------------------------
>>> a = 10.12
>>> b = 10.12
>>> a == b
True
>>> a is b
False
>>> id(a), id(b)
(1184823356912, 1184823356336)
----------------------------------------
>>> a = 10.12
>>> b = a
>>> a == b
True
>>> a is b
True
>>> id(a)
3215396023792
>>> id(b)
3215396023792
multiplication
Rules

1. if a,b is numbers -> multiplication


Below are when repetition happens to string (string)
2. a -> str , b -> (int,bool)
3. a -> (int, bool)m b -> str
# eg: 'hello'*3
gives output as 'hellohellohello'
a = "hello"
b=3
a*b # 'hellohellohello'
b*a # 'hellohellohello'

>>> a = "hi"
>>> a*2
hihi

>>> a = "hi"
>>> a * 2 * 2
"hihi"*2
"hihihihi"
>>> 2 * a * 2
"hihihihi"
>>> 2 * 2 * a
"hihihihi"
# edge cases
>>> a = 'hello'
>>> a * -1
''
>>> a * 0
''
>>> a * True
'hello'
>>> a * False
''
>>> a * 3.5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
logical operators
and
or
not
and

arg1 and arg2


* arg1 is non-zero then result is arg2
* arg1 is 0 then result is arg1
# example
>>> 5 and 10
10
>>> 100 and 0
0
>>> True and False
False
>>> 3.75 and 6.42
6.42
>>> 0 and 100
0
>>> 0 and 0
0
>>> "hello" and "hai"
'hai'
>>> False and True
False
>>> False and False
False
>>> " " and "hai"
'hai'
or

arg1 or arg2
* arg1 is non-zero then result is arg1
* arg1 is 0 then result is arg2
>>> 10 or 5
10
>>> 7 or 0
7
>>> 6.5 or -5
6.5
>>> False or 100
100
>>> False or False
False
>>> 0 or 2.5
2.5
In case of and 1st is 0 don't go next.
In case of or 1st is non-zero then don't go for next.
not

# not True -> False


# not False -> True
# not non-zero => False
# not zero -> True
# not 10 -> False
# not 3.5 -> False
# not False -> True

# in below code,
# == has high priority
# but 10==not is invalid.
>>> a = 10 == not 100
File "<stdin>", line 1
a = 10 == not 100
^^^
SyntaxError: invalid syntax

>>> 10 != (not 100)


True
>>> not 10 == 0
True
>>> not 100 and 20
False
>>> 10 and not 20
False
>>> 0 and not 100
0
# last scenario, as first it is zero.
# python will not check further.
# mostly we use logical operators
# - to compare multiple conditions like
a = 10
b = 20
c = 30
if not ( a>b and b>=c or a==c ):
print('condition satisfied')
>>> 10 and 20 and 0 and 100 and 200
0
>>> 10 and 20 and [] and 20 and (1,2)
[]
# in above cases. It will convert each and every thing into its boolean type then
evaluates. Once it stop evaluation it will through the ans.
# 10 -> bool(10) -> True
# 20 -> bool(20) -> True
# [] -> bool([]) -> False
# 20 -> bool(20) -> True

# As we are using only `and`. Python will stop once it gets


# False ( bool([]) -> False ).
# so only we got output as `[]`
type of errors
syntax error​ Runtime error
No output at all​ runs some code, stop where ever there is a problem
python script is executed from top to bottom, ( controlling is also possible )
python will follow a rule called indentation
every statement should be under a indentation
default indentation is main indentation
# indentation error
print("A")
print("B") # this will come under "main indentation"
# indentation error comes under syntax error, so no output.
a = 10 # we mentioned to python that, we are going to use a.
print(a + b) # error, because, b is not yet defined.
# we didn't told to python, that we are using "b"
Flow control
if
print("A") # main indentation/block
print("B") # main indentation/block
if True: # main indentation/block
print("C") # IF indentation/block
print("D") # IF indentation/block
print("E") # main indentation/block
print("F") # main indentation/block
if syntax
if keyword followed by condition,
where condition can be an valid expression, or variable or anything
that can be converted to boolean.
Python finally checked if it is True or False.
if is True. then executes IF block
if is False. then executes ELSE Block
# some invalid indentation codes.

# code 01
print(10)
if (3>2):
print(20)
print(30) # this will not comes under either main/if block.
print(40)

# code 02
print(10)
if (3>2):
print(20)
print(30) # this will not comes under either main/if block.
print(40)

# code 03
print(10)
if (3>2): # expected an indented block after 'if' statement
print(20)
print(30)
print(40)
some if code scenario's

# code 01
print(10)
if (3>2):
pass # this is used, when you don't want to write a code now.
# but you will write in future.
# pass will do nothing.
print(20)
print(30)
print(40)

# code 02
print(10)
if (): # empty brackets are always valid,
# is consider False,
# Because it means, empty tuple.
print(20)
print(30)

# code 03
print(10)
if :# End up with syntax error.
# Because, bracket is mandatory
print(20)
print(30)
else
# WAP to find max of 2 numbers
a = int(input())
b = int(input())
if a > b:
res = a
else:
res = b
print(res)

# alternate solution
a = int(input())
b = int(input())
res = (a>b)*a + (b>a)*b
print(res)
# some valid indentation codes.

# code 01
print(10)
if (3>2):
print(20) # this will comes under if block
print(25)
else:
print(26)
print(30) # this will not comes under either else block.
print(40)

# 💡 In python switch concept is not there. 💡


# But later match is introduced...
# But now we can use elf for doing switch logic.
key = input() # W A S D
if key == "W":
print("move forward")
elif key=="S":
print("move backward")
elif key == "A":
print("move left")
elif key == "D":
print("move right")
else: # it is optional to write else here.
print("invalid key")
while
#12345
#1234
#123
#12
#1

col = 5
row = 5
c=1
while col <= 5: # outer while
r=1
while r <= r-c: # inner while
print(b, end=" ")
b=b+1
print()
a=a+1

for
In Python, a for loop allows you to repeat a block of code a specific number of times.
When using range(), you can control how many times the loop will run.

Syntax
for variable in range(start, stop, step):
# code to repeat
start (optional): The number where the loop starts (default is 0).
stop (required): The number where the loop stops. This is not included in the loop.
step (optional): The amount the loop counter increases by after each iteration
(default is 1).
Example 1: Basic Loop with range(stop)
for i in range(5):
print(i)
Explanation:

The loop starts at 0 and ends at 4 (since 5 is not included).

Output:

0
1
2
3
4
Example 2: Loop with range(start, stop)
for i in range(2, 6):
print(i)
Explanation:
The loop starts at 2 and ends at 5 (because 6 is not included).

Output:

2
3
4
5
Example 3: Loop with range(start, stop, step)
for i in range(0, 10, 2):
print(i)
Explanation:

The loop starts at 0, ends at 8 (since 10 is not included), and increments by 2 each
time.

Output:

0
2
4
6
8
Example 4: Loop in Reverse with range()
for i in range(5, 0, -1):
print(i)
Explanation:

The loop starts at 5, ends at 1, and decreases by 1 each time.

Output:

5
4
3
2
1
break
break keyword will only work inside
the for, while ( loops )
# some other examples
a=1
while a <=10:
print(a)
if a == 4:
break
a=a+1

# some other examples


# example - 01
a=1
while a <=10:
print(a)
if a == 4:
break
a=a+1
else:
print("loop completed")

# example - 02
a=1
while a <=10:
print(a)
a=a+1
else:
print("loop completed")
# find prime no
n = int(input())
i=2
isPrime = True
while i<(n**0.5):
if n%i == 0:
isPrime = False
break
i=i+1

if isPrime:
print("is Prime")
else:
print("is not a Prime")

# find prime no
n = int(input())
i=2
while i<(n**0.5):
if n%i == 0:
print("Not a prime")
break
i=i+1
else:
print("is a Prime")

# NOTE: If break executed, then else not get executed.


continue
continue keyword will only work inside
the for, while ( loops )
diff continue and break in Python:

Concept​ break​ continue


Purpose​ Exits the loop entirely.​ Skips the current iteration and moves to the
next.
Effect​ Terminates the loop when executed.​ Skips the remaining code in the
current iteration, but the loop continues.
When to use​ When you want to stop the loop early.​ When you want to skip certain
iterations but continue looping.
Example use​Exiting a loop when a condition is met.​ Skipping an iteration based on
a condition.
Example using continue and break together
for i in range(10):
if i == 3:
continue # Skip when i is 3
if i == 8:
break # Exit the loop when i is 8
print(i)
Explanation:

The loop prints numbers from 0 to 9.


When i is 3, continue skips the rest of the code for that iteration, so 3 is not printed.
When i is 8, break terminates the loop, so 8 and anything beyond it are not printed.
Output:

0
1
2
4
5
6
7
pass
In Python, the `pass` statement is a placeholder that does nothing. It’s used when a
statement is syntactically required but you don’t want to execute any code. Typically,
you use `pass` in situations where you plan to add code later or you want to skip a
block without causing errors.
Common Uses of pass
Empty Function or Class: You can use pass when you want to define a function or
class but aren’t ready to implement the logic yet.
Placeholders in Conditional Statements: Sometimes in if, for, or while statements,
you may want to skip certain conditions temporarily.
Example 1: pass in Conditional Statements
for i in range(5):
if i == 3:
pass # Do nothing when i is 3
else:
print(i)
Output:

0
1
2
4
In this example, when i is 3, the pass statement is executed, meaning the loop does
nothing for that iteration, but continues with the next.

conditional operator
is a concept used, literally a short form of if else
Technically we can call it as "conditional expression"
But it act as an expression
a = int(input())
b = int(input())
res = a if a > b else b
print(res)

# syntax
# a if a>b else b
# [True res] [if keyword] [ condition] [else keyword] [False res]
# to put in simple terms.
# res1 if exp else res2
🔹
1️⃣ Magic Auto-Filled Excel (Pandas + ExcelWriter)

🔹
Concept: Automatically generate and save an Excel sheet with random data!
Magic Trick: Use Pandas to create and fill Excel sheets instantly.

Code:
import pandas as pd
import numpy as np

# Generate random data


data = {
"ID": np.arange(1, 11),
"Name": np.random.choice(["Alice", "Bob", "Charlie", "David", "Emma"], 10),
"Score": np.random.randint(50, 100, 10)
}

df = pd.DataFrame(data)

# Save to Excel
df.to_excel("magic_data.xlsx", index=False)
print("Excel file 'magic_data.xlsx' created successfully!")

🔹
2️⃣ Predict Future Sales (Pandas + Excel)

🔹
Concept: Predict future sales using moving averages in Excel!
Magic Trick: Automate calculations and save results to Excel.

Code:
import pandas as pd
import numpy as np

# Generate sample sales data


dates = pd.date_range(start="2024-01-01", periods=30)
sales = np.random.randint(100, 500, size=30)

df = pd.DataFrame({"Date": dates, "Sales": sales})

# Calculate moving average


df["7-Day Avg"] = df["Sales"].rolling(window=7).mean()

# Save to Excel
df.to_excel("sales_forecast.xlsx", index=False)
print("Excel file 'sales_forecast.xlsx' created with predictions!")
🔹
3️⃣ Excel Password Cracker (SHA-256 Decryption)

🔹
Concept: Decode a hashed password from Excel!
Magic Trick: Hashes passwords and verifies matches automatically.

Code:
import pandas as pd
import hashlib

# Load Excel file


df = pd.read_excel("passwords.xlsx")

# Hash function
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()

# Check passwords
df["Hashed_Password"] = df["Password"].apply(hash_password)

# Save updated file


df.to_excel("hashed_passwords.xlsx", index=False)
print("Passwords hashed successfully in 'hashed_passwords.xlsx'!")

🔹
4️⃣ Excel-Based Lottery Number Picker

🔹
Concept: Generate random lottery numbers and save them to Excel!
Magic Trick: NumPy picks lucky numbers for you.

Code:
import pandas as pd
import numpy as np

# Generate random lottery numbers


lottery_data = {
"Game": ["Lotto"] * 10,
"Numbers": [sorted(np.random.choice(range(1, 50), 6, replace=False)) for _ in
range(10)]
}

df = pd.DataFrame(lottery_data)

# Save to Excel
df.to_excel("lottery_numbers.xlsx", index=False)
print("Lucky numbers saved in 'lottery_numbers.xlsx'!")
🔹
5️⃣ Auto-Clean Messy Excel Data

🔹
Concept: Remove duplicates, missing values, and outliers from Excel data.
Magic Trick: Pandas auto-cleans your spreadsheet in seconds!

Code:
import pandas as pd

# Load messy Excel file


df = pd.read_excel("messy_data.xlsx")

# Drop missing values


df_cleaned = df.dropna()

# Remove duplicates
df_cleaned = df_cleaned.drop_duplicates()

# Save cleaned file


df_cleaned.to_excel("cleaned_data.xlsx", index=False)
print("Cleaned data saved in 'cleaned_data.xlsx'! ")

🔹
6️⃣ Create an Animated Excel Graph

🔹
Concept: Make a real-time animated graph from Excel data.
Magic Trick: Update and refresh charts automatically.

Code:
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# Load sales data


df = pd.read_excel("sales_data.xlsx")

# Create plot
fig, ax = plt.subplots()
ax.set_xlabel("Date")
ax.set_ylabel("Sales")

def update(i):
ax.clear()
ax.plot(df["Date"][:i], df["Sales"][:i], marker="o", linestyle="-")
ax.set_title(f"Sales Over Time (Frame {i})")

ani = FuncAnimation(fig, update, frames=len(df), repeat=False)


plt.show()
🔹
7️⃣ Secret Messages in Excel

🔹
Concept: Hide and reveal secret messages inside Excel.
Magic Trick: Convert text to ASCII numbers and back!

Code:
import pandas as pd

# Load secret messages from Excel


df = pd.read_excel("secrets.xlsx")

# Encrypt message (convert to ASCII)


df["Secret_Code"] = df["Message"].apply(lambda x: " ".join(str(ord(c)) for c in x))

# Decrypt message (convert back)


df["Revealed"] = df["Secret_Code"].apply(lambda x: "".join(chr(int(i)) for i in x.split()))

# Save results
df.to_excel("decoded_messages.xlsx", index=False)
print("Secret messages encoded & decoded!")

🌍
🔹
8️⃣ Auto-Translate Excel Data

🔹
Concept: Translate entire Excel columns to another language!
Magic Trick: Use Google Translate inside Excel.

Code:
import pandas as pd
from deep_translator import GoogleTranslator

# Load Excel file


df = pd.read_excel("translate.xlsx")

# Translate text
df["Translated"] = df["Text"].apply(lambda x: GoogleTranslator(source='auto',
target='fr').translate(x))

# Save to Excel

🌍
df.to_excel("translated.xlsx", index=False)
print("Excel data translated successfully! ")
🔹
1️⃣ Magic Excel Data Filler (NumPy)

🔹
Concept: Automatically fill Excel with random structured data.
NumPy Trick: Generate random numbers and choices instantly!

Code:
import numpy as np
import pandas as pd

# Generate random structured data


num_rows = 10
data = {
"ID": np.arange(1, num_rows + 1),
"Age": np.random.randint(18, 60, size=num_rows),
"Salary ($)": np.random.randint(30000, 100000, size=num_rows),
"Score": np.random.uniform(50, 100, size=num_rows).round(2)
}

df = pd.DataFrame(data)

# Save to Excel
df.to_excel("magic_filled_data.xlsx", index=False)
print("Excel file 'magic_filled_data.xlsx' created with random data! ")

🔹
2️⃣ Auto-Detect Outliers in Excel (NumPy)
Concept: Find suspicious values in Excel using NumPy’s mean & standard

🔹
deviation.
NumPy Trick: Auto-detects outliers in a column.

Code:
import numpy as np
import pandas as pd

# Load Excel file


df = pd.read_excel("sales_data.xlsx")

# Detect outliers in the 'Sales' column


sales = df["Sales"].to_numpy()
mean, std = np.mean(sales), np.std(sales)

# Outliers are values beyond (mean ± 2*std)


outliers = df[np.abs(sales - mean) > 2 * std]

# Save outliers to Excel


outliers.to_excel("outliers_detected.xlsx", index=False)
print("Outliers detected and saved in 'outliers_detected.xlsx'! ")

🔹
3️⃣ Excel-Based Lucky Number Generator (NumPy)

🔹
Concept: Generate random lottery numbers in an Excel file.
NumPy Trick: No loops needed!

Code:

import numpy as np
import pandas as pd

# Generate random lottery numbers (6 unique numbers per row)


num_rows = 10
lottery_numbers = [sorted(np.random.choice(range(1, 50), 6, replace=False)) for _ in
range(num_rows)]

df = pd.DataFrame(lottery_numbers, columns=["Num1", "Num2", "Num3", "Num4",


"Num5", "Num6"])

# Save to Excel
df.to_excel("lottery_numbers.xlsx", index=False)
print("Lucky numbers saved in 'lottery_numbers.xlsx'! ")

🔹
4️⃣ Excel-Based Realistic Date Generator (NumPy)

🔹
Concept: Create random but realistic dates in Excel.
NumPy Trick: Use np.datetime64 for instant date calculations.

Code:

import numpy as np
import pandas as pd

# Generate random dates within a range


num_rows = 15
start_date = np.datetime64('2023-01-01')
end_date = np.datetime64('2024-12-31')

random_dates = start_date + np.random.randint(0, (end_date -


start_date).astype(int), num_rows)

df = pd.DataFrame({"Random Date": random_dates})

# Save to Excel
df.to_excel("random_dates.xlsx", index=False)
print("Random dates saved in 'random_dates.xlsx'! ")

🔹
5️⃣ Auto-Categorize Excel Data (NumPy)

🔹
Concept: Categorize Excel data automatically based on conditions.
NumPy Trick: Use vectorized operations for speed!

Code:
import numpy as np
import pandas as pd

# Load Excel data


df = pd.read_excel("employees.xlsx")

# Categorize salaries into groups using NumPy


salary = df["Salary"].to_numpy()
df["Category"] = np.select(
[salary < 40000, salary < 70000, salary >= 70000],
["Low", "Medium", "High"]
)

# Save categorized data

🏷️")
df.to_excel("categorized_employees.xlsx", index=False)
print("Employees categorized in 'categorized_employees.xlsx'!

🔹
6️⃣ Auto-Generate Fake People for Excel (NumPy)

🔹
Concept: Fill Excel with realistic fake names, emails, and salaries.
NumPy Trick: Instant data generation!

Code:

import numpy as np
import pandas as pd

# Fake data generation


num_rows = 10
names = np.array(["Alice", "Bob", "Charlie", "David", "Emma", "Frank", "Grace",
"Hannah", "Ian", "Jack"])
domains = np.array(["gmail.com", "yahoo.com", "outlook.com"])

df = pd.DataFrame({
"Name": np.random.choice(names, num_rows),
"Email": [f"{name.lower()}@{np.random.choice(domains)}" for name in
np.random.choice(names, num_rows)],
"Salary ($)": np.random.randint(35000, 100000, num_rows)
})

# Save to Excel

👥")
df.to_excel("fake_people.xlsx", index=False)
print("Fake people saved in 'fake_people.xlsx'!

🏆
🔹
7️⃣ Automatic Excel Performance Ranker (NumPy)

🔹
Concept: Rank employees based on scores instantly in Excel.
NumPy Trick: Use argsort() for super-fast ranking!

Code:

import numpy as np
import pandas as pd

# Load Excel file


df = pd.read_excel("employee_scores.xlsx")

# Rank employees based on their scores


scores = df["Score"].to_numpy()
df["Rank"] = np.argsort(-scores) + 1 # Negative for descending order

# Save to Excel

🏆")
df.to_excel("ranked_employees.xlsx", index=False)

✅ 🏅
print("Employees ranked in 'ranked_employees.xlsx'!
Magic: Instantly sort & rank thousands of rows!

🔹
8️⃣ Magic Image Generator in Excel (NumPy)

🔹
Concept: Generate random pixelated images and save the color values to Excel.
NumPy Trick: Use 3D arrays to store RGB values!

Code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Create a 10x10 random RGB image


image = np.random.randint(0, 256, (10, 10, 3))

# Convert to DataFrame
df = pd.DataFrame([["({}, {}, {})".format(*image[i, j]) for j in range(10)] for i in
range(10)])
# Save pixel values to Excel
df.to_excel("random_image.xlsx", index=False, header=False)

# Show the image


plt.imshow(image)
plt.axis("off")
plt.show()

NumPy

NumPy (Numerical Python) is one of the fundamental libraries for scientific


computing in Python.

It provides support for large multi-dimensional arrays and matrices, along with a large
collection of high-level mathematical functions to operate on these arrays.

It is an essential library for data science, machine learning, artificial intelligence,


numerical simulations, and more.

Key Features of NumPy

1.​ Powerful N-Dimensional Array Object: NumPy provides the ndarray object,
which is a fast, memory-efficient array of fixed-size items.
2.​ Broadcasting: It allows for operations on arrays of different shapes, making it
possible to perform element-wise operations even on arrays of differing
dimensions.
3.​ Mathematical Functions: NumPy contains a wide range of mathematical
operations such as linear algebra, Fourier transforms, random sampling, and
more.
4.​ Memory Efficiency: NumPy arrays consume less memory compared to Python
lists.
5.​ Integration with Other Libraries: NumPy seamlessly integrates with libraries
like Pandas, SciPy, and Matplotlib.

1. NumPy Array: ndarray

The primary object in NumPy is the ndarray (N-dimensional array), which is a grid of
values, all of the same type. An ndarray can be of any dimension.

Creating a NumPy Array

You can create an ndarray using np.array():


python

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr)

Output:

[1 2 3 4]

For multi-dimensional arrays, you can use nested lists:

python

arr_2d = np.array([[1, 2], [3, 4], [5, 6]])

print(arr_2d)

Output:

[[1 2]

[3 4]

[5 6]]

Array Shape and Dimensions

●​ Shape: The number of elements in each dimension of the array.


●​ ndim: The number of dimensions.

python

print(arr.shape) # (4,)

print(arr_2d.shape) # (3, 2)

print(arr.ndim) # 1

print(arr_2d.ndim) # 2
Data Type of Array

You can check the type of elements in an ndarray using dtype:

print(arr.dtype) # int64

2. NumPy Array Creation Functions

NumPy provides various methods to create arrays:

np.zeros(): Creates an array filled with zeros.​


zeros_arr = np.zeros((2, 3)) # 2x3 array of zeros

print(zeros_arr)

●​

np.ones(): Creates an array filled with ones.​


ones_arr = np.ones((2, 3)) # 2x3 array of ones

print(ones_arr)

●​

np.eye(): Creates an identity matrix.​


eye_arr = np.eye(3) # 3x3 identity matrix

print(eye_arr)

●​

np.arange(): Creates an array with a range of values, similar to Python's range().​


range_arr = np.arange(0, 10, 2) # Array from 0 to 10 with a step of
2

print(range_arr)

●​

np.linspace(): Creates an array of evenly spaced values over a specified range.​


linspace_arr = np.linspace(0, 1, 5) # 5 values between 0 and 1
print(linspace_arr)

●​

np.random.random(): Creates an array of random numbers.​


rand_arr = np.random.random((2, 2)) # 2x2 array of random floats
between 0 and 1

print(rand_arr)

●​

3. Array Indexing and Slicing

NumPy arrays can be indexed and sliced just like Python lists, but with additional
capabilities like multi-dimensional slicing.

1D Array Indexing

arr = np.array([10, 20, 30, 40, 50])

print(arr[2]) # Access third element (30)

2D Array Indexing

arr_2d = np.array([[10, 20, 30], [40, 50, 60]])

print(arr_2d[1, 2]) # Access element in second row, third column


(60)

Slicing Arrays

# Slicing 1D array

print(arr[1:4]) # Elements from index 1 to 3 ([20, 30, 40])

# Slicing 2D array

print(arr_2d[0, :2]) # First row, first two elements ([10, 20])


Advanced Indexing

You can use Boolean indexing or fancy indexing (using an array of indices).

# Boolean indexing

mask = arr > 30

print(arr[mask]) # Output: [40 50]

# Fancy indexing

indices = [0, 3, 4]

print(arr[indices]) # Output: [10 40 50]

4. Array Operations

NumPy arrays support element-wise operations, which means operations are


performed on each element individually.

Mathematical Operations

arr = np.array([1, 2, 3, 4])

# Element-wise addition

print(arr + 10) # Output: [11 12 13 14]

# Element-wise subtraction

print(arr - 2) # Output: [-1 0 1 2]

# Element-wise multiplication

print(arr * 3) # Output: [3 6 9 12]


# Element-wise division

print(arr / 2) # Output: [0.5 1. 1.5 2. ]

Universal Functions (ufuncs)

NumPy provides ufuncs (universal functions), which are vectorized functions that
operate on arrays element-wise.

arr = np.array([1, 2, 3, 4])

# Square root

print(np.sqrt(arr)) # Output: [1. 1.41421356 1.73205081 2. ]

Other common ufuncs include np.exp(), np.log(), np.sin(), etc.

Aggregating Operations

NumPy provides a set of aggregation functions to compute statistics over arrays:

arr = np.array([1, 2, 3, 4, 5])

# Sum of elements

print(np.sum(arr)) # Output: 15

# Mean of elements

print(np.mean(arr)) # Output: 3.0

# Standard deviation

print(np.std(arr)) # Output: 1.4142135623730951


# Minimum and maximum values

print(np.min(arr)) # Output: 1

print(np.max(arr)) # Output: 5

5. Reshaping Arrays

You can reshape arrays without changing their data using reshape(), flatten(),
and similar methods.

Reshape

arr = np.array([1, 2, 3, 4, 5, 6])

# Reshape to 2x3 array

reshaped_arr = arr.reshape(2, 3)

print(reshaped_arr)

Output:

[[1 2 3]

[4 5 6]]

Flatten

To convert a multi-dimensional array to a 1D array:

arr_2d = np.array([[1, 2], [3, 4]])

flattened = arr_2d.flatten()

print(flattened)
Output:

[1 2 3 4]

Transpose

You can transpose an array (swap rows and columns) using transpose() or .T.

arr_2d = np.array([[1, 2], [3, 4]])

transposed = arr_2d.T

print(transposed)

Output:

[[1 3]

[2 4]]

6. Random Sampling

NumPy has a comprehensive suite of functions for random number generation, from
uniform distribution to normal distribution.

Generate Random Numbers

# Generate 5 random numbers between 0 and 1

rand = np.random.random(5)

print(rand)

Random Integers

# Generate random integers between 0 and 10

rand_ints = np.random.randint(0, 10, size=5)


print(rand_ints)

Normal Distribution

# Generate random numbers from a normal distribution with mean 0 and


std deviation 1

rand_norm = np.random.randn(5)

print(rand_norm)

7. Linear Algebra in NumPy

NumPy has a set of linear algebra functions that are essential for scientific
computing.

Dot Product

arr1 = np.array([1, 2])

arr2 = np.array([3, 4])

# Dot product of two vectors

dot_product = np.dot(arr1, arr2)

print(dot_product) # Output: 11

Matrix Multiplication

arr1 = np.array([[1, 2], [3, 4]])

arr2 = np.array([[5, 6], [7, 8]])

# Matrix multiplication (dot product of 2D arrays)

matrix_product = np.matmul(arr1, arr2)


print(matrix_product)

Output:

[[19 22]

[43 50]]

Inverse of a Matrix

arr = np.array([[1, 2], [3, 4]])

# Inverse of a matrix

inv_arr = np.linalg.inv(arr)

print(inv_arr)

Output:

[[-2. 1. ]

[ 1.5 -0.5]]

NumPy is a fundamental library for numerical computing in Python, offering powerful


tools for working with arrays and matrices. From basic array creation and
manipulation to advanced linear algebra and random number generation, NumPy is a
must-have library for anyone involved in scientific computing, machine learning, or
data science. Its simplicity, speed, and versatility make it the core of the Python
scientific ecosystem.
Pandas is an open-source Python library primarily used for data manipulation and analysis.
It is built on top of the NumPy library, providing easy-to-use data structures and data
analysis tools. Pandas is widely used in data science and machine learning for tasks such as
cleaning, transforming, analyzing, and visualizing data.

Pandas provides two main data structures:

1.​ Series (for one-dimensional data)


2.​ DataFrame (for two-dimensional data)

In this guide, we'll cover the core features of Pandas in detail, including its data structures,
operations, and key methods.

1. Key Features of Pandas

●​ Data Structures: Pandas provides two powerful data structures: Series and
DataFrame, which are designed to handle a variety of data formats and types.
●​ Handling Missing Data: Pandas has built-in support for handling missing data, such
as NaN values, with options for imputation or deletion.
●​ Label-based Indexing: Unlike traditional arrays, Pandas allows you to use labels
(like strings) to index and access data, which enhances flexibility and clarity.
●​ Data Alignment: It automatically aligns data when performing operations on multiple
datasets.
●​ Time Series Support: Pandas offers comprehensive tools for working with time
series data, such as date parsing, resampling, and shifting.
●​ Flexible I/O: Pandas can read from and write to various data formats, such as CSV,
Excel, HDF5, SQL, and more.

2. Data Structures in Pandas

a. Series

A Series is a one-dimensional labeled array capable of holding any data type. It is similar to
a Python list, but with additional features like the ability to label its elements (using indices).
Creating a Series:
import pandas as pd

# Create a Series from a list


data = [10, 20, 30, 40]
series = pd.Series(data)
print(series)

Output:

0 10
1 20
2 30
3 40
dtype: int64

By default, the index is the sequence of integers starting from 0. You can also assign custom
indices:

index_labels = ['a', 'b', 'c', 'd']


series_custom = pd.Series(data, index=index_labels)
print(series_custom)

Output:

a 10
b 20
c 30
d 40
dtype: int64

b. DataFrame

A DataFrame is a two-dimensional labeled data structure. It is similar to a table or an Excel


spreadsheet, with rows and columns.

Creating a DataFrame:

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],


'Age': [24, 27, 22, 32],
'City': ['New York', 'Los Angeles', 'Chicago', 'Miami']}

df = pd.DataFrame(data)
print(df)

Output:

Name Age City


0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago
3 David 32 Miami

●​ Columns: Name, Age, and City are the columns of the DataFrame.
●​ Index: By default, the index is a sequence of integers (0, 1, 2, 3, etc.).

3. Indexing and Selecting Data

Pandas offers powerful ways to index and select data in a Series or DataFrame.

a. Accessing Columns

You can access a column by using its name as a key (just like dictionary access):

# Accessing a single column


print(df['Name'])

Output:

0 Alice
1 Bob
2 Charlie
3 David
Name: Name, dtype: object

b. Accessing Rows

You can access rows using the iloc[] method (integer location-based indexing) or the
loc[] method (label-based indexing).
# Accessing the first row using iloc
print(df.iloc[0])

# Accessing the first row using loc


print(df.loc[0])

Output (for both methods):

vbnet
Copy code
Name Alice
Age 24
City New York
Name: 0, dtype: object

c. Slicing DataFrames

You can slice DataFrames by specifying ranges of rows and columns.

# Slicing rows and columns


print(df.iloc[1:3, 0:2]) # Rows 1-2, Columns 0-1

Output:

Name Age
1 Bob 27
2 Charlie 22

d. Conditional Selection

You can use conditional expressions to filter data based on specific conditions.

# Select rows where Age is greater than 25


filtered_df = df[df['Age'] > 25]
print(filtered_df)

Output:

Name Age City


1 Bob 27 Los Angeles
3 David 32 Miami

4. Handling Missing Data

Pandas has robust support for handling missing data (NaN).

Detecting Missing Data

You can use isnull() or notnull() to detect missing values.

df.isnull() # Detects missing values


df.notnull() # Detects non-missing values

Filling Missing Data

You can fill missing data using the fillna() method.

df_filled = df.fillna(value='Unknown') # Fill NaN values with


'Unknown'

Dropping Missing Data

You can drop rows or columns containing missing values using dropna().

df_dropped = df.dropna() # Drop any rows with missing values

5. Data Operations and Transformations

Pandas supports a wide range of operations for transforming and manipulating data.

a. Adding Columns

You can add new columns to a DataFrame by assigning values to them.

df['Country'] = ['USA', 'USA', 'USA', 'USA']


print(df)

Output:
Name Age City Country
0 Alice 24 New York USA
1 Bob 27 Los Angeles USA
2 Charlie 22 Chicago USA
3 David 32 Miami USA

b. Dropping Columns

You can drop columns using the drop() method.

df_dropped_col = df.drop('Country', axis=1) # axis=1 means column


print(df_dropped_col)

Output:

Name Age City


0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago
3 David 32 Miami

c. Renaming Columns

You can rename columns using the rename() method.

df_renamed = df.rename(columns={'Name': 'Full Name', 'City': 'City


Name'})

print(df_renamed)

Output:

Full Name Age City Name


0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago
3 David 32 Miami
6. Grouping and Aggregation

Pandas provides powerful methods for grouping and aggregating data, similar to SQL
group-by operations.

a. GroupBy

You can group data based on a column and perform aggregation.

# Group by 'City' and calculate the mean age for each city
grouped = df.groupby('City')['Age'].mean()
print(grouped)

Output:

City
Chicago 22.0
Los Angeles 27.0
Miami 32.0
New York 24.0
Name: Age, dtype: float64

b. Aggregation with Multiple Functions

You can apply multiple aggregation functions simultaneously using agg().

grouped = df.groupby('City').agg({'Age': ['mean', 'max', 'min']})


print(grouped)

Output:

Age
mean max min
City
Chicago 22.0 22 22
Los Angeles 27.0 27 27
Miami 32.0 32 32
New York 24.0 24 24

7. Merging, Joining, and Concatenating


Pandas provides several methods for combining datasets.

a. Merging DataFrames

The merge() method allows you to merge two DataFrames based on common columns

df1 = pd.DataFrame({'Key': ['A', 'B', 'C'], 'Value': [1, 2, 3]})


df2 = pd.DataFrame({'Key': ['B', 'C', 'D'], 'Value': [4, 5, 6]})

merged_df = pd.merge(df1, df2, on='Key', how='inner') # Inner join


print(merged_df)

Output:

Key Value_x Value_y


0 B 2 4
1 C 3 5

b. Concatenating DataFrames

You can concatenate multiple DataFrames along rows or columns using concat().

concatenated = pd.concat([df1, df2], axis=0) # Concatenate along


rows (default)
print(concatenated)

Output:

Key Value
0 A 1
1 B 2
2 C 3
0 B 4
1 C 5
2 D 6

8. Time Series in Pandas


Pandas has extensive support for time series data, which includes date parsing, resampling,
and frequency conversion.

a. Date Parsing

You can convert strings to datetime objects using pd.to_datetime().

dates = ['2021-01-01', '2021-01-02', '2021-01-03']


dates_parsed = pd.to_datetime(dates)
print(dates_parsed)

Output:

DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03'],


dtype='datetime64[ns]', freq=None)

b. Resampling Time Series

You can resample data at different frequencies (e.g., daily to monthly).

# Resample daily data to monthly and calculate the sum


df_resampled = df.resample('M', on='Date').sum()

Pandas is an extremely powerful and versatile library for data manipulation and analysis. Its
ability to handle various types of data structures, ease of use for time series data, and
support for a wide range of operations (including merging, grouping, and transforming data)
makes it indispensable in the data science ecosystem. Whether you're performing simple
data cleaning or more complex analysis, Pandas provides the tools needed to perform these
tasks efficiently and with high performance.
EXTRA -

matplotlib.pyplot is a module in the Matplotlib library, which is widely used in Python for
creating static, interactive, and animated visualizations. It provides a MATLAB-like interface
for plotting by encapsulating various plotting functionalities and offering a high-level API for
creating plots, graphs, and charts.

Key Features of matplotlib.pyplot


Easy Plotting Interface:

It simplifies plotting tasks with a MATLAB-inspired procedural interface.


You can create, modify, and customize plots using concise and intuitive function calls.
Stateful Interface:

Works like a state machine, where pyplot maintains the state of the current figure and axes
(subplots).
Each function operates on the current figure or axes by default, unless specified otherwise.
Wide Range of Plot Types:

Line plots, scatter plots, bar plots, histograms, pie charts, error bars, box plots, and more.
Customization:

Supports a wide range of customization options for figure size, colors, markers, line styles,
labels, legends, and more.
Interactive Features:

Provides interactivity with zooming, panning, and custom callbacks.


Core Functions of matplotlib.pyplot
Here are some of the commonly used functions in pyplot:

1. Figure Management
plt.figure(figsize=(width, height)): Creates a new figure.
plt.subplots(): Creates a figure with subplots.

2. Plotting
plt.plot(x, y, fmt): Creates a line plot.
plt.scatter(x, y, s): Creates a scatter plot.
plt.bar(x, height): Creates a bar plot.
plt.hist(data, bins): Plots a histogram.
plt.pie(sizes): Creates a pie chart.

3. Customizing Plots
plt.title("Title"): Sets the title of the plot.
plt.xlabel("Label"), plt.ylabel("Label"): Sets axis labels.
plt.legend(): Adds a legend.
plt.grid(True): Adds a grid.

4. Displaying and Saving


plt.show(): Displays the current figure.
plt.savefig("filename.ext"): Saves the figure to a file.
5. Subplot Management
plt.subplot(rows, cols, index): Creates a subplot in a grid.
plt.subplots_adjust(): Adjusts spacing between subplots.
Example Usage

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

# Creating a plot
plt.figure(figsize=(8, 5)) # Create a figure
plt.plot(x, y, label="Prime numbers", color="blue", marker="o")

# Customizing
plt.title("Line Plot Example")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
plt.legend()
plt.grid(True)

# Show plot
plt.show()

1️⃣ Built-in Functions


Functio Description Example
n

print( Displays output print("Hello, World!")


)

type() Returns the type of a variable type(42) → <class 'int'>


id() Returns the memory address of an id(x)
object

input( Takes user input name = input("Enter your


) name: ")

len() Returns length of an object len("Python") → 6

abs() Returns absolute value abs(-10) → 10

round( Rounds a number round(3.14159, 2) → 3.14


)

pow() Returns power of a number pow(2, 3) → 8

sorted Returns sorted list sorted([3, 1, 2]) → [1,


() 2, 3]

2️⃣ Data Type Conversion Functions


Functio Description Example
n

int() Converts to integer int("10") → 10

float( Converts to float float("3.14") → 3.14


)

str() Converts to string str(100) → "100"

list() Converts to list list("abc") → ['a', 'b', 'c']

tuple( Converts to tuple tuple([1, 2, 3]) → (1, 2, 3)


)

set() Converts to set set([1, 2, 2, 3]) → {1, 2, 3}

dict() Converts to dict([(1, "a"), (2, "b")]) → {1:


dictionary "a", 2: "b"}

3️⃣ Math Functions


Functio Description Example
n

min() Returns the minimum min(3, 1, 2) → 1


value

max() Returns the maximum max(3, 1, 2) → 3


value

sum() Returns the sum of sum([1, 2, 3]) →


elements 6

divmod Returns quotient & divmod(10, 3) →


() remainder (3, 1)

hex() Converts to hexadecimal hex(255) →


'0xff'

bin() Converts to binary bin(10) →


'0b1010'

oct() Converts to octal oct(8) → '0o10'

4️⃣ String Functions


Functio Description Example
n

upper( Converts to uppercase "hello".upper() → "HELLO"


)

lower( Converts to lowercase "HELLO".lower() → "hello"


)

strip( Removes whitespace " hello ".strip() →


) "hello"

replac Replaces a substring "hello".replace("h", "y")


e() → "yello"

split( Splits a string "a,b,c".split(",") → ['a',


) 'b', 'c']
join() Joins elements into a ",".join(['a', 'b', 'c'])
string → "a,b,c"

5️⃣ List Functions


Functio Description Example
n

append Adds an item to the end lst.append(4


() )

insert Inserts item at index lst.insert(1


() , 99)

pop() Removes and returns last lst.pop()


item

remove Removes first occurrence lst.remove(3


() )

revers Reverses list order lst.reverse(


e() )

sort() Sorts list lst.sort()

6️⃣ Dictionary Functions


Functio Description Example
n

keys() Returns keys d.keys()

values Returns values d.values()


()

items( Returns key-value d.items()


) pairs

get() Gets value of a key d.get("key")

update Updates dictionary d.update({"new_key


() ": 5})
7️⃣ Set Functions
Function Description Example

add() Adds an element s.add(4)

remove() Removes an element s.remove(2)

union() Combines sets s1.union(s2)

intersecti Common elements s1.intersectio


on() n(s2)

difference Elements in one but not in s1.difference(


() other s2)

8️⃣ File Handling Functions


Functio Description Example
n

open() Opens a file open("file.txt"


, "r")

read() Reads file file.read()


content

write( Writes to file file.write("Hel


) lo")

close( Closes file file.close()


)

9️⃣ Lambda & Functional Programming


Functio Description Example
n

lambda Anonymous function square = lambda x: x**2

map() Applies function to map(str.upper, ["a", "b"])


items
filter Filters elements filter(lambda x: x > 10,
() [5, 15, 25])

reduce Reduces to single reduce(lambda x, y: x + y,


() value [1, 2, 3])

🔟 Miscellaneous Functions
Function Description Example

enumerat Returns index and value enumerate(["a",


e() "b"])

zip() Combines iterables zip([1,2],


["a","b"])

any() Returns True if any item is any([0, 1, False])


True → True

all() Returns True if all items are all([1, True, 3])


True → True

Operation Description Syntax / Example

Opening a Opens a file in different modes file =


File (read, write, append, etc.). open("file.txt", "r")

Modes 'r' (read), 'w' (write), 'a' file =


(append), 'x' (create), 'b' open("file.txt", "w")
(binary), 't' (text), '+' (read &
write).

Reading a Reads content from a file. content = file.read()


File

Reading Reads one line at a time. line =


Line-by-Line file.readline()

Reading All Reads all lines into a list. lines =


Lines file.readlines()
Writing to a Writes data to a file (overwrites if file.write("Hello,
File exists). World!")

Appending to Adds data to the end of a file. file =


a File open("file.txt", "a")
file.write("\nNew
line")

Using with Ensures proper closure of file. with open("file.txt",


Statement "r") as file:
content =
file.read()

Closing a File Manually closes a file. file.close()

Checking if Verifies if a file is closed. file.closed


File is Closed

File Cursor Gets or sets the position of the file.seek(0)


Position cursor. pos = file.tell()

Deleting a Removes a file from the system. import os


File os.remove("file.txt")

Checking if Ensures a file exists before import os


File Exists deleting or opening. if
os.path.exists("file.
txt"):

os.remove("file.txt")

lambda arguments: expression

# A lambda function that doubles the input


double = lambda x: x * 2
print(double(5)) # Output: 10

You might also like