DEPARTMENT OF COMPUTER & SOFTWARE
ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
CS- 333 Applied AI & Machine Learning
LAB MANUAL – 01
Course Instructor: Dr. Taufeeq Ur Rehman
Lab Instructor: Engr. Eman Fatima
Student Name: _______________________________________________
Degree/ Syndicate: ____________________________________________
1
LAB # 01: Introduction to Python Programming
Lab Objective:
The objective of this lab is to introduce students to the basics of programming and problem-
solving using Python. Students will learn to work with Python syntax, variables, operators, and
control structures for writing simple programs. The lab focuses on hands-on practice with data
structures such as strings, lists, and dictionaries, along with modular programming using
functions. It also covers basic file handling, error management, and an introduction to object-
oriented concepts. Overall, the lab aims to develop logical thinking, problem-solving skills, and
good programming practices for writing efficient and well-structured Python code.
Hardware/Software Tools:
Hardware: Desktop/Computer
Software Tool: Python IDLE
Lab Description:
This lab is designed to help students build a strong foundation in programming using Python.
This lab covers the basics of Python, including its syntax, variables, operators, and input/output
operations. Students will gain practical experience in writing simple programs and applying
control structures such as conditional statements and nested if/else to solve problems. With its
clear and beginner-friendly syntax, Python allows students to focus on developing logical
thinking and problem-solving skills. By the end of this lab, students will be able to design and
implement basic Python programs that use decisions to handle different conditions effectively.
What is Programming?
Programming is the way we give instructions to a computer so it can do tasks for us. A computer
cannot think on its own, it only understands step-by-step instructions written in a programming
language. Just like you press keys on a calculator and it gives you the answer, the calculator is
following a program written by programmers to perform addition, subtraction, multiplication,
and division. Think about when you press the Print button on your computer. What really happens? The
computer follows a program:
1. It takes the file you selected.
2. Converts it into a format the printer understands.
3. Sends the instructions step by step to the printer.
4. Finally, the printer prints your document.
All of this happens because a programmer has already written a program (a set of instructions)
that tells the computer exactly how to do this job. Without programming, your computer would
just be a box of hardware with no idea what to do.
2
Python Programming:
Python is a high-level, interpreted, general-purpose programming language created by Guido
Van Rossum in 1991. Known for its simplicity and readability, it is especially beginner-friendly
while remaining powerful for advanced applications. Python is widely used in web development,
data analysis, artificial intelligence, automation, and many other fields. It runs on multiple
operating systems, including Windows, macOS, and Linux, and comes with a large standard
library containing built-in modules and functions to simplify problem-solving. Because of its
flexibility, ease of use, and wide range of applications, Python has become one of the most
popular programming languages in the world.
Apart from Python, there are many other programming languages used for different purposes. C
is considered the foundation language, often used for system programming and embedded
systems. C++ builds on C with object-oriented features, making it suitable for game
development and high-performance applications. Java is widely used for building desktop,
mobile, and enterprise applications because of its platform independence. JavaScript is mainly
used for creating interactive websites and web applications. Python, on the other hand, is
popular for its simplicity and is widely applied in web development, data science, and artificial
intelligence. Each language has its own strengths, and programmers choose them based on the
type of problem they want to solve.
How does Python code work?
3
Let’s have a quick review of a very basic example, how we can add two numbers through
programming. Think of it like peeking inside a calculator to see what happens when you press
the + button. Just like the calculator takes two numbers, processes them, and shows the result,
our program will also follow the same steps: it will take input from the user, add the numbers,
and display the output. This simple example helps us understand the basic idea of programming
by giving step-by-step instructions to the computer to perform a task.
In this example, we are adding two numbers that are already initialized in the code, and we are
not taking the input from the user (User-defined input). After assigning the values to the
variables. We are adding them by using the ‘+’ operator and storing the result in a third variable
named ‘Sum’. In the last step, we are displaying the result using the ‘print ()’ function.
Variables:
In python, variables are used to store data values that can be accessed and manipulated
throughout a program. A variable acts as a container for information. You can assign different
types of data to variables, such as numbers, texts etc. Variables in Python don't need to be
declared with a specific type (like in some other programming languages such as int, string, float
etc). Python automatically understands the type based on the value assigned. Variables can be
used in calculations, comparisons, or simply to store and retrieve data in a program.
● Variable names should be meaningful.
● Variable should start with a letter or underscore (_) and cannot contain spaces or special
characters ($, !, @ etc).
4
Strings:
In Python, strings are sequences of characters enclosed in quotes. They can include letters,
numbers, symbols, and even spaces. Strings are one of the most used data types for storing text.
1. Creating Strings
2. String Concatenation
3. Accessing Characters
4. String Slicing
5. String Length
6. Multi-line String
Operators in Python:
5
Command Name Example Output
+ Addition 4+5 9
- Subtraction 8-5 3
* Multiplication 4*5 20
/ Division 19/3 6
% Remainder 19%3 5
** Exponent 2**4 16
Boolean Operators in Python:
Boolean operators return either TRUE or FALSE
Expression Function
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
!= Not equal to
<> Not equal to (Alternate)
== Equal to
Example:
Lab Tasks:
6
1. Write a python code to display your Name, Roll. No and Syndicate. Display a multi-line
string where you describe your hobby in a few sentences.
2. Write a program that takes two numbers (predefined), stores them in variables, and then
prints their sum, difference, multiplication and division.
3. Write a python code to store one word in a variable. Display it five times in a row, and on
five different lines.
4. Write a python program to store marks of three subjects in variables. Then display the
total marks.