Introduction to Data Science Lab
Fall 2025
Lab 02 – Week 02
Python Programming Cont.
Data type
Python is a dynamically typed language, so we do not need to specify the type of a variable when we create
one.
<class 'float'>
<class 'int'>
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'complex'>
(1-1j)
1.0 -1.0
Check if a certain variable is integer or float....
We can also use the `isinstance` method for testing types of variables:
Type Casting
Typecasting in Python refers to the process of converting a variable from one data type to another. Python
provides several built-in functions for typecasting. Here are some common typecasting functions:
Complex variables cannot be cast to floats or integers. We need to use `[Link]` or `[Link]` to extract the part of
the complex number we want:
Conditional Statements
Conditional statements allow a program to make decisions and execute different blocks of code based on
conditions. In Python, conditions are usually written using comparison operators (==, !=, <, >, <=, >=) and logical
operators (and, or, not).
Logical operators
Strings
A string is a sequence of characters enclosed in either single quotes ('), double quotes ("), or triple quotes (''' or
"""). Strings are widely used to store and manipulate text.
1. Accessing Strings
We can index a character in a string using `[]`:
Indexing starts with 0
We can extract a part of a string using the syntax `[start:stop]`, which extracts characters between index `start`
and `stop` -1 (the character at index `stop` is not included):
If we omit either (or both) of `start` or `stop` from `[start:stop]`, the default is the beginning and the end of the
string, respectively:
We can also define the step size using the syntax `[start:end:step]` (the default value for `step` is 1, as we saw
above). This technique is called slicing.
2. String Operations
3. String Functions
Python provides many built-in functions to work with strings.
4. String Membership Operator
5. String Formatting
Zen of Python
The Zen of Python is a collection of guiding principles for writing computer programs in Python. It’s like
Python’s philosophy, written as short, witty aphorisms by Tim Peters.
You can see it directly in Python by typing this in a Python interpreter:
Mathematical built-in functions
Lab Tasks
Task # 01
Ask the user for: Name, Age and Favorite subject. Print the output using f-strings in this format: "My name is
Ali, I am 20 years old, and my favorite subject is Python."
Task # 02
Print:
a+b
"Hello " + c
a * c (string repetition)
a=5, b=2.5, c= “Python”.
Task # 03
Write a Python program that:
Defines variables x, y, and z. Prints results of:
x // y
int(x / y)
x/y
z+x
z*y
Explains with comments why x // y and int(x / y) differ for negative numbers.
Uses isinstance() to check and print the type of each result.
Task # 04
Write a program that:
Takes two inputs x and y.
If both are integers:
Print whether x // y is equal to int(x / y).
Print "Divisible" if y * (x // y) == x, otherwise "Not divisible".
If any input is a float, cast both to int and print their sum.
Otherwise, print "Invalid input".
Task # 05
The Zen of Python says: “Beautiful is better than ugly.”
Write a program that: Takes an integer n.
Using if-elif-else only:
If n is positive and divisible by 2 (without %), print "Beautiful".
If n is positive but not divisible by 2, print "Not Beautiful".
If n is negative, print "Ugly".
Add comments in your code explaining how these conditions reflect the Zen idea.
Task # 06
Write a Python program that asks the user to enter four numbers.
Find the largest and smallest of the four numbers.
Check if all four numbers are equal.
Check if they are in ascending order or descending order.
Display all results with clear message
Task # 07
Write a Python program that:
1. Creates two complex numbers a and b.
2. Performs addition, subtraction, multiplication, and division.
3. Prints the real & imaginary parts of a.
4. Prints the conjugate of b.
5. Prints the magnitude of a.