HomePYTHONVariables and Data Types in Python

Variables and Data Types in Python

data types

โœ… 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

RuleExample
Must start with a letter or _name, _count
Cannot start with a numberโŒ 1stname
Can only contain alphanumeric and underscoresuser_1, userName
Case-sensitiveName, 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

TypeExampleDescription
int5Integer value
float5.5Decimal
complex2+3jComplex number
str"hello"String
boolTrue, FalseBoolean
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
frozensetfrozenset([1,2])Immutable set
bytesb"abc"Immutable bytes
bytearraybytearray(5)Mutable bytes
NoneTypeNoneNo value

Share:ย 

No comments yet! You be the first to comment.

Leave a Reply

Your email address will not be published. Required fields are marked *