Basic Data Types and Variable Naming, Assignment in Python

1. Defining Different Types of Variables

a = 42
b = 3.14
c = "Python"
d = True
e = None
f = [1, 2, 3]
g = {"name": "Alice"}
h = (1, 2, 3)
i = {1, 2, 3}
j = complex(2, 3)
k = b"byte"
l = bytearray(b"bytearray")
print(type(a))  # <class 'int'>
print(type(b))  # <class 'float'>
print(type(c))  # <class 'str'>
print(type(d))  # <class 'bool'>
print(type(e))  # <class 'NoneType'>
print(type(f))  # <class 'list'>
print(type(g))  # <class 'dict'>
print(type(h))  # <class 'tuple'>
print(type(i))  # <class 'set'>
print(type(j))  # <class 'complex'>
print(type(k))  # <class 'bytes'>
print(type(l))  # <class 'bytearray'>

2. Variable Type Conversion

num = 123
num_str = "3.14"
num_int = int('3')
num_float = float(num_str)
bool_val = bool(num_int)
str_num = str(num)
list_data = list('hello')
tuple_data = tuple([1,2,3])
print(num_int, type(num_int))  # 3 <class 'int'>
print(num_float, type(num_float)) # 3.14 <class 'float'>
print(bool_val, type(bool_val)) # True <class 'bool'>
print(str_num, type(str_num)) # 123 <class 'str'>
print(list_data, type(list_data)) # ['h', 'e', 'l', 'l', 'o'] <class 'list'>
print(tuple_data, type(tuple_data))  # (1, 2, 3) <class 'tuple'>

3. Variable Swapping

x = 10
y = 20
x, y = y, x
print(x, y)  # 20 10

4. Multiple Variable Assignment

a, b, c = 1, 2, 3
print(a, b, c)  # 1 2 3

5. Chained Assignment

x = y = z = 0
print(x, y, z)  # 0 0 0

6. Chained Assignment

x = y = z = 0
print(x, y, z)  # 0 0 0

7. Unpacking Assignment

values = (4, 5, 6)
a, b, c = values
print(a, b, c)  # 4 5 6

8. Using Uppercase for Constant Naming

PI = 3.14159
MAX_USERS = 100
DEFAULT_TIMEOUT = 30
print(PI, MAX_USERS, DEFAULT_TIMEOUT)  # 3.14159 100 30

9. Variable Naming Rules

user_name = "Alice"  # Snake case
userAge = 25         # Camel case
UserClass = "VIP"    # Pascal case
print(user_name, userAge, UserClass)  # Alice 25 VIP

10. Deleting Variables

temp_var = "temp"
del temp_var
# print(temp_var)  # This will raise NameError

Leave a Comment