Python Data Types (Python ke Data Types)
Python me different types ke data types hote hain jo alag-alag tarah ka data store karne ke liye use hote hain.
Common Data Types:
Data Type Example Description
Integer (int) 10 Poore numbers bina decimal
Float (float) 10.5 Decimal wale numbers
String (str) "Hello" Characters ya text
Boolean (bool) True / False True ya False values
List (list) [1, 2, 3] Ordered & mutable collection
Tuple (tuple) (1, 2, 3) Ordered & immutable collection
Dictionary (dict) {"name": "Ram", "age": 20} Key-value pairs
Set (set) {1, 2, 3} Unique values ka unordered collection
Example:
x = 10 # Integer
y = 10.5 # Float
name = "Ram" # String
is_active = True # Boolean
Aap type(variable_name) function se kisi bhi variable ka data type check kar sakte hain:
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'float'>
print(type(name)) # Output: <class 'str'>
Python Numbers (Numbers in Python)
Python me numbers ke 3 main types hote hain:
1. int - Poore numbers (e.g., 10, -5, 100)
2. float - Decimal numbers (e.g., 10.5, -3.14, 2.0)
3. complex - Complex numbers (e.g., 2 + 3j, -1j)
Example:
a = 10 # Integer
b = 5.5 # Float
c = 3 + 2j # Complex
print(type(a), type(b), type(c))
Mathematical operations bhi perform kar sakte hain:
print(a + b) # 15.5
print(a * b) # 55.0
print(abs(-10)) # 10
print(pow(2, 3)) # 8 (2^3)
Python Casting (Data Type Conversion)
Kabhi kabhi hume ek data type ko dusre me convert karna padta hai. Python me casting ka use hota hai.
x = int(10.5) # 10
y = float(10) # 10.0
z = str(100) # "100"
print(x, y, z)
Python Strings (Strings in Python)
String ek sequence of characters hoti hai jo " " ya ' ' ke andar likhi jati hai.
s = "Hello, Python!"
print(s[0]) # H
print(s[0:5]) # Hello
print(len(s)) # String ki length
Important String Functions:
print(s.lower()) # Sab small letters me
print(s.upper()) # Sab capital letters me
print(s.replace("Python", "World")) # Word replace
print(s.split(",")) # String split into list
Python Booleans (Boolean Values)
Boolean sirf do values leta hai: True aur False
x = True
y = False
print(x, y)
Comparison me bhi use hota hai:
print(10 > 5) # True
print(10 == 5) # False
print(bool("Hello")) # True
print(bool(0)) # False
Python Operators (Operators in Python)
Operators ka use calculations aur logical operations ke liye hota hai.
Arithmetic Operators:
a = 10
b=3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a % b) # Modulus (Remainder)
print(a ** b) # Exponentiation (Power)
Comparison Operators:
print(a > b) # True
print(a == b) # False
print(a != b) # True
Logical Operators:
print(True and False) # False
print(True or False) # True
print(not True) # False
Python Lists (Lists in Python)
List ek ordered aur mutable collection hoti hai.
fruits = ["Apple", "Banana", "Cherry"]
print(fruits[0]) # Apple
fruits.append("Mango") # Add new item
print(fruits)
Python Tuples (Tuples in Python)
Tuple ek ordered but immutable collection hoti hai.
tuple1 = (1, 2, 3, "Hello")
print(tuple1[1]) # 2
Tuples ko change nahi kar sakte.
Python Sets (Sets in Python)
Set ek unordered collection hoti hai jo unique values store karti hai.
s = {1, 2, 3, 3, 4}
print(s) # Output: {1, 2, 3, 4} (Duplicates remove ho jate hain)
s.add(5) # Add new element
print(s)
Python Dictionaries (Dictionaries in Python)
Dictionary ek key-value pair collection hoti hai.
student = {"name": "Rahul", "age": 20, "marks": 85}
print(student["name"]) # Rahul
student["age"] = 21 # Update value
print(student)
Python If ... Else (Conditional Statements)
If-Else ka use decision making ke liye hota hai.
age = int(input("Enter your age: "))
if age >= 18:
print("You can vote")
elif age == 17:
print("You will be eligible next year")
else:
print("You cannot vote")
Nested If Example:
num = 10
if num > 0:
if num % 2 == 0:
print("Positive even number")
else:
print("Positive odd number")
else:
print("Negative number")