Class 9 Artificial Intelligence Code 417 Solutions
Session 2025 - 26
Artificial Intelligence class 9 code 417 solutions PDF and Question Answer are provided and class 9 AI book code 417 solutions. Class 9 AI Syllabus. Class 9 AI Book. First go through Artificial Intelligence (code 417 class 9 with solutions) and then solve MCQs and Sample Papers, information technology code 417 class 9 solutions pdf is the vocational subject.
--------------------------------------------------
Chapter - Introduction to Python
Other Topics
Data Types in Python (Detailed)
Python has different built-in data types used to store various kinds of information.
1. Numeric Types
a) int
– Integer
Stores whole numbers (positive or negative) without decimals.
x = 10
y = -5
print(type(x)) # Output: <class 'int'>
b) float
– Floating Point
Stores decimal numbers.
pi = 3.14
height = -2.5
print(type(pi)) # Output: <class 'float'>
c) complex
– Complex Numbers
Numbers with real and imaginary parts.
z = 3 + 5j
print(type(z)) # Output: <class 'complex'>
2. Text Type
a) str
– String
Used to store text (characters, words, sentences).
name = "Alvin"
message = 'Hello, world!'
print(type(name)) # Output: <class 'str'>
3. Boolean Type
a) bool
– Boolean
Represents True or False.
is_active = True
is_logged_in = False
print(type(is_active)) # Output: <class 'bool'>
4. Sequence Types
a) list
– List
Ordered, changeable (mutable), allows duplicates.
fruits = ["apple", "banana", "mango"]
print(type(fruits)) # Output: <class 'list'>
b) tuple
– Tuple
Ordered, unchangeable (immutable), allows duplicates.
colors = ("red", "green", "blue")
print(type(colors)) # Output: <class 'tuple'>
c) range
– Range
Used for generating a sequence of numbers.
nums = range(5)
print(type(nums)) # Output: <class 'range'>
5. Set Types
a) set
– Set
Unordered, no duplicate values.
unique_numbers = {1, 2, 3, 2}
print(unique_numbers) # Output: {1, 2, 3}
print(type(unique_numbers)) # Output: <class 'set'>
b) frozenset
– Immutable set
frozen = frozenset([1, 2, 3])
print(type(frozen)) # Output: <class 'frozenset'>
6. Mapping Type
a) dict
– Dictionary
Stores key-value pairs.
person = {"name": "Alvin", "age": 25}
print(type(person)) # Output: <class 'dict'>
7. None Type
a) NoneType
Represents no value or null.
x = None
print(type(x)) # Output: <class 'NoneType'>
Summary Table
Data Type | Example | Description |
---|---|---|
int |
5 |
Integer number |
float |
3.14 |
Decimal number |
complex |
2 + 3j |
Complex number |
str |
"Hello" |
Text |
bool |
True , False |
Boolean values |
list |
[1, 2, 3] |
Ordered, mutable |
tuple |
(1, 2, 3) |
Ordered, immutable |
set |
{1, 2, 3} |
Unordered, no duplicates |
dict |
{"key": "value"} |
Key-value pairs |
NoneType |
None |
Represents no value |
Type Conversion in Python
Type conversion means changing the data type of a value or variable into another type. There is two types of type conversion.
1. Implicit Type Conversion
Python automatically converts one data type to another during operations.
Example:
a = 5 # int
b = 2.0 # float
result = a + b # int + float = float
print(result) # Output: 7.0
print(type(result)) # <class 'float'>
Python converts int
to float
automatically.
2. Explicit Type Conversion (Type Casting)
We manually convert one data type to another using functions like int()
, float()
, str()
, etc.
Example:
x = "100"
y = int(x) # Convert string to integer
print(y + 50) # Output: 150
Common Type Casting Functions
Function | Description | Example |
---|---|---|
int() |
Converts to integer | int("5") → 5 |
float() |
Converts to float | float("3.14") → 3.14 |
str() |
Converts to string | str(100) → "100" |
list() |
Converts to list | list("abc") → ['a', 'b', 'c'] |
tuple() |
Converts to tuple | tuple([1,2]) → (1,2) |
set() |
Converts to set | set([1,2,2]) → {1, 2} |
More Example:
a = 10
b = "20"
# Convert string to int before addition
total = a + int(b)
print(total) # Output: 30
Conditional Statements in Python
Conditional statements are used to make decisions in a program based on certain conditions.
Types of Conditional Statements:
1. if
Statement
Executes a block of code only if the condition is True
.
age = 18
if age >= 18:
print("You are eligible to vote")
2. if-else
Statement
Executes one block if the condition is True
, otherwise executes another block.
age = 16
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
3. if-elif-else
Statement
Used when you have multiple conditions.
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 50:
print("Grade C")
else:
print("Fail")
Comparison Operators Used in Conditions:
Operator | Meaning | Example (x = 5 , y = 10 ) |
---|---|---|
== |
Equal to | x == y → False |
!= |
Not equal to | x != y → True |
> |
Greater than | y > x → True |
< |
Less than | x < y → True |
>= |
Greater or equal | x >= 5 → True |
<= |
Less or equal | x <= 3 → False |
Example Program:
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even number")
else:
print("Odd number")
Loop Statements in Python
Loops are used to repeat a block of code multiple times, either for a fixed number of times or until a certain condition is met.
Python supports two main types of loops:
-
for
loop while
loop
1. for
Loop in Python
Purpose:
Used to iterate over a sequence like a list, tuple, string, or range.
Syntax:
for variable in sequence:
# block of code
variable
: Takes the value of each item in the sequence one by one.sequence
: A collection (like list, string, orrange()
).
Example: Using range()
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
Program: Print numbers from 5 to 20 with step 3
for i in range(5, 21, 3):
print(i)
Output:
5
8
11
14
17
20
In this example:
start = 5
stop = 21
(loop runs up to 20)step = 3
(adds 3 each time)
Example: Iterating a List
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Output:
apple
banana
cherry
2. while
Loop in Python
Purpose:
Executes a block of code as long as a condition is True
.
Example: Counting from 1 to 5
i = 1
while i <= 5:
print(i)
i += 1
Output:
1
2
3
4
5
Loop Control Statements
Python provides these control statements to manage loops:
Statement | Description |
---|---|
break |
Exits the loop immediately |
continue |
Skips the current iteration |
else | Runs a block after the loop ends |
Example with break
:
for i in range(5):
if i == 3:
break
print(i)
Output: 0 1 2
Example with continue
:
for i in range(5):
if i == 2:
continue
print(i)
Output: 0 1 3 4
Difference Between for
and while
Loop
Feature | for Loop |
while Loop |
---|---|---|
Use Case | Known number of repetitions | Unknown, depends on condition |
Based On | Sequence | Boolean condition |
Risk of Infinite | Low | Higher if condition is not updated |
No comments:
Post a Comment