Python Data Types: Syntax and Example Code
1. Integer (int)
Syntax:
variable_name = integer_value
Example Code:
age = 20
employees = 150
print(age)
print(employees)
Output:
20
150
2. Float (float)
Syntax:
variable_name = decimal_value
Example Code:
price = 199.99
profit = 25430.75
print(price)
print(profit)
Output:
199.99
25430.75
3. String (str)
Syntax:
variable_name = "Text"
# or
variable_name = 'Text'
Example Code:
name = "Anu"
company = "ABC Pvt Ltd"
print(name)
print(company)
Output:
Anu
ABC Pvt Ltd
4. List (list)
Syntax:
variable_name = [item1, item2, item3]
Example Code:
fruits = ["Apple", "Banana", "Mango"]
marks = [85, 90, 78]
print(fruits)
print(marks)
Output:
['Apple', 'Banana', 'Mango']
[85, 90, 78]
5. Dictionary (dict)
Syntax:
variable_name = {
"key1": value1,
"key2": value2
}
Example Code:
student = {
"Name": "Anu",
"Age": 20,
"Course": "BBA"
}
print(student)
Output:
{'Name': 'Anu', 'Age': 20, 'Course': 'BBA'}
6. Tuple (tuple)
Syntax:
variable_name = (item1, item2, item3)
Example Code:
colors = ("Red", "Green", "Blue")
months = ("January", "February", "March")
print(colors)
print(months)
Output:
('Red', 'Green', 'Blue')
('January', 'February', 'March')
7. Set (set)
Syntax:
variable_name = {item1, item2, item3}
Example Code:
numbers = {10, 20, 30, 20}
cities = {"Chennai", "Delhi", "Mumbai", "Delhi"}
print(numbers)
print(cities)
Output:
{10, 20, 30}
{'Chennai', 'Delhi', 'Mumbai'}
8. Boolean (bool)
Syntax:
variable_name = True
# or
variable_name = False
Example Code:
is_student = True
payment_done = False
print(is_student)
print(payment_done)
Output:
True
False
Summary Table
Data Type Syntax Example
int x = 10 age = 20
float x = 10.5 price = 199.99
str x = "Text" name = "Anu"
list x = [1,2,3] fruits = ["Apple","Banana"]
dict x = {"key": value} student = {"Name":"Anu"}
tuple x = (1,2,3) colors = ("Red","Blue")
set x = {1,2,3} numbers = {10,20,30}
bool x = True is_student = True