PYTHON Khurramshahzad
PYTHON Khurramshahzad
1. Turning on your PC and compatible windows or 1. Basics-intermediate of other python libraries for
other OS each task
2. Installation of python and other programs 2. Basics of Statistics for Data Science
necessary 3. Logic building in Python for Data Science
for the course 4. How to search public Datasets and work on them
3. Writing your first line of code with us to build your portfolio
4. Basic python programming skills 5. GitHub Basics-Intermediate
5. What is Data? And how to handle large Data? 6. Basic to Advance- Markdown
6. Data Science Basics vs. Data Analytics Basics 7. How to get free resources of worth (90k USD if
7. Python for Data Science you have a student ID)?
8. Basics-intermediate of Pandas library 8. Basic to Advance Data Visualization with Python
9. Basics-intermediate of NumPy library 9.Machine Learning of Traditional Algorithms
10. Basics-intermediate of matplotlib library 10. Basic to Intermediate-Deep Learning
3. 4.
Use folders to group related files: Follow a consistent naming convention:
Group files logically into folders based on their • Consistency is key. If you start with a convention
functionality orl like
stage in the data science pipeline. verb_noun (e.g., clean_data.py), maintain it
• Example: data/, scripts/, models/, notebooks/ throughout your
project.
5. 6.
Add versioning when appropriate: Avoid special characters and spaces:
• For version control, especially when iterating on • Use only underscores (_) or hyphens (-) in names.
models, Avoid
you can append version numbers or dates. using spaces or special characters, as these can cause
• Example: model_vl .py, model_v2.py, issues in various systems.
data_cleaning_20231001 .py • Do not add any other special character @, #, %, $
etc.
7. 8.
Avoid using names that are reserved Python Use Limited length:
keywords or • Keep file and folder names short but descriptive to
built-in functions: avoid
• DO not use names that conflict with Python built-in long paths. Use concise names where possible.
functions or keywords (e.g. list, dict. input, print, • Instead of train-random-forest-classification-
etc.) model.py,
• Avoid naming a file list.py or a variable input as it Use train_rf_model.py
conflicts with Python built-in functionality.
TRUE. FALSE etc.,
9. 10.
Avoid starting names with numbers: Use plural forms for collections:
• File names should not begin with numbers, as this • Use plural names for directories/folders containing
can multiple files of the same type (e.g., scripts/,
cause issues with some systems and tools. datasets/)
• Instead of 123_data.py, use data_123.py • scripts/, models/, plots/
• You can end it with numbers for versioning.
11.
Use README files:
• Use a README.md in each folder to explain the contents,
structure, and any specific instructions.
Variables
A variable is a symbolic name that references or points to an object in memory.
• It allows programmers to Store data and manipulate it throughout the code,
• Think of a variable as a labeled box where you can place eta and retrieve it when needed.
In Python, declaring a variable is straightforward. You simply assign a value
to a variable name using the = operator.
# Variable assignment
x = 10
name = "Alice"
is_active = True
• x, name, and is_active are variables.
• 10, "Alice", and True are the values assigned to these variables.
Note: Unlike some Other programming languages, Python does not
require you to declare the type of a variable explicitly.
Types of Variables
l. Integer (int) 2. Floating-point numbers (float)
Integers are whole numbers, positive or negative, Float represents that have a decimal point or in
without decimal points, exponential form.
# Example of an integer variable # Example Of a float variable
age = 25 temperature = 36.6
print(type(age)) # Output: < class ' int' > print (type (temperature)) # Output: < Class ' float' >
3. String (str) 4. Boolean (bool)
Strings are Sequences of Characters enclosed in Booleans represent one Of two values: True or False.
Single (‘) Of double (“) quotes, # Example Of a Boolean variable
# Example of a string variable is_sunny = True
greeting = "Hello, World!" print(type(is_sunny)) # Output:< Class ‘Bool’ >
print(type(greeting)) #Output: <Class 'str'>
5. List (list) 6. Tuple (tuple)
Lists are Ordered, mutable collections of items. Tuples are ordered, immutable collections of items,
which Can Of any data type. Lists are defined using Tuples are defined using parentheses ().
square brackets []. # Example of a tuple variable
# Example of a list variable coordinates = (10.0, 20.0)
Numbers = [1, 2, 3, 4, 5] print (type(coordinates)) # Output:< Class ‘tuple’ >
print(type(Numbers)) # Output: (Class ‘list’)
7. Dictionary (dict) 8. Set (set)
Dictionaries are unordered collections of key-value Sets are unordered collections of unique items,
pairs, defined using curly braces {}. defined using curly braces {} or the set ( ) function.
# Example of a dictionary variable # Example of a set variable
person = {"name": "Alice". "age": 30} fruits = {"apple", "banana","orange"}
print(type(person)) # < Output: Class 'dict'> print(type(fruits)) # Output: <class ‘set’>
None (None Type) 10. Complex (complex)
None represents the absence of a value and is an Complex numbers are numbers with a real and
Object Of its own datatype. imaginary part. The imaginary part is denoted by j
# Examee of NoneType variable # Example of a complex number variable
result = None complex_num = 3+4j
print(type(none)) # Output: <class ‘NoneType’ > print(type(complex_num) # Output: < Class
‘complex’ >
Type Casting
Type casting in Python is the process of converting one data type into another.
• This is useful when you need to convert between different types, such as turning an integer into a string or a
float into an integer
• Python provides several built-in functions to accomplish type casting.
Benefits
1. Interoperability: Type casting allows seamless 2. Data Cleaning: Data from various soves such as
interaction between systems that represent data APIs, files, and user inputs often come in a non-ideal
differently (e.g., strings vs. integers). When users format. Type casting helps normalize this data for
input numeric values as strings, casting them to further analysis, calculations, or visualizations.
integers or floats allows the data to be processed for
calculations.
3. Error Prevention; Converting Can prevent errors 4, Efficiency in Data Processing: In data-heavy
during operations that expect specific data types. For applications like sensor readings or financial
example, performing mathematical on strings without transactions, converting data to the appropriate types
casting them to numbers would result in errors. (e.g., float for precision) ensures the Correct analysis
and reduces processing time.
l. Casting ‘int’ to ‘float’ 2. Casting ‘float’ to ‘int’
You can convert an integer to a floating-point When converting float to an integer, the decimal part
number. is truncated (not rounded).
# Example # Example
num_int = 10 num_ float = 9.8
num_float = float (num_int) num_int = int (num_float)
print (num_ftoat) # Output: 10.0 print(num_int) # Output: 9
print(type(num_float)) # Output: <Class ‘float’> print (type (num_int ) ) # Output: <Class ‘int’>
5. Casting ‘list’ to ‘tuple’ and ‘vice versa’ 6. Casting list to set and ‘vice versa’
Lists can be converted into tuples and vice versa. You can convert a list into a set, which removes
# Example: List to tuple duplicates.
my_list = [1,2,3] # Example: List to set
my_tuple_to_list = tuple(my_list) my_list = [1,2,3]
print(my_tuple_to_list) # Output: (1,2,3) my_list_to_set = set(my_list)
# Example: Tuple to list print(my_list_to_set) # Output: {1,2,3}
my_list_to_tuple = list(my_tuple) # Example: Tuple to list
print(my_list_to_tuple) # Output: [1,2,3] my_set_To_list = list(my_tuple)
print(my_set_To_list) # Output: [1,2,3]
7. Casting to bool
Any non-zero value or non-empty data type evaluates
to True, and zero or empty data types evaluate to
False.
# Example
num =10
bool_value=bool(num)
print(bool_value) # Output; \: True
empty_list = []
bool_empty_list = bool(empty_list)
print(bool_empty_list) # Output: False
Use of variables
I. Integer (int) 2. Float (float)
• When to use: ‘int’ when you need to work with • When to use: Use ‘float’ when you need to
whole numbers Without a fractional component, represent real numbers that include decimals or
• Examples: fractions.
1.Counting items (e.g., number of people, products, Examples:
events 1. Measurements (e.g., weight, height, temperature).
2.Indexing elements in a list. 2.Financial calculations (e.g., prices, discounts, tax
3.Discrete calculations (e.g., age, quantity). rates).
3. Scientific data (e.g., distance, speed, density).
3. String (str) 4. Boolean (bool)
• When to use: Use ‘str’ to represent textual data such • When to use: Use ‘bool ‘to represent truth values
as names, sentences, or identifiers. (True or False) for decision-making or conditional
• Examples: logic.
1.User input (e.g., names, addresses, emails). Examples:
2.Descriptions or messages (e.g., error messages, 1.Status flags (e.g., whether a user is logged in or
status updates). not).
3.Representing codes (e.g., product codes, order 2. Conditional checks (e.g., if a process is complete,
numbers). if a door is open).
3.Binary states (e.g., yes/no, on/off).
5. List (list) 6. Tuple (tuple)
• When to use: Use ‘list’ when you need to store • When to use: Use tuple when you need an ordered
multiple items in an ordered, mutable collection. It collection of items that should remain immutable
can contain any data type. (i.e., cannot change after creation).
Examples: • Examples:
1.Storing a collection of items (e.g., shopping cart, 1.Grouping related but unchangeable data (e.g.,
to-do lists). coordinates, dates).
2.Grouping related data (e.g., names, scores, sensor 2.Return multiple values from a function (e.g.
readings). „ position and velocity).
3.Dynamic collections (e.g., adding or removing 3.Grouping settings or configuration constants.
items).
Data Structures
In Python, data structures are used to store and organize data efficiently, making it easy to perform
operations like accessing, modifying, and manipulating the data.
• Python data structures are the building blocks of any robust and efficient programming solution.
• They play a crucial role in organizing and managing data, making our code more organized, optimized, and
scalable.
Dictionaries:
Dictionaries work like real-life dictionaries, where words are connected to their meanings.
In Python, a dictionary associates keys with values. This allows you to quickly find a value using its
corresponding key.
Stacks:
Imagine a stack of plates; you can only add or remove
plates from the top. In programming, a stack follows the
Last-in-First-Out (LIFO) principle.
The last item added is the first one to be removed.
QUEUES:
A queue is a linear data structure that follows the First IN, First Out (FIFO) principle, the first item added is
the first one to be removed. Queues are used in scenarios like managing tasks in a print queue or scheduling
processes in an operating system.
Trees:
It is a hierarchical data structure that
consists of nodes, where each node has at
most two children, referred to as the left
child and the right child.
Linked List:
Linked lists are helpful when you need
dynamic storage that can grow or
shrink easily.
Unlike arrays, which have a fixed size,
linked lists can be modified without
reallocating memory.
They're often used in implementing
data structures like stacks, and queues,
and as the foundation for more complex
structures like graphs.
Heapq:
Heapq is like a special type of list where the elements are
arranged in a particular order that makes it easy to find the
smallest (or largest) element quickly.
Graph:
A graph is a collection of nodes (vertices) and edges that
connect pairs of nodes. Graphs are widely used to represent-
networks or relationships between objects.
Sequence
A sequence in Python refers to an ordered collection of items, where each item can be accessed by its position
(index) in the sequence.
• Sequences allow for systematic organization and retrieval of data using indexing and slicing.
Common of Sequences in Python:
1. Strings (str): A sequence of characters.
2. List (list): A mutable (changeable) sequence of items.
3. Tuples tuple): An immutable (unchangeable) sequence of items.
4. Ranges (range): A sequence of numbers generated using the range () function.
Characteristic of Sequences:
1. Order: The elements in a sequence are arranged in specific order, meaning each element has a specific
position or index.
2. Indexing: You can Access individual elements in a sequence using their position(index). Python use zero bas
indexing (i.e., the first name in index a).
3. Slicing: You can extract a position of sequence ( a “slice”) using slicing syntax.
Indexing
Indexing in Python refers to accessing individual elements from a sequence (such as lists, strings, or tuples)
using their position (index) within the sequence.
• Python uses zero-based indexing, meaning that the first element in a sequence is at index O, the second
element is at index 1, and so on.
Types of Sequences that Support Indexing:
Examples
• Strings: A sequence of characters. • Lists: A sequence of items that can be of any data
text = “Python” type.
# Accessing individual characters fruits = [“apple”, “banana”, “cherry” “dates”]
print(text[0]) # Output: P # Accessing elements using positive indexing
print(text[3]) # Output: h print(fruits[0]) # Output apple
print(text[-1]) # Output: n (negative index start from print(fruits[2]) # Output cherry
the end) # Accessing elements using positive indexing
print(fruits[-1]) # Output dates
print(fruits[-3]) # Output banana
• Tuples: A sequence of immutable items. Negative Indexing
coordinates = {10,20,30,40} Python allows negative indexing. where -1 refers to
# Accessing tuple elements the last element. -2 refers to the second last element,
print(coordinates[0]) # Output: 10 and so on.
print(coordinates[2]) # Output: 30 Example with Negative indexing:
animals = [“cat”, “dog”, “elephant”, “tiger”]
Mutable Elements
Modifying Lists Using Indexing
Since lists are mutable, you can change elements using indexing.
Example:
colors = ["red", "green", "blue"]
print(colors) # Output: ["red", "green", "blue"]
# Modifying an element
color[1] = "yellow"
print(colors) # Output: ["red", "yellow", "blue"]