Total Hours: 10 | Marks: 5
What is Python?
Python is a simple, beginner-friendly, and powerful programming language. It is used to build websites, apps, games, Artificial Intelligence, and more.
Python is one of the most popular languages in the world because:
- It is easy to read and write
- It works on all operating systems
- It is used by big companies like Google, YouTube, and Instagram
Why Learn Python in AI?
Python is the most commonly used programming language in Artificial Intelligence. It helps in:
- Working with data
- Training AI models
- Automating tasks
- Creating smart applications
Python is a good first language for students to start coding.
Writing Your First Python Program
The print() Function
print()
is used to display a message on the screen.
Example:
pythonCopyEditprint("Hello, AI World!")
Output:
CopyEditHello, AI World!
Getting Input from the User
We can use the input()
function to ask the user to type something.
Example:
pythonCopyEditname = input("What is your name? ")
print("Hello, " + name)
Output:
pgsqlCopyEditWhat is your name? Riya
Hello, Riya
Variables in Python
A variable is like a container that stores data (like numbers or text).
Example:
pythonCopyEditage = 15
name = "Arjun"
print(name + " is " + str(age) + " years old.")
Output:
pgsqlCopyEditArjun is 15 years old.
Data Types
Python has different types of data:
Data Type | Example |
---|---|
String | “Hello”, “AI” |
Integer | 5, 100, -45 |
Float | 3.14, 9.8 |
Boolean | True, False |
List | [1, 2, 3] |
Operators in Python
Operators are symbols used to perform operations like addition, subtraction, etc.
Operator | Meaning | Example |
---|---|---|
+ | Addition | 5 + 2 = 7 |
- | Subtraction | 10 – 4 = 6 |
* | Multiplication | 3 * 4 = 12 |
/ | Division | 8 / 2 = 4.0 |
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
<, > | Less/Greater | 4 > 3 → True |
if-else Statements
These are used to make decisions in programs.
Example:
pythonCopyEditmarks = 75
if marks >= 33:
print("Pass")
else:
print("Fail")
Output:
nginxCopyEditPass
Loops in Python
Loops are used to repeat actions.
1. for Loop
Used to repeat actions a fixed number of times.
Example:
pythonCopyEditfor i in range(5):
print("AI is fun!")
Output:
kotlinCopyEditAI is fun!
AI is fun!
AI is fun!
AI is fun!
AI is fun!
2. while Loop
Used when we don’t know how many times to repeat, but we have a condition.
Example:
pythonCopyEditcount = 1
while count <= 3:
print("Welcome!")
count = count + 1
Lists in Python
A list is a group of items stored together.
Example:
pythonCopyEditfruits = ["apple", "banana", "orange"]
print(fruits[0]) # Output: apple
Lists are very useful when working with large amounts of data, such as AI datasets.
Suggested Classroom Activities and Practicals
1. Grade Checker Program
Ask students to write a program that takes marks and prints pass or fail.
pythonCopyEditmarks = int(input("Enter your marks: "))
if marks >= 33:
print("You Passed!")
else:
print("You Failed.")
2. Sum of Two Numbers
pythonCopyEdita = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("The sum is:", a + b)
3. List Average Program
pythonCopyEditnumbers = [60, 70, 80]
average = sum(numbers) / len(numbers)
print("Average is:", average)
4. Even or Odd Checker
pythonCopyEditnum = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Summary of the Unit
- Python is a simple and widely used programming language.
- Basic concepts include
print()
,input()
, variables, operators, conditions, loops, and lists. - Python is the foundation of many AI systems.
- By learning Python, students can create basic programs and build confidence in coding.
- Practical exercises help in developing real problem-solving skills.