In Python, everything is an object, and data types are classes.
Variables are instances of
these classes. Python provides a set of built-in data types to classify and store values
appropriately.
Main Categories of Data Types in Python:
Numeric Types
Sequence Types
Boolean Type
Set Type
Dictionary Type
A. Numeric Data Types
Numeric data types store numeric values and are of three types:
1. Integer (int)
o Whole numbers (positive or negative) without decimals.
o Example: x = 10, y = -5
2. Float (float)
o Real numbers with decimal points.
o Example: pi = 3.14, temp = -98.6
3. Complex (complex)
o Consists of a real part and an imaginary part ( j is used).
o Example: z = 2 + 3j
B. Sequence Data Types
These types store multiple values in an ordered manner.
1. String (str)
A sequence of characters enclosed in single ('), double ("), or triple (''') quotes.
Strings are immutable (cannot be changed after creation).
Example:
name = "Alice"
char = 'A' # Even single characters are strings
paragraph = '''This is
a multiline string.'''
2. List (list)
An ordered, mutable collection that can hold items of different types.
Use square brackets [ ].
Example:
my_list = [1, "Hello", 3.5]
my_list[0] = 10 # Lists are mutable
3. Tuple (tuple)
Similar to lists but immutable (cannot be modified).
Defined using parentheses () or just commas.
Example:
my_tuple = (1, 2, 3)
another_tuple = 1, 2, 3 # also valid
C. Dictionary (dict)
A collection of key-value pairs.
Keys must be unique and immutable.
Defined using curly braces {}.
Example:
student = {"name": "Ali", "age": 20}
print(student["name"]) # Output: Ali
D. Set (set)
Unordered collection of unique elements.
Use curly braces {} or the set() function.
Example:
numbers = {1, 2, 3, 2} # Output: {1, 2, 3}
2. Introduction to Data Science
What is Data Science?
An interdisciplinary field combining statistics, machine learning, algorithms, and
systems to extract knowledge from data (structured or unstructured).
Helps in decision-making through insights and visualization.
Core Elements of Data Science:
Big Data
Machine Learning
Artificial Intelligence
Data Mining
Statistical Analysis
Visualization
Why It Matters?
Deals with the explosion of data from various sources (social media, sensors, apps).
Converts raw data into meaningful knowledge.
Applications: Healthcare, Finance, AI, Marketing, Engineering, etc.
3. Loops in Python
Loops are used to repeat a block of code multiple times.
Types of Loops:
A. for Loop
Used to iterate over a sequence (list, tuple, string, dictionary, etc.)
Syntax:
for item in iterable:
# do something
Example:
for i in range(5):
print(i) # prints 0 to 4
B. while Loop
Repeats a block while a condition is true
Syntax:
while condition:
# do something
Example:
i = 0
while i < 5:
print(i)
i += 1
Loop Control Statements:
break: Exit the loop early.
continue: Skip the current iteration.
pass: Placeholder, does nothing.
4. Functions in Python
Functions are reusable blocks of code designed to perform a specific task.
Defining Functions:
def function_name(parameters):
# code block
return result
Calling a Function:
function_name(arguments)
Types of Functions:
Built-in functions: print(), len(), type(), etc.
User-defined functions: Functions you create.
Example:
def greet(name):
return f"Hello, {name}!"
print(greet("Mudassir")) # Output: Hello, Mudassir!
Default & Keyword Arguments:
def add(a=0, b=0):
return a + b
print(add(b=3)) # Output: 3
*args and **kwargs:
*args: Accepts any number of positional arguments.
**kwargs: Accepts any number of keyword arguments.
5. Types of Errors in Python
Python errors can be divided into three broad categories:
1. Syntax Errors
Occur when Python code is not written correctly.
Example:
if True # Missing colon
print("Yes")
2. Runtime Errors
Occur while the program is running.
Example: Division by zero, file not found.
x = 10 / 0 # ZeroDivisionError
3. Logical Errors
Code runs but produces incorrect output.
Hardest to detect because there is no error message.
Example:
def square(x):
return x * 2 # Wrong logic
Conclusion
These notes cover:
Python data types in depth (numeric, sequence, dictionary, set)
A complete beginner’s guide to data science
All necessary details about loops and functions
All types of errors you may face in Python
Let me know if you'd like these compiled into a PDF or formatted for printing.