1) Program: Demonstrating Built-in Data Types in Python
a = 10 # int
b = 3.14 # float
c = 2 + 3j # complex
# Sequence Types
d = "Hello" # str
e = [1, 2, 3] # list
f = (4, 5, 6) # tuple
g = range(5) # range
# Mapping Type
h = {"name": "Alice", "age": 25} # dict
# Set Types
i = {1, 2, 3} # set
j = frozenset ([4, 5, 6]) # frozenset
# Boolean Type
k = True # bool
# Binary Types
l = b"hello" # bytes
m = bytearray(b"hello") # bytearray
n = memoryview(b"hello") # memoryview
# None Type
o = None # NoneType
# Printing all types
print("a:", a, "type:", type(a))
print("b:", b, "type:", type(b))
print("c:", c, "type:", type(c))
print("d:", d, "type:", type(d))
print("e:", e, "type:", type(e))
print("f:", f, "type:", type(f))
print("g:", g, "type:", type(g))
print("h:", h, "type:", type(h))
print("i:", i, "type:", type(i))
print("j:", j, "type:", type(j))
print("k:", k, "type:", type(k))
print("l:", l, "type:", type(l))
print("m:", m, "type:", type(m))
print("n:", n, "type:", type(n))
print("o:", o, "type:", type(o))
Output
a: 10 type: <class 'int'>
b: 3.14 type: <class 'float'>
c: (2+3j) type: <class 'complex'>
d: Hello type: <class 'str'>
e: [1, 2, 3] type: <class 'list'>
f: (4, 5, 6) type: <class 'tuple'>
g: range(0, 5) type: <class 'range'>
h: {'name': 'Alice', 'age': 25} type: <class 'dict'>
i: {1, 2, 3} type: <class 'set'>
j: frozenset({4, 5, 6}) type: <class 'frozenset'>
k: True type: <class 'bool'>
l: b'hello' type: <class 'bytes'>
m: bytearray(b'hello') type: <class 'bytearray'>
n: <memory at 0x...> type: <class 'memoryview'>
o: None type: <class 'NoneType'>
2) Simple Python Program Using Most Operators
# Arithmetic Operators
a = 10
b=3
print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.33
print("Floor Division:", a // b)# 3
print("Modulus:", a % b) #1
print("Exponent:", a ** b) # 1000
# Comparison Operators
print("a == b:", a == b) # False
print("a > b:", a > b) # True
# Assignment Operator
x=5
x += 3 # Same as x = x + 3
print("x after += 3:", x) #8
# Logical Operators
print("a > 5 and b < 5:", a > 5 and b < 5) # True
print("not(a == b):", not(a == b)) # True
# Membership Operator
name = "python"
print("'y' in name:", 'y' in name) # True
Operators are special symbols used to perform operations (like addition,
comparison, etc.) on values or variables.
Types of Operators in Python:
Example
Type Use
Operators
1. Arithmetic Operators +, -, *, /, //, %, ** Math operations like add, subtract
2. Assignment
=, +=, -=, etc. Assign or update values
Operators
3. Comparison
==, !=, >, <, >=, <= Compare two values
Operators
4. Logical Operators and, or, not Combine conditions
5. Bitwise Operators &, ` , ^, ~, <<, >>`
6. Membership
in, not in Check if a value is in a list, string
Operators
Check if two variables point to same
7. Identity Operators is, is not
object
Explanation of Few Common Operators
Operator Meaning Example Output
+ Add 5+2 7
== Equal 5 == 5 True
and Both conditions must be True True and False False
in Value present in list or string 'a' in 'cat' True
Simple Calculator using Operators
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operator (+, -, *, /”)
if op == '+':
print("Result:", a + b)
elif op == '-':
print("Result:", a - b)
elif op == '*':
print("Result:", a * b)
elif op == '/':
print("Result:", a / b)
else:
print("Invalid operator")