Class 8 AI Unit 1 Python Basics

Core Concepts of Python Programming

We are about to start one of the most anticipated chapters in the field of artificial intelligence, programming in Python. But this is also the most misunderstood chapter by all the students because when you are starting programming it is not only about coding. Before that Core Concepts are Necessary.

What is Programming ?

In the simplest terms, a programming language is like a set of instructions that allows humans to communicate with computers. It’s the tool we use to tell a computer what we want it to do. 

A Program is a set of instructions that tells the computers what to do in order to come up with a solution for a particular problem.

When you instruct a computer to perform specific tasks then this is called Computer Programming.

Programming Languages

Programming Languages - Code with JD Sir

Machine Language (low level language)
Low-Level language is the only language which can be understood by the computer. Low-level language is also known as Machine Language. The machine language contains only two symbols 1 & 0. All the instructions of machine language are written in the form of binary numbers 1's & 0's. A computer can directly understand the machine language.

Assembly Language (middle level language)
Middle-level language is a computer language in which the instructions are created using symbols such as letters, digits and special characters. Assembly language is an example of middle-level language. 
In assembly language, we use predefined words called mnemonics. Binary code instructions in low-level language are replaced with mnemonics and operands in middle-level language. But the computer cannot understand mnemonics, so we use a translator called Assembler to translate mnemonics into machine language.

Assembler is a translator which takes assembly code as input and produces machine code as output. That means, the computer cannot understand middle-level language, so it needs to be translated into a low-level language to make it understandable by the computer. Assembler is used to translate middle-level language into low-level language.

High Level Language
High-level language is a computer language which can be understood by the users. The high-level language is very similar to human languages and has a set of grammar rules that are used to make instructions more easily. 
Every high-level language has a set of predefined words known as Keywords and a set of rules known as Syntax to create instructions. The high-level language is easier to understand for the users but the computer can not understand it. 
High-level language needs to be converted into the low-level language to make it understandable by the computer. We use Compiler or interpreter to convert high-level language to low-level language.
Languages like FORTRAN,C, C++, JAVA, Python, etc., are examples of high-level languages. 

Introduction to Python

Python is a widely used general-purpose, high-level programming language. It was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code. 

Features of Python
  1. Python is a high level language. It is a free and open source language.
  2. It is an interpreted language, as Python programs are executed by an interpreter.
  3. Python programs are easy to understand as they have a clearly defined syntax and relatively simple structure.
  4. Python is case-sensitive. For example, NUMBER and number are not same in Python.
  5. Python is portable and platform independent, means it can run on various operating systems and hardware platforms.
  6. Python has a rich library of predefined functions.
  7. Python is also helpful in web development. Many popular web services and applications are built using Python.
  8. Python uses indentation for blocks and nested blocks.
Why Name Python?
Guido  van  Rossum,  the  creator  of  the  Python  language,  named  the language after the BBC show “Monty Python’s Flying Circus”. He doesn’t particularly  like  snakes  that  kill  animals  for  food  by  winding  their  long bodies around them and crushing them.

Applications of Python

Why is Python the best for artificial intelligence and machine learning?
Python is one of the leading solutions. How? It is a prototyping language used to write codes for some top software projects. We have already seen the advent of Facebook, Google, YouTube, and more. Machine learning is on the hierarchy, thereby providing numerous benefits. 

The AI and machine learning projects vary significantly. The significant difference is because of the stack technology(the collection of technologies used to develop and run a website, application, or project). There is a demand for deep research in every step. Python AI projects are taking over the world because of their flexibility.

AI companies use technical information to boost productivity. Therefore, there is no doubt that AI can help ease a wide range of tasks, and it helps to meet the strategic goals that play an important role in improving efficiency. 

Python has proven to be one of the most efficient programming languages for AI and ML solutions. The technology transformation of AI can help in providing better outputs. 

Reasons Why Python is a Stand-Out Language for AI and ML Implementations :

1. Simple and consistent
Python provides the benefit of a reasonable code. AI and ML require solving complex algorithms. However, the simplicity of Python will ensure that developers can easily write the codes. One of the main reasons most individuals opt for the Python programming language is that it is easy to learn. Developers can easily understand Python codes, making it easier for everyone to understand the basics of machine learning. 

2. Better library ecosystem
Comparatively, Python has a better library system that is crucial for the development process. A library refers to a group of modules with a pre-written code set. Depending on these codes, the users can focus on promoting functionality. 

3. Flexible
One of the main reasons companies hire expert python developers is that they offer flexibility. The programming language provides the benefits of choosing between OOPS and scripting. Furthermore, you can consider recompiling the source code to bring any changes.

4. Popular
As per a 2020 research by Stack Overflow, Python is one of the top 5 most popular programming languages. Therefore, you can easily hire Python development professionals as long as they have the necessary skill set.

5. Better visualization option
We already know that Python has a variety of libraries online, and most of these libraries also feature exclusive visualization tools. When it comes to artificial intelligence, the developers need to highlight the visuals accurately to get attention. Furthermore, it also plays a vital role in presenting the data in a readable format. 

Libraries such as Matplotlib can be helpful for data scientists. It will allow us to create histograms, charts, and plots to help data comprehension, representation, and visualization. As a result, it will enable us to build better reports. 

6. Readability
Python is one of the leading platforms that bring the benefit of readability. Since it is an easy-to-read programming language, beginners can easily share and change the codes. Unlike the other programming languages, Python is not at all complicated.

7. Platform independence
Not all programming languages are platform-independent. However, Python, a versatile programming language, does benefit platform independence. Therefore, Python can easily function on different platforms such as Unix, Linux, macOS, Windows, and more.

8. Rapid development
Many developers consider Python to be as simple as English as far as writing and readability are concerned. As a Python development professional, you will not need to learn complicated codes. Thanks to the availability of numerous libraries, AI and ML development becomes easy, thereby simplifying the project. 

9. Less coding
Artificial intelligence is rapidly developing. Therefore, the application of AI will require you to indulge in using numerous algorithms. When you use Python for AI and ML development, you will access numerous predefined packages. 
Therefore, Python does not require you to indulge in hard-core coding because you already have predefined packages. 

10. Speed of execution
The syntax of Python is quite easy to understand and is very developer-friendly. As there is an abundance of libraries and frameworks, software development can be executed with high proficiency and efficiency on priority. By using Python, you can do a lot with just a few of the code lines. You can use it for developing prototypes and enhancing productivity.

Moreover, there are a lot of code testing and reviewing tools available with Python. Hence, the developers have the flexibility of quickly checking the quality and correctness of the code.

Download Python:
The latest version of Python is available on the official website:

How to install python steps by steps :




Working with Python:
  • Python Interpreter : Python interpreter is must be installed on your computer, to write and execute a Python program. It is also called Python shell. 
  • >>> : Symbol >>> is called Python prompt.
  • Python prompt : Python prompt, which indicates that the interpreter is ready to receive instructions. We can type commands or statements on this prompt for execution.
Execution Mode / Working Mode in Python :
There are two ways to run a program using the Python interpreter:
   1) Interactive mode and    2) Script mode

1) Interactive Mode :
In the interactive mode, we can type a Python statement on the >>> prompt directly. As soon as we press enter, the interpreter executes the statement and displays the result(s).

Python Interactive Mode  Code with JD Sir

Advantages of using interactive mode : It is convenient for testing a single line code for instant execution.

Disadvantage of using Interactive mode : The interactive mode, we cannot save the statements for future use and we have to retype the statements to run them again.

2) Script Mode :
In the script mode, we can write a Python program in a file, save it and then use the interpreter to execute the program from the file.
  • Python program files have a .py extension.
  • Python programs are also known as scripts.
  • Python has a built-in editor called IDLE which can be used to create programs / scripts.
Advantages of using Script mode : 
  • Multiple commands can be saved in a file as a program and then we can execute the entire program.
  • We type Python program in a file and then use the interpreter to execute the content from the file.
  • Working in interactive mode is convenient for beginners and for testing small pieces of code, as we can test them immediately.
Python IDLE :
IDLE : Integrated Development and Learning Environment

How to Create a Program :
  1. First open the IDLE,
  2. Click File>New File to create a new file,
  3. then write your program on that file and
  4. save it with a desired name.
  5. By default, the Python scripts are saved in the Python installation folder.
Python Script Mode Code with JD Sir

How to Execute a program in IDLE:
  1. In IDLE, 
  2. go to [Run] then [Run Module] or press F5 Shortcut key to execute the program
  3. The output appears on shell.


output window code with Jd sir

How to Edit a program in IDLE:
  • Select your file from file location (where you saved).
  • right click and choose Edit with IDLE and choose your version.

Python Basics:
Python code can be written in text editors as well as Integrated Developments (IDEs).


The Print() Function:
Python has inbuilt function print() that is used for displaying content on the screen.
The syntax for the print( ) is:

Print Statement - Code with JD Sir


Note : Double-quotes are used as per the syntax for displaying simple messages.

Python Statement & Comments:
Statements: Any instruction that a Python interpreter can execute are called statements.
Ex. 
var = 24 is an assignment statement.
data = "Code with JD Sir" is an assignment statement.

Comments: Comments are used to explain codes. It makes the codes more readable and enables an on-looker to have an idea about the
program and functionality of particular lines of code.
Note : Python ignores the comments mentioned in a program.

Single Line Comment : For writing, single-line comments, add ‘#’
before every line.

Multi-line comments: For writing, multi-line comments, add triple quotes (‘‘‘ ’’’).

Python Comment - Code with JD Sir

Keywords & Identifiers:
Keyword: A python keyword is a reserved word, which cannot be used as the name of a variable, class, function, etc. These keywords have a specific purpose in Python and should be used only in the way they are intended.
Here is a list of Python keywords:
Python keywords - Code with JD Sir

Identifiers : Identifiers are names used to identify a variable, function, or other entities in a program. The rules for naming an identifier in Python are as follows:
  1. The name should begin with an uppercase or a lowercase alphabet or an underscore sign (_). 
  2. This may be followed by any combination of characters a–z, A–Z, 0–9 or underscore (_). Thus, an identifier cannot start with a digit.
  3. It can be of any length. (However, it is preferred to keep it short and meaningful).
  4. It should not be a keyword or reserved word.
  5. We cannot use special symbols like !, @, #, $, %, etc., in identifiers.