DM_Lab_01
DM_Lab_01
What is Python?
print(”Hello, World!”)
a = 10 + 5
b = 10 − 5
c = 10 ∗ 5
d = 10/5
e = 10%3
f = 2 ∗ ∗3
g = 7//2
1
Relational Operations in Python
• Relational (comparison) operators are used to compare values, returning either True
or False
print(5 > 3)
print(5 < 3)
print(5 == 3)
print(5! = 3)
print(5 >= 3)
print(5 <= 3)
• Logical operators (and, or, not) are used to combine conditional statements.
• Primitive data types are the most basic data types available in Python.
• Integers Ex-10
• Floats Ex-10.5
• Strings Ex-”Alice”
2
Data Structures in Python
• Python has built-in data structures like lists, tuples, dictionaries, and sets, which
allow for the storage and manipulation of data.
List-
Tuple
Dictionary
Set
Variables in Python
age = 25
name = ”John”
isa ctive = T rue
x = 10
y = 20
print(x + y)
Comments in Python
• Comments are lines in Python code that are ignored by the interpreter.
• They are used to explain the code and improve readability.
This is a
multi-line comment
”””
3
Type Casting in Python
• In Python, you can cast between primitive data types (like int, float, str) and
convert data structures like lists to tuples, sets, etc.
Integer to Float
a = 10
b = float(a)
c = 12.34
d = int(c)
e = ”123”
f = int(e)
Integer to String
g = 100
h = str(g)
Boolean to Integer
i = True
j = int(i)
Integer to Boolean
k=0
l = bool(k)
• list(), tuple(), set(), dict() functions are used for converting the data structures for
specific types.
Ex-
List to Tuple
my list = [1, 2, 3]
my tuple = tuple(my list)
4
Tuple to List
my tuple = (4, 5, 6)
my list = list(my tuple)
my list = [1, 2, 2, 3]
my set = set(my list)
Set to List
my set = {4, 5, 6}
my list = list(my set)
Practice Questions
(a) Compares two numbers (x = 15, y = 10) and prints whether x is greater than
y.
(b) Checks if 25 is equal to 25 and prints the result.
(c) Checks if 30 is not equal to 25 and prints the result.
(d) Compares 10 and 20 using <= and prints the result.
(a) Check if both 5 > 3 and 10 > 5 are true, and print the result.
(b) Check if 7 > 9 or 15 > 10 is true, and print the result.
(c) Use not to check if 8 == 8 and print the result.
4. Define variables for the following data types and print them,
5
5. Write a Python program to,
(a) Create a dictionary with at least 3 key-value pairs (Ex- name, age, country).
(b) Retrieve and print the value of a specific key.
(c) Add a new key-value pair to the dictionary and print the updated dictionary.
(a) Convert a float number x = 45.67 to an integer and print the result.
(b) Convert an integer y = 100 to a string and print the result.
(c) Convert a string z = ”25” to an integer and print the result.
(d) Convert a boolean status = False to an integer and print the result.