0% found this document useful (0 votes)
42 views

Lab-01-Introduction To Python (133-20-0005)

The document provides an introduction to Python programming and machine learning labs. It discusses Python basics like data types, lists, tuples, sets, dictionaries, loops, functions and conditional statements. It also introduces Google Colab as an integrated development environment for Python. The document includes exercises to practice key Python concepts like defining lists, developing functions to calculate BMI and temperature conversions, using loops and conditional logic.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Lab-01-Introduction To Python (133-20-0005)

The document provides an introduction to Python programming and machine learning labs. It discusses Python basics like data types, lists, tuples, sets, dictionaries, loops, functions and conditional statements. It also introduces Google Colab as an integrated development environment for Python. The document includes exercises to practice key Python concepts like defining lists, developing functions to calculate BMI and temperature conversions, using loops and conditional logic.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Computer Systems Engineering

Artificial Intelligence & Machine Learning

Lab 01: Introduction to Python

Submission:
 All lab reports and the corresponding Notebook should be uploaded to LMS
 Submission should be made during the lab time, unless specified otherwise
 One mark is assigned to each lab report

Rubrics
Task/Design works Task/Design works Task/Design works Task/Design does not Not appeared in the
Task Performed perfectly. (0.3 Marks) with minor errors. with major errors. (0.2 work. lab/left without
(30%) (0.25 Marks) Marks) (0.15 Marks) performing. (0.0
Marks)
Components used (or Components used (or Components used (or Components not used Not appeared in the
Proper
Coded) as per Coded) as per Coded) as per (or Coded) as per lab/left without
Designed/Programme
instructions or instructions or instructions or instructions or performing. (0.0
d as per instructions
properly. (0.3 Marks) properly with minor properly with major properly. (0.15 Marks) Marks)
(30%)
mistakes. (0.25 Marks) mistakes. (0.2 Marks)
Performed without any Minor help needed or Major help needed or Consistently needed Not appeared in the
Performed help and contributed less contribution in the minor contribution in help or no contribution lab/left without
independently (20%) equally in the group. group. (0.175 Marks) the group. (0.15 to the group. (0.1 performing. (0.0
(0.2 Marks) Marks) Marks) Marks)
Timely and properly Timely submission Timely submission Delayed submission Not appeared in the
written report and minor mistakes in and major mistakes in (same week) lab/left without
submission. (0.2 the report the report OR performing. (0.0
Marks) OR OR Delayed submission Marks)
Submission (20%)
Delayed submission Delayed submission (same day) and major
(same day) and (same day) and minor mistakes in the report.
properly written mistakes in the report. (0.05 Marks)
report. (0.15 Marks) (0.1 Marks)

1
Learning Objectives:
 Understand the basics of Python programming and Integrated Development Environments (IDEs)
 Understand and use online environments (e.g., Google Colab) for Python programming and
completing the tasks

Introduction
Python – developed by Guido Van Rossum developed in 1991 – is a high-level, general-purpose, easily
readable, and most popular programming language. Its formatting is visually neat and often uses English
keywords and indentations to delimit the blocks. Whereas, other languages use punctuations and curly
brackets. An increase in indentation comes after certain statements; a decrease in indentation signifies the
end of the current block. Thus, the program's visual structure accurately represents its semantic
structure. The recommended indent size is four spaces. Python is enriched with open-source libraries and
global contributors.
An IDE is a software application that provides comprehensive facilities to computer programmers for
software development. An IDE normally consists of at least a source code editor, build automation tools,
and a debugger. Some IDEs, such as NetBeans and Eclipse, contain the necessary compiler, interpreter, or
both. The common IDEs for Python programming are shown in Fig. 1.

Fig. 1. Top Python IDEs.

Google Colab
Google Colaboratory (Colab) is an executable document that lets one write, run, and share Python code
within Google Drive without any configuration/setup (run-time). In addition, it provides free access to the
graphics processing unit (GPUs).
The Colab notebook, like the Jupyter notebook, is an interactive environment and is composed of cells.
Each cell can have code, text, image, and many more.

Working with Python


Python data types are shown in Fig. 2, and can be verified using “type(variable name/data)”.
The list in Python is a collection of ordered, and mutable data (maybe heterogeneous and duplicate)
items. It is used to store multiple data items in a single variable. It can be created either by using square

2
brackets [] or a constructor list(()). Fig. Error: Reference source not found shows the common
operations performed on a list.

The tuple in Python is a collection of ordered, and immutable data (maybe heterogeneous and duplicate)
items. Like a list, the tuple is used to store multiple data items in a single variable. It can be created either
by using round brackets () or a constructor tuple(()).

3
Fig. 2. Python data types

The set in Python is an unindexed collection of unordered, immutable, and unique (no duplication) data
items. It can be created using curly brackets {} or a constructor set(()).

Fig. 3. Common operations performed on a list.

4
A dictionary in Python is like a set but used to store data values in key: value pairs. A dictionary can be
created with curly brackets {“brand”: “Ford”, “age”: 5} or a constructor dict(“brand”= “Ford”, “age”= 5).

Loops, functions, and IF statements in Python are ended with ‘:’, and the code block is indented as shown

Fig. 4. Illustration of while loop, IF statement, and a function.

in Fig. 4.

Exercise:

1. Define “names” and “height” lists that contain the name and height of your friends respectively. Then
find the total number of entries in the list, maximum height, and minimum height with their names,
and find the average height of your friends.
Source code:
n=['kashaf','aliza','kiran','usman','raqeeb']
h=[5,5.1,5.3,5.7,5.6]
a=len(n)
print("The total enteries are ",a)
print("The maximaum height in the given enteries is m",max(h))
print("The min height in the given enteries is m",min(h))
b=(h[0]+h[1]+h[2]+h[3]+h[4])/a
print("The average height of the given enteries is ",b )
z=h.index(max(h))
y=h.index(min(h))
print("Maximum height ",n[z],h[z])
print("Minimum height ",n[y],h[y])

OUTPUT:

2. Develop a BMI function that gets the weight and height as inputs and returns whether the user is
underweight, normal weight, overweight or obese.

5
Source code:
def bmi(a,b):
height=b/3.281;
weight=a*2.20462;
BMI=b/height**2
if BMI<18.5:
print("You are under weight");
elif BMI>18.5 and BMI <24.9:
print("you have healthy weight");
elif BMI>25.0 and BMI < 29.9:
print("you are overweight.")
else:
print("you are in obese range");
c=float(input("Enter your weight in Kg."));
d=float(input("Enter your height in feet."));
z=bmi(c,d);

OUTPUT:

3. Write a function that takes the temperature in degree Celsius, and return the corresponding
temperature in kelvin and Fahrenheit.
SOURCE CODE:
def temp(a):
f=(a*(9/5)) + 32;
k=a+ 273.15
print("The temperature in farheniet is ",f)
print("The temperature in kelvin is ", k)
c=float(input("Enter temperature in celcius."));
z=temp(c);

OUTPUT:

6
4. Create a list of even numbers and odd numbers with the help of for loop and append function. Then
combine both lists.
SOURCE CODE:
e=[];
o=[];
b=1;
for a in range(5):
n=b*2;
e.append(n)
z=n-1
o.append(z)
b = b + 1;
print("lIST OF EVEN NUM=",e);
print("lIST OF ODD NUM=",o);

OUTPUT:

5. Write a Python program that accepts a sequence of comma-separated numbers from the user and
generates a list and a tuple of those numbers.
SOURCE CODE:

a=input("Enter the numbers of your choice and seperate it with commas: ")
lists=a.split(',')
print("The enterd list of numbers are ",lists);
t=tuple(lists)
print("The list of number after using tuple function ",t)

OUTPUT;

6. Write a Python program that determines whether a given number (accepted from the user) is even or
odd, and prints an appropriate message to the user.

SOURCE CODE:
a=int(input("Enter any number:"));
if a%2==0:
print("Number you entered is even.");
else:
print("Number is odd.")

7
OUTPUT

7. Weather data of Pakistan’s different regions is given in figure below. Create a nested dictionary for
this data. Also write a function that takes the region and type of data required as arguments and return
with required data value.

Source code:

weather={
'azadkashmir':{"maxtempday":29.6,"mintempnight":16.0,"humidity":61,"per-
cipittion":1168,"sunshine":2884,"rainydays":67},
'balochistan':{"maxtempday":32.0,"mintempnight":18.0,"humidity":61.0,"per-
cipittion":1296,"sunshine":2957,"rainydays":74},
'sindh':{"maxtempday":34.5,"mintempnight":20.3,"humidity":53.0,"percipit-
tion":624,"sunshine":3103,"rainydays":14},
'kpk':{"maxtempday":29.1,"mintempnight":15.3,"humidity":57.0,"percipit-
tion":730,"sunshine":2847,"rainydays":53},
'islamabad':{"maxtempday":28.9,"mintempnight":15.8,"humidity":61.0,"per-
cipittion":1296,"sunshine":2957,"rainydays":74},
'punjab':{"maxtempday":31.1,"mintempnight":17.8,"humidity":57.0,"percipit-
tion":730,"sunshine":2847,"rainydays":53}
}

a=input("enter the reigion:")


b=input("enter the data you want:")
def w(a,b):
print(weather[a][b])

z=w(a,b)

OUTPUT:

8
9

You might also like