# Python

Python is a high-level programming language that is known for its simplicity and readability. It is widely used in various fields such as web development, data analysis, artificial intelligence, and scientific computing. The language's clean syntax allows developers to write code in fewer lines compared to other languages, making it efficient and easy to understand. Python also has a vast collection of libraries and frameworks, providing a wide range of tools and functionalities for developers. It supports both object-oriented and functional programming paradigms, allowing programmers to choose the best approach for their projects. With its versatility and extensive community support, Python has become one of the most popular programming languages in the world.

**1. Mathematical Foundation for Python: **

Python is a powerful programming language that is widely used for various applications. When it comes to performing mathematical operations in Python, having a strong understanding of basic math concepts is essential. Basic math in Python involves performing operations such as addition, subtraction, multiplication, and division. Python provides built-in functions and operators that can be used to perform these operations on numbers. Additionally, Python supports various mathematical functions, such as finding the square root, raising a number to a power, and finding the absolute value. By mastering basic math in Python, you will have a solid foundation to tackle more complex mathematical problems and algorithms in your programming journey.

**1.1.**** ****Number System:** is the basic building block to implement Python programs and is referred to a type of data.

Integers*: {......-3, -2, -1, 0, 1, 2, 3, etc.} ==> int*

Decimal*: 12.3, 54.32, -34.432, etc. ==> float*

Rational : 2/3, 3/5 --> 0.6, -2/7

Irrational : π, √3, √5

Real : Rational + Irrational

Imaginary* : 3i+4j ==> complex*

**1.2.**** ****Arithmetic Operations:**

**Basic Operations:** Addition, Subtraction, Multiplication, and Division

Subtraction: 2 - 3 = -1

Multiplication: 2 X 3 = 6

Division : 21 / 4 = quotient 5,remainder 1

BODMAS Principle- used to compute the order of precedence to evaluate an expression

Brackets : ()

Of: the power of

: percent of

: fraction of

Division: %

Multiplication: X

Addition : +

Subtraction: -

**Comparison :**

Symbol Words Example Use

= equals 1 + 1 = 2 ==> True

≠ not equal to 1 + 1 ≠ 1 ==> True

> greater than 5 > 7 ==> False

< less than 7 < 9 ==> True

≥ greater than or equal to money ≥ 1

≤ less than or equal to bill ≤ 3

**1.3.**** ****Binary Number System: **Used to represent numbers in a sequence of zeros and ones.

Decimal number = 137

Binary Number = 1000 1001

**1.4.**** ****Logic Gates: **Used to perform arithmetic and logic operations and are basic elementary units of a computer‘s arithmetic and logic unit.

1 represents True

0 represents False

AND gate :

-------------

1 and 1 => 1 True and True* => True

1 and 0 => 0 True and False* => False

0 and 1 => 0 False* and True => False

0 and 0 => 0 False* and False => False

OR gate :

-----------

1 or 1 => 1 True* or True => True

1 or 0 => 1 True* or False => True

0 or 1 => 1 False or True* => True

0 or 0 => 0 False or False* => False

NOT gate:

------------

not 1 => 0 not True => False

not 0 => 1 not False => True

1s compliment ==> 1101110 ==> 0010001

2s compliment ==> 1s + 1 => 0010001

**1.5.**** ****SETs:** Unordered collections of elements and not encourage duplicates

s1 = {1, 2, 3, 7}

s2 = {3, 4, 5, 7}

Set Union : s1 ∪ s2 => {1,2,3,4,5,7}

Set Intersection : s1 ∩ s2 => {3,7}

Set Difference : s1 - s2 => {1,2}

: s2 - s1 => {4,5}

**1.6.**** ****Sequence & Series:**

**Sequence:**

Infinite Sequence : 1,2,3,4,5...….n # n(n+1)/2

Finite Sequence : 1,2,3,4,5,6,7,....1000

Backward Sequence: 5,4,3,2,1

Alphabetical sequence: a, b, c, d, e, …z

Negative sequence : -1,-2,-3,-4,-5,....

: …-5,-4,-3,-2,-1

Decimal à 10, 20, 30, 40, 50, 60....10000

Even à 2,4,6,8,10,12,14, …

Squareà1, 2, 4, 8, 16, 32, …

Not a sequence 1,5,7,8,10,11, … **Series :** 4

∑ 5x

x = 1

STATE: x=1 to 4, BEHAVIOR: 5x

=Solution==> = 5(1) + 5(2) + 5(3) + 5(4) = 5 + 10 + 15 + 20 = 50

**1.7.**** ****Algebra:**

Expression: An expression is a number, a variable, or a combination of numbers, variables, and operation symbols.

Ex: 4.5 + 1 <==> 5.5

Equation: An equation is made up of two expressions connected by an equal sign.

Ex: 16 - 6 = 10

x + 8 = 40

3*x + 4 = 5x + 14

Numeric expression: It will contain all numbers. To apply operations to numbers we can use this

Ex: 2(3 + 8)

= 6 + 8

= 14

Algebraic expression: At least one variable and at least one operation

Ex: 2(x + 8y)

**Use case:**

Simplify the algebraic expression: 3(4x+5y)-2(3x-7y)

Then evaluate the simplified expression for x = 3 and y = -2.

==> I. Substitute the values, get the final output

Then evaluate the simplified expression for x = 2/7 and y = 3/4.

==> II. Simplify the expression, then substitute the values

**Solution:**

Step 1: Simplify the algebraic expression using the basic properties of real numbers.

= 3(4x+5y) - 2(3x-7y)

= 3(4x+5y) + (-2)(3x+[-7]y) <== Definition of Subtraction

= 12x + 15y + (-6)x + 14y <== Distributive property

= 12x + (-6)x + 15y + 14y <== Commutative property of Addition

= (12+[-6])x + (15 + 14)y <== Distributive property

= 6x + 29y <== Simplify

Step 2: Now substitute x with 3 and y with -2

= 6(3) + 29(-2)

= -40

**1.8.**** ****Function:** used to represent the behavior of an entity or object

Given function: f(x) = 2x + 1. Find f(x) when x = 10

Math: Equation Question Input data

Python: Behavior REQ/Ticket State

x = 10 # STATE

2x+1 # BEHAVIOR

**1.9.**** ****Plane Geometry:**

POLYGON (# Generalization)

TRIANGLE QUADRILATERAL PENTAGON HEXAGON

- Equilateral - Parallelogram

- Isosceles - Square

- Scalene - Rectangle

- Rhombus

- Trapezium

**1.10.**** ****Matrices:** majorly used to represent multi-dimensional data.

A[1][1][2][2]

Above is a 3X3 matrix: Means 3 rows 3 columns A[3X3]

A[2][1] ==> 2nd row,1st column ==> 32

A[1][3] ==> 1st row,3rd column ==> 5

A[2][2] ==> 2nd row,2nd column ==> 12

### 2. Variables

**2.1. Variable:**

The variable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value. Variable names can be a group of both letters and digits, but they have to begin with a letter or an underscore.

**2.2. Types:**

There are two types of variables in Python - Local variables and Global variables.

· Local variables are the variables that are declared inside the function and have scope within the function.

· Global variables can be used throughout the program, and its scope is in the entire program. We can use global variables inside or outside the function.

A variable declared outside the function is the global variable by default. Python provides the **global** keyword to use the global variable inside the function. If we don't use the **global** keyword, the function treats it as a local variable.

**2.3. Garbage Collection:**

· The process of automatic deletion of unwanted or unused objects to free the memory

· The garbage collector in Python starts running as soon as the program's execution starts. Whenever the object's reference counter reaches 0, the garbage collector is triggered.

**2.4. Assign a single value to multiple variables:**

A = B = C = 10

**2.5. Multiple variables with multiple values:**

A, B, C = 10, ‘python’, 16.21

A= 10,

B = ‘python’,

C = 16.21

**2.6. Tokens:** The smallest individual unit of a program.

**Keywords- **Keywords are the pre-defined set of words in a language that perform their specific function. You cannot assign a new value or task to them other than the pre-defined one. Ex: if, Elif, while, True, False, None, break, etc

**Identifiers- **Identifiers are the names that you can assign a value to

**Literals - **Literals are fixed or constant values. They can either be** **string, numeric, or Boolean.

**Punctuators or Separators** - Punctuators, also known as separators give a structure to code. They are [mostly] used to define blocks in a program. We will be covering code blocks in control flow statements,

Single quotes – ‘ ‘, double quote – ” ”, parenthesis – ( ), brackets – [ ], Braces – { },

colon – ( : ), comma (, ), etc.**2.7. Operators- **Operators are the symbols that are used to perform operations between operands.

**List of Operators:**

Arithmetic operators ( +, -, /, * etc)

Assignment operators ( = )

Comparison operators ( >, <, >=, <=, ==, !=)

Logical operators ( and, or, not)

Identity operators ( is, is not)

Membership operators ( in, not in)

Bitwise operators ( &, |, ^ etc)

**Types of Operators:**

· **Unary Operators**: Operators having a single operand.

Ex: +8, -7, etc

· **Binary Operators:** Operators working on 2 operands.

Ex: 2+2, 4-3, 8*9, etc.

· **Ternary Operators:** work on 3 operands and so on.

These are just basics and not so important to know but the operators listed below are very important

### 3.Operators

*Operators* are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called an operand.

**3.1. Arithmetic operators** are used to perform mathematical operations like addition, subtraction, multiplication, etc.

**3.2. Comparison operators**

Comparison operators are used to compare data values. It returns either *True* or *False* according to the condition.

**3.3. Logical operators**

Logical operators are and, *or*, *not* operators.

**3.4. Bitwise operators**

Bitwise operators act on operands as if they were strings of binary digits. It operates bit by bit, hence the name.

**Ex:** Assume x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

**3.5. Assignment operators**

Assignment operators are used to assign values to variables.

For example X = 5 is a simple assignment, that assigns the value 5 on the right to the variable **X** on the left. There are various compound operators

like **X += 5** that add to the variable and later assign the same. It is equivalent to **X = X + 5**. Some of the operators are listed in the below table.

**3.6. Identity operators are **used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal do not imply that they are identical.

**3.7. Membership operators**

**in** and **not in** are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (Let X be string, list, tuple, set, and dictionary).

### 4. DataTypes

**4.1. Numbers: INT, FLOAT, COMPLEX**

· x = 10 # int

· x = 10.4 # float

· x = int(x) # Convert to int

· x = float(x) # Convert to float

**4.2. Boolean: BOOL**

· Boolean --> True or false

· it occupies 1 bit of memory location

· True as 1

· False as 0

**4.3. Data Structures:**

· String = > ”hello “

· List = > [1,2,’h’, 10.2]

· Tuple = > (1, 2, 4, 6)

· Dictionary = > {1:’hai’, 2:’war’}

· Set = > {12, 34, 1, 6}

**4.4. CRUD – Create Read (Retrieve) Update Delete:**

CREATE

x = 10

RETRIEVE

print ("Value of x : ", x)

UPDATE

x = 20

print("Value of x : ", x)

DELETE

del x

### 5. Keywords

Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier.

**>>> import keyword**

**>>> print (keyword. kwlist)**

A logical operator

To create an alias

For debugging

To break out of a loop

To define a class

To continue to the next iteration of a loop

To define a function

To delete an object

Used in conditional statements, same as else if

Used in conditional statements

Used with exceptions, what to do when an exception occurs

Boolean value, the result of comparison operations

Used with exceptions, a block of code that will be executed no matter if there is an exception or not

To create a for loop

To import specific parts of a module

To declare a global variable

To make a conditional statement

To import a module

To check if a value is present in a list, tuple, etc.

To test if two variables are equal

To create an anonymous function

Represents a null value

To declare a non-local variable

A logical operator

A logical operator

A null statement, a statement that will do nothing

To raise an exception

To exit a function and return a value

Boolean value, the result of comparison operations

To make a try...except statement

To create a while loop

**with**

Used to simplify exception handling

**yield**

To end a function, return a generator

### 6. Decision Making / Conditional Statements

**If-else statements:** The **if-else** statement allows you to execute different blocks of code based on a condition. Here's an example:

x = 10

if x > 0:

print("x is positive")

else:

print("x is non-positive")

**Elif statements:** If you have multiple conditions to check, you can use **elif** (short for "else if") statements. Here's an example:

x = 10

if x > 0:

print("x is positive")

elif x == 0:

print("x is zero")

else:

print("x is negative")

**Nested if statements:** You can nest **if** statements inside other **if** statements to handle more complex conditions. Here's an example:

x = 10

if x > 0:

if x < 100:

print("x is positive and less than 100")

else:

print("x is positive but greater than or equal to 100")

else:

print("x is non-positive")

**Ternary operator: **Python also provides a concise way to write simple conditional expressions using the ternary operator. Here's an example:

x = 10

message = "x is positive" if x > 0 else "x is non-positive"

print(message)