0% found this document useful (0 votes)
16 views11 pages

Artifical Intelligence - Lab 3

Uploaded by

aligujar7800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Artifical Intelligence - Lab 3

Uploaded by

aligujar7800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Artificial Intelligence

Course: Artifical Intelligence (Lab) - CS-324

Lab - 3
1. Conditions (if, if-else, elif)

 Conditions are used for decision making in programs.


 If a condition is true, a block of code is executed.
 If it is false, another block may be executed.
 Python uses if, elif, and else for conditional statements.

General Syntax
if condition:

# executed when condition is trueelif another_condition:

# executed when this condition is trueelse:

# executed when none of the conditions are true

Example 1: Check if a number is positive, negative, or zero

Aim: To check whether the entered number is positive, negative, or zero.

Code:

num = int(input("Enter a number: "))

if num > 0:

print("The number is Positive")elif num < 0:

print("The number is Negative")else:


print("The number is Zero")

Sample Output:

Enter a number: -5 The number is Negative

Example 2: Check if a number is even or odd

Aim: To determine if a number is even or odd using modulus operator.

Code:

num = int(input("Enter a number: "))

if num % 2 == 0:

print("The number is Even")else:

print("The number is Odd")

Sample Output:

Enter a number: 9

The number is Odd

2. Loops (for, while)

 Loops allow us to repeat a block of code multiple times.


 For loop: Used when the number of iterations is fixed or known.
 While loop: Used when repetition depends on a condition.

General Syntax
# For loopfor variable in range(start, end, step):

# statements

# While loopwhile condition:


# statements

Example 1: Print numbers from 1 to 10 using for loop

Aim: To print numbers in a sequence using for loop.

Code:

for i in range(1, 11):

print(i)

Sample Output:

3 ...

10

Example 2: Print numbers from 10 to 1 using while loop

Aim: To print numbers in reverse order using while loop.

Code:

i = 10while i >= 1:

print(i)

i -= 1

Sample Output:

10

8 ...
1

Example 3: Sum of first 10 natural numbers

Aim: To calculate the sum of the first 10 natural numbers.

Code:

total = 0for i in range(1, 11):

total += iprint("The sum is:", total)

Sample Output:

The sum is: 55

3. Nested Loops

 A nested loop means placing one loop inside another.


 The outer loop controls the rows.
 The inner loop controls the columns.
 Nested loops are useful for patterns and tables.

General Syntax
for i in range(rows):

for j in range(columns):

# inner loop statements

# outer loop statements

Example 1: Print a 5×5 square of stars

Aim: To display a square pattern using nested loops.

Code:

for i in range(5):
for j in range(5):

print("*", end=" ")

print()

Sample Output:

*****

*****

*****

*****

*****

Example 2: Print a right-angled triangle of stars

Aim: To display a triangle pattern using nested loops

Code:

for i in range(1, 6):

for j in range(i):

print("*", end=" ")

print()

Sample Output:

**

***

****
*****

Example 3: Multiplication table (1 to 5)

Aim: To display multiplication tables from 1 to 5 using nested loops.

Code:

for i in range(1, 6):

for j in range(1, 11):

print(f"{i} x {j} = {i*j}")

print("------------")

Sample Output:

1x1=1

1 x 2 = 2 ...

1 x 10 = 10

------------

2 x 1 = 2 ...

5 x 10 = 50

4. Arrays

 Python does not have built-in support for arrays.


 Instead, Python Lists can be used as arrays.
 If you want to work with real arrays, you can use external libraries like NumPy.

What is an Array?

 An array is a special variable that can hold multiple values under a single name.
 Instead of creating separate variables for each value:

car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"

You can store all of them in a single list (array):

cars = ["Ford", "Volvo", "BMW"]


Accessing Array Elements

Elements are accessed using index numbers (starting from 0).

Example:

cars = ["Ford", "Volvo", "BMW"]print(cars[0]) # First element

Output:

Ford

Modify an element:

cars[0] = "Toyota"print(cars)

Output:

['Toyota', 'Volvo', 'BMW']


Length of an Array

Use the len() function to get the number of elements in an array.

Example:

cars = ["Ford", "Volvo", "BMW"]


print(len(cars))
Output:

Looping Through an Array

You can loop through an array using a for loop.

Example:

cars = ["Ford", "Volvo", "BMW"]


for x in cars:
print(x)

Output:

Ford
Volvo
BMW

Adding Array Elements

Use append() to add an element at the end.

Example:

cars = ["Ford", "Volvo", "BMW"]


[Link]("Honda")print(cars)

Output:

['Ford', 'Volvo', 'BMW', 'Honda']

Removing Array Elements


 pop(index): Removes element at a specific position.
 remove(value): Removes the first occurrence of a value.

Example (pop):

cars = ["Ford", "Volvo", "BMW"]


[Link](1) # removes "Volvo"
print(cars)

Output:

['Ford', 'BMW']

Example (remove):

cars = ["Ford", "Volvo", "BMW"]


[Link]("Volvo")
print(cars)

Output:

['Ford', 'BMW']
Common Array (List) Methods
Method Description

append() Adds an element at the end

clear() Removes all elements

copy() Returns a copy of the list

count() Returns number of elements with specified value

extend() Adds elements of another list/iterable

index() Returns index of the first matched value

insert() Inserts element at a specific position


Method Description

pop() Removes element at a specific position

remove() Removes first matched value

reverse() Reverses order of elements

sort() Sorts the list

Lab Exercise
Task 1: Print the following patterns.

Task 2: Print First 10 natural numbers using while loop.

Task 3: Display Fibonacci series up to 10 terms.

Task 4: Find the factorial of a number inputted by user.

Task 5: Find the sum of the series up to term inputted by user.

Task 6: Input a number and check whether it is prime or not.

Task 7: Find the factorial of a number inputted by [Link]. Sajida Parveen

Task 8: Write a Python program to create the multiplication table (from 1 to 10) of a
number.s

Declare a 0-dimension,1-dimension,2-dimension,3-dimension,5-dimension array and


print it.
Task 10: Iterative over the following array using for loop.
["apple", "banana", "cherry"]
Task 11: Write a Python program to create the multiplication table (from 1 to 10) of a
number.
Task 12: Find the largest element in an array using loop.
Task 13: Write a program to sum all the array elements using loop.

You might also like