Practical 2: Display Data of Different Types Using Variables and Literal Constants
Problem Statement:
Write a Python program to display different data types using variables and literal constants.
Solution Code:
# Displaying data using variables and literal constants
# Integer type
int_var = 25
print("Integer (Variable):", int_var)
print("Integer (Literal Constant):", 100)
# Float type
float_var = 12.34
print("Float (Variable):", float_var)
print("Float (Literal Constant):", 3.14)
# String type
str_var = "Python Programming"
print("String (Variable):", str_var)
print("String (Literal Constant):", "Hello, World!")
# Boolean type
bool_var = True
print("Boolean (Variable):", bool_var)
print("Boolean (Literal Constant):", False)
# Complex type
complex_var = 4 + 5j
print("Complex (Variable):", complex_var)
print("Complex (Literal Constant):", 2 + 3j)
Output:
- The program will display the values of different data types (integer, float, string, boolean, and complex) from both variables and literal constants.
- The output will look something like this:
Integer (Variable): 25
Integer (Literal Constant): 100
Float (Variable): 12.34
Float (Literal Constant): 3.14
String (Variable): Python Programming
String (Literal Constant): Hello, World!
Boolean (Variable): True
Boolean (Literal Constant): False
Complex (Variable): (4+5j)
Complex (Literal Constant): (2+3j)
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 1 Practical 2 solution where we have written Python program to display different data types using variables and literal constants.
- Integer Data Type:
- We declare int_var = 25 as a variable.
- We directly use 100 as a literal constant.
- Float Data Type:
- float_var = 12.34 is a float variable.
- 3.14 is a float literal constant.
- String Data Type:
- str_var = “Python Programming” is a string variable.
- “Hello, World!” is a string literal constant.
- Boolean Data Type:
- bool_var = True is a boolean variable.
- False is a boolean literal constant.
- Complex Number Data Type:
- complex_var = 4 + 5j is a complex number variable.
- 2 + 3j is a complex number literal constant.
Key Concept Learned
- Difference between variables and literal constants.
- Displaying different data types (int, float, str, bool, complex) in Python.
- Using print() to output both variables and constants.