DSA_Lab_01
DSA_Lab_01
Lab Overview
This lab focuses on introducing Python to beginners, providing a strong
foundation in the language's core constructs. It is designed to cover
essential topics like variables, data types, strings, Boolean logic, control
structures, and Python collections. The lab consists of guided tasks to help
students gain hands-on experience, along with exercises to test their
understanding. At the end of this lab, students will be equipped with the
basic knowledge required to advance into more complex programming
concepts.
Lab Objectives
1. Familiarize students with Python syntax and programming basics.
2. Understand and work with variables, data types, and strings.
3. Learn the concepts of Boolean logic, operators, and control
structures.
4. Explore Python collections such as lists, tuples, sets, and
dictionaries.
5. Introduce the use of functions and arrays.
Lab Requirements
Install Python: Download and install Python 3.10+ from python.org.
Install VSCode: Download and install Visual Studio Code from
code.visualstudio.com.
Add Python Extension in VSCode: Open VSCode → Go to Extensions
(Ctrl+Shift+X) → Search for "Python" → Install.
Install Code Runner Extension: In VSCode Extensions, search for
"Code Runner" and install it to run Python programs easily.
Verify Python Installation: Open a terminal or command prompt and
type python --version to confirm Python is installed correctly.
University of Engineering and Technology Taxila
Computer Engineering Department
_____________________________________________
____
Set Up Workspace: Create a folder for lab exercises (e.g.,
Python_Labs) and open it in VSCode as your workspace.
Reference Official Documentation: Use the Python official
documentation (https://docs.python.org/3/) and sample programs to
enhance learning.
Guided Tasks
Task 1: Python Variables
Objective: Learn how to define and use variables in Python.
Steps:
# Define variables
name = "Alice"
age = 25
is_student = True
# Print variables
print("Name:", name)
print("Age:", age)
What You Learn: How to declare variables and store data in Python.
Expected Output
Name: Alice
Age: 25
Is Student: True
# Data types
integer_value = 10
float_value = 20.5
boolean_value = True
print(type(integer_value))
print(type(float_value))
print(type(string_value))
print(type(boolean_value))
What You Learn: The common data types in Python and how to check
them using type().
Expected Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Steps:
num = 10
University of Engineering and Technology Taxila
Computer Engineering Department
_____________________________________________
____
decimal = 5.7
text = "123"
# Arithmetic operations
# Type conversions
Product: 57.0
Steps
# Slicing
# Concatenation
print("Uppercase:", text.upper())
What You Learn: String manipulation and using methods like upper() and
replace().
Expected Output:
First 6 characters: Python
Steps:
# Boolean values
a = 10
b = 20
Expected Output:
University of Engineering and Technology Taxila
Computer Engineering Department
_____________________________________________
____
Is a equal to b? False
Steps
x = 15
y=4
# Arithmetic operators
print("Addition:", x + y)
print("Division:", x / y)
# Comparison operators
# Logical operators
Division: 3.75
# Modifying list
fruits.append("orange")
print("Fruit:", fruit)
Fruit: apple
Fruit: banana
Fruit: cherry
Fruit: orange
# Tuples
University of Engineering and Technology Taxila
Computer Engineering Department
_____________________________________________
____
colors = ("red", "green", "blue")
# Unpacking tuple
(a, b, c) = colors
print("Unpacked values:", a, b, c)
# Sets
numbers = {1, 2, 3, 4}
numbers.add(5)
numbers.remove(2)
# Set operations
even_numbers = {2, 4, 6}
print("Union:", numbers.union(even_numbers))
print("Intersection:", numbers.intersection(even_numbers))
Union: {1, 2, 3, 4, 5, 6}
Intersection: {4}
# Dictionaries
student["age"] = 22
name : Alice
age : 22
grade : A
Steps:
if number > 0:
else:
Expected Output
For input = 5
Enter a number: 5
Steps
i=1
while i <= 5:
print("Number:", i)
i += 1
What You Learn: Using while loops for repetitive tasks with a known exit
condition.
Expected Output
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Steps
print("Character:", char)
Steps:
def greet(name):
greet("Alice")
greet("Bob")
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
Steps
square = lambda x: x ** 2
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
What You Learn: Defining and using lambda functions with built-in
methods like filter().
Exercise Questions:
Fahrenheit = (Celsius x 9 / 5 ) + 32
Ask the user to input the units consumed and print the total
bill.
2- GPA Calculation
Where:
_______________________________________________________________