Skip to content

datasciencekibaatein/Python-Tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Python Tutorial By - Datascience ki Baatein

Why We Use Python + Its Role in Data Science

Python has become one of the most popular programming languages. Here's why:

βœ… 1. Easy to Learn & Read

β€’ Simple syntax (close to English).

β€’ Great for beginners and pros alike.

Example: Hello World

print("Hello, World!")

βœ… 2. Versatile

β€’ Can be used in web development, automation, AI, data analysis, machine learning, etc.

βœ… 3. Huge Community Support

β€’ Tons of tutorials, solutions, and discussions online.

βœ… 4. Powerful Libraries

β€’ Rich set of libraries for every use case: data, AI, visualization, web, etc.

Role of Python in Data Science

Python is the #1 language for Data Science. Here's how it fits into each stage:

πŸ“Š 1. Data Collection

β€’ Scrape data from the web using BeautifulSoup, Scrapy.

β€’ Collect API data using requests.

import requests
response = requests.get("https://api.github.com")
print(response.json())

πŸ“ˆ 2. Data Cleaning & Preparation

β€’ Use pandas and numpy to clean, filter, and preprocess data. import pandas as pd df = pd.read_csv("data.csv") df.dropna(inplace=True)

πŸ“‰ 3. Data Analysis

β€’ Analyze trends, find patterns, and summarize data.

print(df.describe())
print(df.groupby("Gender")["Salary"].mean())

πŸ“Š 4. Data Visualization

β€’ Use matplotlib, seaborn, or plotly to create graphs and dashboards.

import matplotlib.pyplot as plt
df['Salary'].hist()
plt.show()

🧠 5. Machine Learning

β€’ Use scikit-learn, TensorFlow, or PyTorch to build models.

from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(X, y)

πŸ€– 6. Deployment

β€’ Deploy models via Flask, FastAPI, or cloud services (AWS, Heroku).

Variables, Data Types, Comments & Docstrings

πŸ”Ή 1. Variables in Python

Variables are used to store data. You don’t need to declare the type; Python figures it out.

Syntax:

name = "Alice"
age = 25
height = 5.6

β€’ name is a string

β€’ age is an integer

β€’ height is a float

Rules for Variable Names:

β€’ Must start with a letter or underscore _

β€’ Cannot start with a number

β€’ Case-sensitive (name β‰  Name)

πŸ”Ή 2. Python Data Types

Type Example Description
int 10, -3 Whole numbers
float 3.14, -7.2 Decimal numbers
str "hello" Text
bool True, False Boolean (logical) values
list [1, 2, 3] Ordered, changeable collection
tuple (1, 2, 3) Ordered, immutable collection
dict {"name": "John"} Key-value pairs
set {1, 2, 3} Unordered, no duplicates

Example:

x = 42             # int
pi = 3.14          # float
name = "Python"    # str
is_active = True   # bool
colors = ["red", "blue", "green"]  # list

πŸ”Ή 3. Comments in Python

Comments help explain the code. They are ignored by the interpreter. βœ… Single-line Comment:

# This is a comment
name = "Alice"  # This is also a comment

βœ… Multi-line Comment (Hacky Way):

# This is a multi-line comment
# You can continue using #
# on every line

πŸ”Ή 4. Docstrings (Documentation Strings)

Docstrings are multi-line strings used to describe what a function/class/module does. They’re enclosed in triple quotes (''' or """).

βœ… Example:

def greet(name):
    """
    This function greets the user with the given name.
    """
    print(f"Hello, {name}!")

You can access the docstring using:

print(greet.__doc__)

Output & Type Conversion

πŸ”Ή 1. Input in Python input() is used to take user input from the keyboard. It always returns a string. βœ… Example:

name = input("Enter your name: ")
print("Hello", name)

πŸ“ Note: You need to convert input to integer/float if needed (explained in type conversion below).


πŸ”Ή 2. Output in Python print() is used to display output. βœ… Example:

print("Hello, world!")
βœ… Multiple Values:
name = "Alice"
age = 25
print("Name:", name, "| Age:", age)

βœ… Using f-strings (modern & readable):

print(f"My name is {name} and I am {age} years old.")

πŸ”Ή 3. Type Conversion in Python Used to convert data from one type to another. βœ… Common Functions:

Function Converts To
int() Integer
float() Float
str() String
bool() Boolean

πŸ”Ή 4. Examples of Type Conversion πŸ§ͺ Convert string input to integer:

age = input("Enter your age: ")
age = int(age)  # now it's an integer
print(f"Next year you’ll be {age + 1}")

πŸ§ͺ Float to int:

num = 3.99
num_int = int(num)
print(num_int)  # Output: 3

πŸ§ͺ Int to float:

x = 5
print(float(x))  # Output: 5.0

πŸ§ͺ String to float:

rate = float(input("Enter interest rate: "))
print(rate * 2)

βœ… Bonus: Check Type

x = "123"
print(type(x))  # Output: <class 'str'>

y = int(x)
print(type(y))  # Output: <class 'int'>

🧠 Mini Practice

name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"{name}, you were born in {2025 - age}")

Operators

Operators are symbols or words used to perform operations on variables and values.


βœ… Categories of Operators

Type Used For
Arithmetic Operators Basic math
Assignment Operators Assigning values
Comparison Operators Comparing values
Logical Operators Combining conditions
Identity Operators Object identity
Membership Operators Membership testing
Bitwise Operators Binary operations

πŸ”Ή 1. Arithmetic Operators

Used for mathematical operations.

Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 5 / 2 2.5
// Floor Division 5 // 2 2
% Modulus (remainder) 5 % 2 1
** Exponentiation 2 ** 3 8

πŸ”Ή 2. Assignment Operators

Used to assign values to variables.

Operator Example Same As
= x = 5 Assign 5 to x
+= x += 3 x = x + 3
-= x -= 2 x = x - 2
*= x *= 3 x = x * 3
/= x /= 2 x = x / 2
//= x //= 2 x = x // 2
%= x %= 2 x = x % 2
**= x **= 2 x = x ** 2

πŸ”Ή 3. Comparison Operators

Used to compare values. Returns True or False.

Operator Description Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5 > 3 True
< Less than 3 < 5 True
>= Greater or equal 5 >= 5 True
<= Less or equal 5 <= 6 True

πŸ”Ή 4. Logical Operators

Used to combine conditional statements.

Operator Description Example Result
and True if both are True True and False False
or True if at least one is True True or False True
not Reverses the result not True False

πŸ”Ή 5. Identity Operators

Used to compare memory locations (not just values).

Operator Description Example Result
is True if both refer to same object a is b True/False
is not True if not the same object a is not b True/False
πŸ§ͺ Example:
a = [1, 2]
b = a
print(a is b)        # True (same memory)
print(a == b)        # True (same value)

c = [1, 2]
print(a is c)        # False
print(a == c)        # True

πŸ”Ή 6. Membership Operators

Used to test whether a value is in a sequence.

Operator Description Example Result
in True if value exists "a" in "cat" True
not in True if value doesn’t 3 not in [1, 2] True

πŸ”Ή 7. Bitwise Operators (Advanced)

Used for binary operations on integers.

Operator Description Example Result
& AND 5 & 3 1
` ` OR `5
^ XOR 5 ^ 3 6
~ NOT ~5 -6
<< Left Shift 5 << 1 10
>> Right Shift 5 >> 1 2

🧠 Practice Examples:

# Arithmetic
a, b = 10, 3
print(a + b, a % b, a ** b)

# Comparison
print(a > b, a == 10)

# Logical
print((a > 5) and (b < 5))

# Identity
x = [1, 2]
y = [1, 2]
print(x is y, x == y)

# Membership
print("e" in "hello", 2 in [1, 2, 3])

Control Flow Statements Control flow statements allow you to control the execution of your Python code based on conditions and loops.


1. Conditional Statements

πŸ”Ή if, elif, and else These statements are used to make decisions in Python. βœ… Syntax:

if condition:
    # block of code
elif another_condition:
    # block of code
else:
    # block of code

βœ… Example:

age = 18

if age >= 18:
    print("You are eligible to vote.")
elif age > 0:
    print("You are too young to vote.")
else:
    print("Invalid age")

πŸ”Έ Nested if:

num = 10
if num > 0:
    if num % 2 == 0:
        print("Positive Even")
    else:
        print("Positive Odd")

2. Loops in Python

πŸ”Ή for Loop

Used to iterate over a sequence (list, string, range, etc.) βœ… Example 1: Iterating a list

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)

βœ… Example 2: Using range()

for i in range(1, 6):
    print(i)

πŸ”Ή while Loop

Runs as long as the condition is True. βœ… Example:

count = 0
while count < 5:
    print("Count:", count)
    count += 1

πŸ” 3. Loop Control Statements

These help manage flow inside loops.

πŸ”Έ break: Exit the loop early

for i in range(10):
    if i == 5:
        break
    print(i)

πŸ”Έ continue: Skip the current iteration

for i in range(5):
    if i == 2:
        continue
    print(i)

πŸ”Έ pass: Placeholder (does nothing)

for i in range(3):
    pass  # used when code is not yet written

πŸ”‚ 4. else with Loops

You can use an else block with loops. It runs only if the loop doesn't break. βœ… Example:

for i in range(5):
    print(i)
else:
    print("Loop finished normally.")

βœ… Example with break:

for i in range(5):
    if i == 3:
        break
    print(i)
else:
    print("Won’t print because loop was broken.")

🧠 Mini Project: Even/Odd Counter

nums = [1, 4, 7, 8, 10, 13]
even = 0
odd = 0

for num in nums:
    if num % 2 == 0:
        even += 1
    else:
        odd += 1

print(f"Even numbers: {even}")
print(f"Odd numbers: {odd}")

πŸ“Œ Summary Table

Statement Purpose
if / elif / else Decision making
for Loop through iterable
while Repeat until condition is false
break Exit loop early
continue Skip current iteration
pass Do nothing (placeholder)
else (with loop) Run if loop wasn’t broken

Python Tutorial: Data Structures in Python (with Key Methods)

πŸ“˜ What are Data Structures? Data structures are ways to store and organize data so it can be used efficiently. Python’s Built-in Data Structures:

  1. List
  2. Tuple
  3. Set
  4. Dictionary
  5. String (optional, sometimes treated as a data structure)

πŸ”Ή 1. List

A list is an ordered, mutable collection.

βœ… Create a list:

fruits = ['apple', 'banana', 'cherry']

βœ… Key Methods:

Method Description
append() Add item to end
insert() Insert at a position
pop() Remove and return item
remove() Remove specific item
sort() Sort the list
reverse() Reverse the list
index() Find index of an item
count() Count occurrences

πŸ§ͺ Example:

fruits.append("mango")
fruits.insert(1, "orange")
print(fruits.pop())
print(fruits.index("banana"))

πŸ”Ή 2. Tuple

A tuple is an ordered, immutable collection. βœ… Create a tuple:

colors = ('red', 'green', 'blue')

βœ… Key Methods:

Method Description
count() Count occurrences of value
index() Return first index of value

πŸ§ͺ Example:

print(colors.count("red"))
print(colors.index("blue"))

πŸ”Ή 3. Set

A set is an unordered, mutable, no-duplicates collection.

βœ… Create a set:

nums = {1, 2, 3, 4}

βœ… Key Methods:

Method Description
add() Add an element
update() Add multiple elements
remove() Remove element (error if not found)
discard() Remove if present
clear() Remove all elements
union() Return union of sets
intersection() Return common elements
difference() Return difference

πŸ§ͺ Example:

nums.add(5)
nums.update([6, 7])
nums.remove(2)
nums2 = {5, 6, 8}
print(nums.union(nums2))

πŸ”Ή 4. Dictionary

A dictionary is an unordered collection of key-value pairs.

βœ… Create a dict:

student = {
    "name": "Alice",
    "age": 21,
    "course": "Python"
}

βœ… Key Methods:

Method Description
get() Get value for key
keys() All keys
values() All values
items() All key-value pairs
update() Update with another dict
pop() Remove and return value by key
clear() Remove all items

πŸ§ͺ Example:

print(student.get("name"))
student.update({"age": 22})
print(student.keys())
student.pop("course")

πŸ”Ή 5. String (as a bonus)

Strings are immutable sequences of characters.

βœ… Create a string:

text = "hello python"

βœ… Key Methods:

Method Description
upper() Convert to uppercase
lower() Convert to lowercase
split() Split into list
replace() Replace text
find() Find index of substring
strip() Remove leading/trailing spaces

πŸ§ͺ Example:

print(text.upper())
print(text.replace("python", "world"))
print(text.split())

🧠 Mini Practice

# List
shopping = ["milk", "bread"]
shopping.append("eggs")
shopping.remove("milk")

# Tuple
coords = (10, 20)
print("X:", coords[0])

# Set
unique_nums = set([1, 2, 2, 3])
print(unique_nums)

# Dict
book = {"title": "Python", "pages": 300}
print(book["title"])

Structure Ordered Mutable Duplicate Allowed Example Type
List βœ… βœ… βœ… []
Tuple βœ… ❌ βœ… ()
Set ❌ βœ… ❌ {}
Dictionary ❌ βœ… ❌ (on keys) {key: val}

Python OOPs Tutorial – Beginner to Intermediate

πŸ”· What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm that models real-world entities as objects, with attributes (data) and behaviors (methods).


4 Core Pillars of OOP:

Pillar Description
Encapsulation Binding data and methods in a class
Abstraction Hiding internal details, showing only essentials
Inheritance Reusing code from parent class
Polymorphism Same method name behaves differently in classes

βœ… 1. Class & Object

πŸ”Ή Class: Blueprint for creating objects πŸ”Ή Object: Instance of the class

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def start(self):
        print(f"{self.brand} {self.model} is starting...")

# Creating an object
my_car = Car("Toyota", "Camry")
my_car.start()

βœ… 2. Encapsulation

Protects data by restricting access using methods.

class BankAccount:
    def __init__(self, owner, balance):
        self.owner = owner
        self.__balance = balance  # private variable

    def deposit(self, amount):
        self.__balance += amount

    def get_balance(self):
        return self.__balance

acc = BankAccount("Alice", 1000)
acc.deposit(500)
print(acc.get_balance())  # 1500

βœ… 3. Abstraction

Hides complex internal logic and shows only the interface.

from abc import ABC, abstractmethod

class Animal(ABC):
    @abstractmethod
    def sound(self):
        pass

class Dog(Animal):
    def sound(self):
        return "Bark"

d = Dog()
print(d.sound())

βœ… 4. Inheritance

One class inherits properties from another.

class Vehicle:
    def __init__(self, brand):
        self.brand = brand

    def start(self):
        print(f"{self.brand} is ready to go!")

class Bike(Vehicle):
    def ring_bell(self):
        print("Ring Ring!")

b = Bike("Hero")
b.start()
b.ring_bell()

βœ… 5. Polymorphism

Same function name behaves differently. πŸ”Ή With Inheritance

class Bird:
    def sound(self):
        print("Some generic sound")

class Parrot(Bird):
    def sound(self):
        print("Squawk")

class Crow(Bird):
    def sound(self):
        print("Caw")

for bird in (Parrot(), Crow()):
    bird.sound()
πŸ”Ή Operator Overloading
class Book:
    def __init__(self, pages):
        self.pages = pages

    def __add__(self, other):
        return self.pages + other.pages

b1 = Book(100)
b2 = Book(200)
print(b1 + b2)  # 300

🧠 Important OOP Concepts Quick View

Concept Syntax Feature
Constructor init() method
Access Modifiers self.__private
Abstraction from abc import ABC,Inheritance class B(A)
Polymorphism Method Overriding/Overloading

πŸš€ Mini Project: Student Management System

We'll create a small project that allows:

  1. Adding students
  2. Viewing student details
  3. Calculating average marks
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
        self.marks = []

    def add_mark(self, mark):
        self.marks.append(mark)

    def get_average(self):
        if self.marks:
            return sum(self.marks) / len(self.marks)
        return 0

    def display_info(self):
        print(f"Name: {self.name}")
        print(f"Roll No: {self.roll}")
        print(f"Marks: {self.marks}")
        print(f"Average: {self.get_average():.2f}")


# Driver code
students = []

while True:
    print("\n1. Add Student\n2. Show Students\n3. Exit")
    choice = input("Enter choice: ")

    if choice == "1":
        name = input("Enter name: ")
        roll = input("Enter roll no: ")
        s = Student(name, roll)
        for i in range(3):
            mark = int(input(f"Enter mark {i+1}: "))
            s.add_mark(mark)
        students.append(s)

    elif choice == "2":
        for s in students:
            s.display_info()

    elif choice == "3":
        break

    else:
        print("Invalid choice")

About

This repository contains a complete Python tutorial designed specifically for aspiring Data Scientists, Analysts, and Machine Learning enthusiasts. Whether you're a beginner or brushing up your skills, this resource covers Python from scratch to advanced topics with a focus on data science applications.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors