โ 1. Variables in Python
๐ธ What is a Variable?
A variable is a container for storing data values.
๐ธ Variable Declaration
Python is dynamically typed, so you don’t need to declare the type. Just assign the value.
x = 10 # Integer
name = "John" # String
price = 99.99 # Float
is_active = True # Boolean๐ Rules for Naming Variables
| Rule | Example |
|---|---|
Must start with a letter or _ | name, _count |
| Cannot start with a number | โ 1stname |
| Can only contain alphanumeric and underscores | user_1, userName |
| Case-sensitive | Name, name, and NAME are different |
๐งช Type Checking
Use the type() function to check variable type:
x = 10
print(type(x)) # <class 'int'>โ 2. Data Types in Python
Python has the following built-in data types grouped into categories:
๐งฎ Numeric Types
1. int โ Whole numbers
age = 25
print(type(age)) # <class 'int'>2. float โ Decimal numbers
price = 99.99
print(type(price)) # <class 'float'>3. complex โ Complex numbers
num = 5 + 3j
print(type(num)) # <class 'complex'>๐ค Text Type
4. str โ String
name = "Alice"
message = 'Hello'
print(type(name)) # <class 'str'>โ Boolean Type
5. bool โ True or False
is_active = True
print(type(is_active)) # <class 'bool'>๐ฆ Sequence Types
6. list โ Ordered, mutable, allows duplicates
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # <class 'list'>7. tuple โ Ordered, immutable, allows duplicates
colors = ("red", "green", "blue")
print(type(colors)) # <class 'tuple'>8. range โ Sequence of numbers
r = range(5)
print(type(r)) # <class 'range'>๐ Set Types
9. set โ Unordered, no duplicates
s = {1, 2, 3, 3}
print(s) # {1, 2, 3}
print(type(s)) # <class 'set'>10. frozenset โ Immutable set
f = frozenset([1, 2, 3])
print(type(f)) # <class 'frozenset'>๐งพ Mapping Type
11. dict โ Key-value pairs
user = {"name": "Alice", "age": 25}
print(type(user)) # <class 'dict'>๐ Binary Types
12. bytes โ Immutable sequence of bytes
b = b"Hello"
print(type(b)) # <class 'bytes'>13. bytearray โ Mutable sequence of bytes
ba = bytearray(5)
print(type(ba)) # <class 'bytearray'>14. memoryview โ Memory view object
mv = memoryview(bytes(5))
print(type(mv)) # <class 'memoryview'>โ 3. Type Conversion
Type Conversion means converting a value from one data type to another.
๐ Convert between types using:
int()float()str()list()tuple()set()etc.
x = "100"
y = int(x)
print(y, type(y)) # 100 <class 'int'>๐ธ Convert string to int:
a = "100"
b = int(a)
print(b, type(b)) # 100 <class 'int'>๐ธ Convert int to string:
x = 10
y = str(x)
print(y, type(y)) # "10" <class 'str'>๐ธ Convert list to tuple:
fruits = ["apple", "banana"]
fruits_tuple = tuple(fruits)
print(fruits_tuple) # ('apple', 'banana')๐ธ Convert int to bool:
print(bool(1)) # True
print(bool(0)) # Falseโ ๏ธ Common Mistakes
- Can’t convert invalid strings:
x = "abc"
y = int(x) # โ Error: invalid literal for int()- Loss of data:
x = 10.99
y = int(x)
print(y) # 10 (decimal lost)โ
4. Special Type: NoneType
None is used to define a null or empty value.
x = None
print(type(x)) # <class 'NoneType'>๐ Summary Table
| Type | Example | Description |
|---|---|---|
int | 5 | Integer value |
float | 5.5 | Decimal |
complex | 2+3j | Complex number |
str | "hello" | String |
bool | True, False | Boolean |
list | [1, 2, 3] | Mutable sequence |
tuple | (1, 2, 3) | Immutable sequence |
dict | {"a": 1} | Key-value pairs |
set | {1, 2, 3} | Unique unordered elements |
frozenset | frozenset([1,2]) | Immutable set |
bytes | b"abc" | Immutable bytes |
bytearray | bytearray(5) | Mutable bytes |
NoneType | None | No value |
