0% found this document useful (0 votes)
15 views9 pages

Safi ML Lab2

The document describes a Python lab session where the student completed various exercises to practice foundational concepts like loops, conditional statements, and dictionary operations. This included determining if numbers were prime or Disarium, calculating averages from user input, and manipulating a dictionary of fruit prices by accessing keys and values, filtering conditionally, and updating values. By completing these tasks, the student gained experience in handling and analyzing data with Python in preparation for machine learning and artificial intelligence applications.

Uploaded by

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

Safi ML Lab2

The document describes a Python lab session where the student completed various exercises to practice foundational concepts like loops, conditional statements, and dictionary operations. This included determining if numbers were prime or Disarium, calculating averages from user input, and manipulating a dictionary of fruit prices by accessing keys and values, filtering conditionally, and updating values. By completing these tasks, the student gained experience in handling and analyzing data with Python in preparation for machine learning and artificial intelligence applications.

Uploaded by

safiullahf20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

National University of technology

Name: Muhammad Safi Ullah Adam


ID: F20604031
Batch: CEN-20
Subject: ML/AI LAB

Submitted to: Engr. Abdul Qadeer


Experiment # 02

Algorithm to check a number is disarium:

STEP 1: Count the digits in the number i.e length.

STEP2: Create the copy of the number

STEP 3: Initialize and declare single_digit(remainder) and sum as 0 in start.

STEP 4: Using while loop calculates remainder single_digit repeatedly by modulus num with 10.

STEP 5: Calculate the single digit with the power with respect to its position . add this value in the sum.

STEP 6: Decrement the count of digits.

STEP 7: Check whether the sum is equal to the number(copy of number). If yes, then given number

is Disarium number. Else, it is not a Disarium number.

Theory

1. Python Variables

Variable is a name which is used to refer memory location. Variable also known as identifier and used to

hold value.

In Python, we don't need to specify the type of variable because Python is a type infer language and

smart enough to get variable type. Variable names can be a group of both letters and digits, but they
have

to begin with a letter or an underscore.

It is recommended to use lowercase letters for variable name. Rahul and rahul both are two different

variables.
LAB TASKS

1. Write a python code to check wheatear a number is prime or not.


2. Write a python code to determine the number is a disarium number.
3. Write a Python program to determine whether the given number is a
Harshad Number. If a number is divisible by the sum of its digits, then it
will be known as a Harshad Number.
For example:
The number 156 is divisible by the sum (12) of its digits (1, 5, 6 ) Some
Harshad numbers are 54, 120, etc.
4. Write a program to input ten values from user using for loop in the list
and calculate
average.

5. loop through a tuple of mixed data type and extract only integer values
6. Find the following values:
my_dict = {"apple": 2.50, "orange": 4.99, "banana": 0.59, "watermelon": 10.29,
"tomato": 9.02}
 Access all the keys in a dictionary
 Access all the values in a dictionary
 Access both the keys and the values together from a dictionary
 Find an average of all values in a dictionary
 Conditional looping: filtering dictionary items based on certain conditions
 ["apple", "orange", "pear", "watermelon"]
 Updating values: reduce all values by 25% (in a practical application it
would be like giving a 25% discount on all fruits)
Conclusion

In this lab session, I delved into various foundational Python concepts, ranging from conditional
statements and loops to intricate dictionary operations. The exercises began with determining
prime and Disarium numbers, emphasizing the importance of loops, conditional checks, and
digit manipulation. As I progressed, tasks like calculating the average of ten user-inputted
values and iterating through mixed data type tuples highlighted the significance of user input
validation and type checking. These initial tasks solidified my grasp on basic Python operations
and set the stage for more complex challenges.

The dictionary-based tasks were particularly enlightening. Working with a sample dictionary of
fruit prices, I practiced essential operations like accessing keys and values, and more advanced
techniques like conditional filtering and value updates using dictionary comprehensions. These
exercises simulated real-world data manipulation scenarios, reinforcing the practical
applications of Python in data analysis. By the end of the session, I felt more equipped and
confident in handling and analyzing data, paving the way for future endeavors in machine
learning and AI.

You might also like