Class 11 Artificial Intelligence – Unit 3: Python Programming | Complete Notes
Class 11 · Artificial Intelligence
Unit 3: Python Programming
Unit 3: Python Programming
Learn the fundamentals of Python programming and understand how Python supports data analysis, machine learning and Artificial Intelligence.
1. Learning Outcomes
By the end of this unit, students will be able to:
- Explain the basics of the Python programming language.
- Write Python programs using basic programming concepts and tokens.
- Use selective statements to control program flow.
- Use iterative statements for repetitive tasks.
- Understand and use essential Python libraries such as
NumPy,PandasandScikit-learn. - Apply Python programming to solve simple real-life problems.
2. Introduction to Python
Python is a general-purpose, high-level programming language. It is one of the most widely used programming languages in the fields of Artificial Intelligence, Machine Learning, Data Science and Web Development.
Why is it called Python?
The name Python was inspired by the BBC comedy series "Monty Python's Flying Circus".
Key Features of Python
| Feature | Meaning |
|---|---|
| Easy to read and learn | Python has a simple and readable syntax that is close to human language. |
| Interpreted | Python programs are executed line by line by an interpreter. |
| Free and Open Source | Python is freely available and its source code can be accessed and modified. |
| Platform Independent | Python programs can run on operating systems such as Windows, macOS and Linux. |
| Versatile | Python is used in AI, data science, web development and many other areas. |
3. Getting Started: Tools and Editors
Python code can be written and executed using different development environments and editors.
Jupyter Notebook
Jupyter Notebook is an interactive web application that allows users to create and share documents containing live Python code, explanations and visualizations.
It is widely used in Data Science, Machine Learning and Artificial Intelligence.
Anaconda
Anaconda is a popular Python distribution that comes with several important libraries, including NumPy and Pandas.
Other Python Tools and Editors
- IDLE
- PyCharm
- Spyder
- Google Colab
4. Python Tokens
Tokens are the smallest individual units of a Python program that are recognized by the interpreter.
4.1 Keywords
Keywords are reserved words that have a special meaning in Python. They cannot be used as ordinary variable or function names.
Examples:
if, else, for, import,
while, True, False
4.2 Identifiers
Identifiers are names given to variables, functions, classes and other programming elements.
Rules for identifiers:
- An identifier cannot start with a digit.
- It can contain letters, digits and underscore (
_). - Special characters are not allowed.
- Python keywords cannot be used as identifiers.
| Identifier | Validity | Reason |
|---|---|---|
student_name |
Valid | Contains letters and underscore. |
1st_place |
Invalid | An identifier cannot start with a digit. |
4.3 Literals
Literals are fixed/raw data values directly specified in a program.
| Type | Example |
|---|---|
| String Literal | "Hello" |
| Numeric Literal | 45 |
| Boolean Literal | True |
4.4 Operators
Operators are symbols or keywords used to perform operations on values or operands.
| Operator Type | Operators | Purpose |
|---|---|---|
| Arithmetic | +, -, *, /, % |
Perform mathematical operations. |
| Relational | ==, !=, <, >, <=, >= |
Compare two values. |
| Logical | and, or, not |
Combine or modify conditions. |
| Assignment | =, +=, -= |
Assign or update values. |
4.5 Punctuators
Punctuators are symbols used to organize the structure of Python code.
Examples:
:, ( ), [ ], { },
,, .
5. Data Types and Variables
What are Data Types?
Data types classify data items and tell the computer what kind of operations can be performed on them.
What is a Variable?
A variable is a named label used to store a value that can be processed during program execution.
Example:
age = 16
name = "Ria"
Here, age and name are variables.
Standard Data Types in Python
| Category | Data Type | Description | Example |
|---|---|---|---|
| Numbers | Integer | Whole numbers. | count = 10 |
| Numbers | Floating Point | Numbers containing decimal values. | price = 99.99 |
| Boolean | Boolean | Represents either True or False. | is_passed = True |
| Sequences | String | Text enclosed within quotes; immutable. | name = "Ria" |
| Sequences | List | Ordered and changeable collection. | tasks = ["eat", "code"] |
| Sequences | Tuple | Ordered collection that cannot be changed. | coords = (12.5, 77.3) |
| Mappings | Dictionary | Collection of key-value pairs enclosed in curly brackets. | {'One': 1} |
Type Casting
Type casting is the explicit conversion of one data type into another.
Example:
age = int(input("Enter your age: "))
Here, the value entered through input() is converted into an integer
using int().
6. Input and Output
print() Function
The print() function is used to display output on the screen.
print("Welcome to Python")
print(25)
input() Function
The input() function is used to receive data from the user.
By default, the value returned by input() is a string.
name = input("Enter your name: ")
print(name)
age = int(input("Enter your age: "))
7. Control Flow Statements
Control flow statements determine the order in which statements in a program are executed.
They can broadly be divided into:
- Selection Statements
- Looping Statements
7.1 Selection Statements
Selection statements are used to make decisions and execute specific blocks of code depending on conditions.
if Statement
The if statement executes a block of code when the specified
condition is true.
marks = 80
if marks >= 40:
print("Pass")
if-else Statement
The if-else statement provides two alternatives:
one block executes when the condition is true and another when it is false.
marks = 35
if marks >= 40:
print("Pass")
else:
print("Fail")
if-elif-else Ladder
An if-elif-else ladder is used when multiple conditions need
to be checked.
choice = input("Enter your choice: ")
if choice == "Veg":
print("Vegetarian Menu")
elif choice == "Non-veg":
print("Non-Vegetarian Menu")
else:
print("Mixed Menu")
elif statement allows multiple
conditions to be checked one after another.
7.2 Looping Statements
Looping statements are used to execute a block of code repeatedly.
for Loop
A for loop is generally used to iterate over a sequence or
a range of values.
Example: Printing the first ten even natural numbers
for i in range(2, 21, 2):
print(i)
while Loop
A while loop executes a block of code as long as a specified
condition remains true.
count = 1
while count <= 5:
print(count)
count = count + 1
8. CSV Files
CSV stands for Comma Separated Values. A CSV file stores tabular data such as numbers and text in plain-text form.
Uses of CSV Files
- Storing tabular data.
- Importing data into spreadsheets.
- Exporting data from databases.
- Preparing datasets for AI and Machine Learning analysis.
students.csv may contain
student names, classes, marks and other information in tabular form.
9. Essential Python Libraries for AI
Python provides powerful libraries that make it easier to work with numerical data, datasets and Machine Learning algorithms.
9.1 NumPy
NumPy is used for numerical computing and mathematical operations. Its primary data structure is the ndarray.
Key use: Working efficiently with numerical data and arrays.
9.2 Pandas
Pandas is an important Python library used for data manipulation and analysis.
Pandas uses DataFrames to work with multiple columns of data simultaneously.
Example:
import pandas as pd
data = pd.read_csv("admission.csv")
Such data can then be analyzed to study student marks or other information.
9.3 Scikit-learn
Scikit-learn (Sklearn) is a powerful library used to implement Machine Learning algorithms.
It supports important Machine Learning tasks such as classification and regression.
| Library | Main Purpose | Important Structure / Feature |
|---|---|---|
| NumPy | Numerical computing and mathematical operations | ndarray |
| Pandas | Data manipulation and analysis | DataFrame |
| Scikit-learn | Machine Learning | Classification, Regression and other ML algorithms |
10. Practical Applications of Python
Python can be used to solve a variety of real-life problems by combining variables, operators, input/output and control statements.
10.1 Tipper Program
A tipper program can calculate the tip amount based on a customer's total bill. For example, the program may calculate 15% and 20% tips.
bill = float(input("Enter total bill: "))
tip15 = bill * 15 / 100
tip20 = bill * 20 / 100
print("15% Tip =", tip15)
print("20% Tip =", tip20)
10.2 Service Reminder
A service reminder can check the kilometer reading of a vehicle and determine whether servicing is required.
km = int(input("Enter kilometer reading: "))
if km >= 15000:
print("Service is required")
else:
print("Service is not required")
10.3 Salary Calculator
A salary calculator can calculate net salary by adding allowances such as HRA and DA and subtracting deductions such as PF.
basic = float(input("Enter basic salary: "))
hra = float(input("Enter HRA: "))
da = float(input("Enter DA: "))
pf = float(input("Enter PF: "))
net_salary = basic + hra + da - pf
print("Net Salary =", net_salary)
11. Quick Revision Table
| Concept | Key Point to Remember |
|---|---|
| Python | General-purpose, high-level programming language. |
| Creator | Guido van Rossum |
| First Released | 1991 |
| Python Name | Inspired by "Monty Python's Flying Circus". |
| Token | Smallest individual unit recognized by the Python interpreter. |
| Variable | Named label used to store a value. |
input() |
Accepts user input and returns a string by default. |
print() |
Displays output on the screen. |
if |
Used for decision-making. |
for |
Used to iterate over a sequence or range. |
while |
Repeats statements while a condition remains true. |
| CSV | Comma Separated Values. |
| NumPy | Numerical computing; uses ndarray. |
| Pandas | Data manipulation and analysis; uses DataFrame. |
| Scikit-learn | Machine Learning algorithms such as classification and regression. |
12. Important Exam Points
- Python was created by Guido van Rossum.
- Python was first released in 1991.
- The name Python came from "Monty Python's Flying Circus".
- Tokens are the smallest individual units recognized by the interpreter.
- The five important token categories covered in this unit are keywords, identifiers, literals, operators and punctuators.
input()returns a string by default.int()can be used for converting a value into an integer.if,if-elseandif-elif-elseare selection statements.forandwhileare looping statements.- CSV means Comma Separated Values.
- NumPy is primarily used for numerical computing.
- Pandas is used for data manipulation and analysis.
- Scikit-learn is used for Machine Learning.
13. One-Minute Concept Map
Python Programming
-
Basics
- Python
- Features
- Jupyter Notebook
- Anaconda
-
Tokens
- Keywords
- Identifiers
- Literals
- Operators
- Punctuators
-
Data Handling
- Variables
- Data Types
- Type Casting
- Input / Output
-
Control Flow
- if
- if-else
- if-elif-else
- for
- while
-
Data Files
- CSV
-
AI Libraries
- NumPy → Numerical Computing
- Pandas → Data Analysis
- Scikit-learn → Machine Learning