Python Practise Notes For ML

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Indentation is the spaces or tabs at the beginning of a line of code that determines its grouping or scope in

Python. Unlike other programming languages that use curly braces or keywords to define code blocks, Python
uses indentation to indicate the beginning and end of blocks of code

So don’t give white spaces this were called indentations errors

Type conversions means assings one value to another variable ..you must give it’s data type then only we get error less
execution

Type conversion, also known as type casting, is the process of converting a value
from one data type to another. In Python, you can perform type conversion using
built-in functions like int(), float(), str(), etc. Here are some common type
conversions:

1. Integer to Float: You can convert an integer to a float using the float()
function:
pythonCopy code
integer_value = 10 float_value = float (integer_value)
2. Float to Integer: You can convert a float to an integer using the int()
function. Note that the decimal part will be truncated:
pythonCopy code
float_value = 10.5 integer_value = int (float_value)
3. Integer/Float to String: You can convert an integer or float to a string using
the str() function:
pythonCopy code
number = 10 string_number = str (number)

Strings and operators in python

so python is interprented lang..which execute s this source codeline by line if anything getsmwronng
then it wonpt go for the next line
The // operator in Python is the floor division operator. It divides the left operand by the right operand
and returns the largest integer less than or equal to the result. In the expression 6//2, the result is 3,
because 6 divided by 2 is 3 without any remainder.

Operator precedence

In Python, operator precedence determines the order in which operators are evaluated in an
expression. Operators with higher precedence are evaluated first. Here is a general overview of
operator precedence in Python, from highest to lowest:

1. Parentheses ()
2. Exponentiation **
3. Unary plus +, unary minus - (as in -2)
4. Multiplication *, division /, floor division //,
remainder %
5. Addition +, subtraction -
6. Bitwise shift operators <<, >>
7. Bitwise AND &
8. Bitwise XOR ^
9. Bitwise OR |
10. Comparison operators ==, !=, >, >=, <, <=, is, is not, in, not in
11. Boolean NOT not
12. Boolean AND and
13. Boolean OR or

Operators within the same level of precedence are evaluated from left to right. You can use
parentheses to override the default precedence and explicitly specify the order of evaluation.

Multiline comment “”” and single line comment #

In Python, conditions are used to make decisions in your code. You can use if, elif (short for "else
if"), and else statements to create conditional blocks. Here's a basic example:

pythonCopy code
x = 10 if x > 5 : print ( "x is greater than 5" ) elif x == 5 : print ( "x is equal to 5" ) else : print ( "x is less than 5" )

In this example, the if statement checks if x is greater than 5. If it is, it executes the indented block of
code (prints "x is greater than 5"). If not, it moves to the elif statement and checks if x is equal to 5.
If it is, it executes the corresponding block of code. If neither the if nor elif conditions are true, the
else block is executed

Mini calculator
RANGE

In Python, range() is a built-in function used to generate a sequence of numbers. It can


be used in for loops to iterate over a sequence of numbers. The range() function can take
one, two, or three arguments:

1. range(stop): Generates a sequence of numbers from 0 up to, but not including, the
stop value.
2. range(start, stop): Generates a sequence of numbers from start up to, but not
including, the stop value.
3. range(start, stop, step): Generates a sequence of numbers from start up to, but not
including, the stop value, incrementing by step each time.

Loops

In Python, loops are used to repeatedly execute a block of code until a certain condition is met. There are two main types of loops in
Python: for loops and while loops.

in while loop if print (i) simple


with give this like i+=1 it will go to
infinte loop
List:

In Python, a list is a data structure that is used to store a


collection of items. Lists are mutable, which means that you can
change the contents of a list after it has been created. Lists can
contain` items of different data types, including other lists.

Methods on list[ ]:

Break (it end upto


their)and continue(it
continues by skipping
that)

In Python, break and


continue are control flow
statements used in loops
(for and while) to alter the
flow of the loop execution.

Tuples ( ): similar to
this list [ ] but this
tuples( ) were
unmutable i.e we
can’t change those
items present init..

n Python, a tuple is a
collection of
immutable
(unchangeable)
objects, similar to a list. However, tuples are defined using
parentheses () instead of square brackets []

The main differences between tuples and lists in Python are:

1. Mutability: Lists are mutable, which means you can change their contents (add, remove, or modify elements). Tuples, on the other hand,
are immutable, so once they are created, their contents cannot be changed.
2. Syntax: Lists are defined using square brackets [], while tuples are defined using parentheses ().
3. Use cases: Lists are typically used when you have a collection of items that may need to be modified, such as when you're working with a
dynamic list of elements. Tuples are used when you have a collection of items that should not be changed, such as when you're working
with a fixed set of data or when you want to ensure that the data remains unchanged.

In machine learning projects, both tuples and lists can be useful, depending on the situation:

• Use lists when you need a mutable collection of items. For example, you might use a list to store training data that needs to be modified
during preprocessing or when you're building a dynamic list of features.
• Use tuples when you have a fixed set of data that should not be changed. For example, you might use a tuple to store the
hyperparameters of a machine learning model or when you want to return multiple values from a function in a way that prevents them
from being modified.

In general, if you need to store data that should not be changed, or if you want to ensure that your data remains constant throughout your
program, tuples are a good choice. If you need to store data that may need to be modified, or if you need a more flexible data structure, lists are
more appropriate.

Set { all unique// no duplicate elements were present //indexing unspport}:In Python, a set is an unordered collection of unique elements. Sets are mutable,
which means you can add or remove elements from them, but they do not support indexing or slicing like lists and tuples. Sets are defined using curly braces
{}.
Operation on sets{
}

Dictionary {
‘key’:’value’} we can change -mutuable

n Python, a dictionary is a collection of key-value


pairs. It is a mutable, unordered collection, meaning
that you can change its contents and it does not
maintain the order of items. Dictionaries are defined
using curly braces {} and consist of keys and their
corresponding values, separated by a colon :.

Functions in python :

you can define a function using the def keyword followed by the function name and parentheses containing
any parameters the function takes. The body of the function is indented and contains the code that defines
the function's behavior.

In Python, a function is a block of reusable code


that performs a specific task. Functions allow you
to organize your code into logical blocks, making
it easier to read, understand, and maintain.

In Python, functions can be categorized into three main types based on how they are defined and used:

1. Built-in functions: These are functions that are provided by Python as part of the standard library. They are ready to use without the need
for any additional code. Examples of built-in functions include print(), len(), range(), max(), min(), etc.
2. Module functions: These are functions that are defined in modules, which are files containing Python code. Modules allow you to
organize your code into separate files and reuse code across multiple programs. To use a function from a module, you need to import the
module using the import statement. For example, the math module provides functions for mathematical operations:

3.
pythonCopy code
import math print (math.sqrt( 16 )) # Output: 4.0
4. User-defined functions: These are functions that you define yourself in your Python code. User-defined functions allow you to
encapsulate a block of code that performs a specific task and reuse it throughout your program. You define a user-defined function using
the def keyword, followed by the function name and any parameters the function takes. Here's an example of a simple user-defined
function:
pythonCopy code
def greet ( name ): return f'Hello, {name}!' print (greet( 'Alice' )) # O

utput: Hello, Alice!

User-defined functions are a powerful feature of Python that allow you to write modular and reusable code, making your programs easier to read,
maintain, and debug.

Before moving to machine learning projects in Python, it's important to have a good understanding of the following foundational concepts and
skills:

1. Python programming: Have a solid grasp of basic Python syntax, data types, control flow (if statements, loops), functions, and modules.

2. NumPy: Learn NumPy, which is a fundamental package for scientific computing in Python. It provides support for large, multi-dimensional
arrays and matrices, along with a collection of mathematical functions to operate on these arrays.
3. Pandas: Understand Pandas, a powerful data manipulation library built on top of NumPy. Pandas provides data structures like DataFrames
and Series, which are essential for data cleaning, manipulation, and analysis.

4. Matplotlib and Seaborn: Learn Matplotlib and Seaborn for data visualization. These libraries allow you to create various types of plots
and charts to visualize data and gain insights.

5. Scikit-learn: Familiarize yourself with Scikit-learn, which is a machine learning library for Python. It provides a wide range of tools for
building and evaluating machine learning models, as well as data preprocessing and model selection functionalities.

6. Statistics: Have a basic understanding of statistics, including concepts like mean, median, mode, standard deviation, and hypothesis
testing. This knowledge is essential for understanding and evaluating machine learning models.

7. Linear Algebra: Brush up on linear algebra concepts, as they are foundational to understanding many machine learning algorithms. Focus
on vectors, matrices, matrix operations, and concepts like eigenvectors and eigenvalues.

8. Probability: Understand basic probability concepts, such as conditional probability, Bayes' theorem, and probability distributions.
Probability theory is essential for understanding the probabilistic models used in machine learning.

9. Machine Learning Concepts: Have a basic understanding of machine learning concepts such as supervised learning, unsupervised
learning, overfitting, underfitting, and model evaluation metrics like accuracy, precision, recall, and F1-score.

10. Data Preprocessing: Learn about data preprocessing techniques such as handling missing data, encoding categorical variables, and
scaling features. Data preprocessing is an important step in preparing data for machine learning models.

By mastering these foundational concepts and skills, you'll be well-equipped to start working on machine learning projects in Python and further
advance your knowledge in the field.
1. https://www.youtube.com/watch?v=2qGCYeUz1_Q

2. https://www.youtube.com/watch?v=p2k_9I4VGYs

3. https://www.youtube.com/watch?v=mnwNKckSVr8

4. https://www.youtube.com/watch?v=Fcnk5yEU7EI

5. https://www.youtube.com/watch?v=MvmkFra5HAQ

6. https://www.youtube.com/watch?v=QGBlxSLZL3Y

7. https://www.youtube.com/watch?v=brgzTJwHIKI

8. https://www.youtube.com/watch?v=WJGL4UN9J_k

9. https://www.youtube.com/watch?v=UehuI1w10lg&pp=ygUZbWFjaGluZWxlYXJuaW5nIGluIHRlbHVndQ%3D%3D

You might also like