Understanding Variables and Data Types in Python
| Attribute | Value |
|---|---|
| fruit_name | apple |
| fruit_color | red |
| quantity_kg | 6 |
| price_rupees | 120.5 |
| healthy | True |
In Python, we can store this data using variables as shown below:
fruit_name = "apple"
fruit_color = 'red'
quantity_kg = 6
price_rupees = 120.5
healthy = True
| Example values | Data type |
|---|---|
| "apple", "red", "my name is ramesh" | str |
| 12, 18000, -250 | int |
| 120.5, 3.14, -0.5 | float |
| True, False | bool |
We can check the data type of a variable using the type() function:
print(type(fruit_name))
print(type(fruit_color))
print(type(quantity_kg))
print(type(price_rupees))
print(type(healthy))
To display the values stored in the variables:
print(f"Fruit name is {fruit_name}")
print(f"Fruit color is {fruit_color}")
print(f"Quantity in kg is {quantity_kg}")
print(f"Price in rupees is {price_rupees}")
print(f"Is it healthy? {healthy}")