Python basics to learn artificial intelligence

Ankit kumar
6 min readOct 18, 2022

--

In this blog, we will learn about the basics of python language which we will be using to learn artificial intelligence.

Why Python?

  1. It is simple and so easy to learn
  2. It has powerful libraries for AI: NumPy, Scipy, Pandas, Scikit-learn, Matplotlib
  3. It is also highly used in the tech Industry.

Identifiers, reserved words, comments, Indentation

Identifiers: It is a name used to define a variable, class, or function. It starts with a letter in any alphabet or an underscore followed by any number of letters, digits, and underscores.

Reserved Words: There are a few reserved words in Python, that you cannot use as any identifier’s name or constants. They have their own operating uses which we will be learning later on. For example if, else, and, or, break, continue, def, del, print, for, while, return, import, etc.

Comments: Here, we use the hash (#) symbol to write single-line comments and ‘“ ’’’ to write multiple lines comments in the Python language. It is always best practice to use comments in your code to make the code more readable and easy to understand for anyone.

Indentation: It is very important in Python, it is used to indicate a block of code, and it refers to the spaces at the beginning of a code line. A block of code should have the same number of spaces but the number of spaces in the indentation is variable.

Variables and Data types

Variables: It is the name assigned to the memory location which is used to store the assigned data/values. In Python, you do not declare the data types or reserve memory space for a variable. The equal sign (=) operator is used to assign values to variables. For example a = 100.0, b = 10, c = ‘ Hello guys !’

Data Types: In Python, everything is an object and every value has a data type. It can be of many types. There are a few standard data types:

Numbers: It stores numeric values. Example: a = 10

String: It is a continuous set of characters represented by ‘single or double quotes. var1 = ‘We are learning AI’.

List: It is an ordered sequence of values separated by commas and enclosed within square brackets ([]). All the values in a list do not need to have the same type.

Eaxmple: ab = [10, 100.0, ‘abcedef’]

Tuple: It is an ordered sequence of values separated by commas and enclosed within round brackets (). All the values in a list do not need to have the same type. The only difference between a list and a tuple is, we cannot modify the tuples once created.

Example: ab = (10, 100.0, ‘tuple it is’ )

Set: It is an unordered collection of unique values separated by commas and enclosed within braces {}. We can compute set operations over them like Union, and intersection over two sets.

Example: ab = {10, 20, 40, 70, 30, 30, 20, 20}

Dictionary: It is an unordered collection of key: value pairs separated by commas and enclosed within braces {}. Key and value can be any type.

Example: ab = {‘a’:10, ‘b’: 20, ‘ef’: 50.2}

Operators

Arithmetic Operators: Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus (%), Exponent performs power calculation(**).

Comparison (Relational) Operators:

Operator (==): If two values are equal, then the condition becomes true.

Operator (!=): If two values are not equal, then the condition becomes true.

Operator (>): If the left value is greater than the right value, then the condition becomes true.

Operator (<): If the left value is smaller than the right value, then the condition becomes true.

Operator (>=): If the left value is greater than or equal to the right value, then the condition becomes true.

Operator (<=): If the left value is smaller than or equal to the right value, then the condition becomes true.

Assignment Operators:

Operator (=): It assigns values from right side operands to the left side operand, for example, c = a + b

Operator (+=): It adds the right operand to the left operand and assigns the result to the left operand, for example, c += a

Operator (-=): It subtracts the right operand from the left operand and assigns the result to the left operand, for example, c -= a

Operator (*=): It multiplies the right operand with the left operand and assigns the result to the left operand, for example, c *= a

Operator (/=): It divides the left operand with the right operand and assigns the result to the left operand, for example, c /= a

Operator (%=): It takes modulus using two operands and assigns the result to the left operand, for example, c %= a

Operator (**=): It performs exponential (power) calculation on operators and assigns value to the left operand, for example, c **= a

Logical Operators:

Operator (and): If both the operands are true then the condition becomes true, for example: (a and b) are true

Operator (or): If any of the two operands is true then the condition becomes true, for example: (a or b) is true

Operator (not): It is used to reverse the logical state of its operand, for example: not (a) is false.

(*above, a = True, b = True)

Control flow (Decision making)

if statement: It is used for decision-making, if the statement is true, we execute the operations inside that block of code. Otherwise, we proceed to the next statements.

if …. else… statement: An if statement is followed by an else statement, which executes when the if statement is false.

Nested if statements: We can have if … else if … else statement inside another if… else if … else statement. This is called nested if statements.

fig 1.

While loop: It is used to iterate over a block of code as long as the loop condition is true.

fig 3.

For loop: It is used to execute a sequence of statements multiple times by iterating over a sequence (might be a tuple, list, …) or any other iterable objects.

fig 4.

Nested loop: We can use one or more loops inside any other loop.

Break Statement: It can change the flow of the loop. It terminates the loop and transfers the execution to the statement immediately following the loop.

fig 5.

Continue statement: It can also change the flow of the loop. It makes the loop skip the rest of its body and it will take you to the next iteration of the loop.

fig 6.

References:

Can read articles from GeeksforGeeks.

Can read articles from tutorialspoint.

Can practice over hackerrank to start with python programming

--

--

Ankit kumar
Ankit kumar

No responses yet