In Python, data types are like different boxes that can hold different types of things. Each type of box is best suited for certain types of items. Let’s dive deeper into some of these types and learn how to use them effectively.
Working with Integers and Floats
Integers are numbers without decimals. They are great for counting things or keeping track of whole items, like the number of students in a class.
Floats are numbers that have decimals. They are useful when you need more precision, like when you"re measuring something in centimeters or calculating a price with cents.
Example:
Python
# Integers and Floats
apples = 10 # Integer
price_per_apple = 0.75 # Float
total_cost = apples * price_per_apple # This will give 7.5, which is a float
print("Total cost:", total_cost)
Playing with Strings
A string is a sequence of characters, like letters, numbers, and symbols, all put together in a line. Strings are used for text, like names, sentences, or any other kind of text.
You can do a lot of cool things with strings in Python, like combining them, repeating them, or picking out specific letters.
Example:
Python
# Working with Strings
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name # Combine strings
greeting = "Hello, " * 3 # Repeat the string 3 times
print("Full name:", full_name)
print("Greeting:", greeting)
Lists: A Collection of Items
A list is like a to-do list or a shopping list where you can keep multiple items. Lists are super flexible because you can add, remove, or change items whenever you want.
Example:
Python
# Creating and Using Lists
shopping_list = ["eggs", "milk", "bread"]
shopping_list.append("butter") # Add an item to the list
shopping_list[0] = "cheese" # Change the first item to "cheese"
print("Shopping List:", shopping_list)
Tuples: Fixed Collections
A tuple is like a list, but once you create it, you can’t change it. This is useful when you have a collection of items that should stay the same, like the days of the week or the coordinates of a point on a map.
Example:
Python
# Creating and Using Tuples
days_of_week = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
print("Days of the Week:", days_of_week)
Sets: Unique Collections
A set is a collection of items where each item must be unique. Sets are great for storing items where duplicates don’t make sense, like a collection of unique numbers or names.
Example:
Python
# Creating and Using Sets
unique_numbers = {1, 2, 3, 4, 4, 5} # Duplicate "4" will only appear once
print("Unique Numbers:", unique_numbers)
Dictionaries: Key-Value Pairs
A dictionary is like a real dictionary where each word has a definition. In Python, you use a key to look up a value. This is really handy when you want to store information about something, like a person’s name and age.
Sometimes, you might need to change one type of data into another. For example, you might want to turn a number into a string so you can combine it with other text. Python makes it easy to convert data types.
Example:
Python
# Converting Data Types
age = 21
age_str = str(age) # Convert integer to string
print("I am " + age_str + " years old.")
Understanding Mutability
In Python, some data types can be changed after you create them, and some cannot. If you create a list, you can add or remove items from it, but if you create a string, you can’t change it directly. Knowing whether a data type is mutable or immutable helps you avoid errors in your code.
Example:
Python
# Mutable vs Immutable
my_list = [1, 2, 3]
my_list.append(4) # You can change lists
my_string = "Hello"
# my_string[0] = "J" # This will cause an error because strings are immutable
print("Updated List:", my_list)
print("String:", my_string)
Special Data Types
Python also provides several special data types that have specific uses. These include:
None
None is a special type that represents the absence of a value or a null value. It's commonly used to signify that a variable has no value.
Example:
Python
# Using None
result = None
if result is None:
print("No result yet.")
Complex Numbers
Complex numbers have a real part and an imaginary part. They are used in mathematical computations involving complex numbers.
Example:
Python
# Creating and Using Complex Numbers
complex_number = 3 + 4j
print("Real part:", complex_number.real)
print("Imaginary part:", complex_number.imag)
Data Types in Python
Python mein, data types alag-alag boxes jaise hote hain jo alag-alag types ki cheezein rakh sakte hain. Har type ka box kuch specific types ki items ke liye best hota hai. Chaliye, in types ke baare mein aur jaante hain aur seekhte hain ki inka sahi tareeke se kaise istemal karein.
Working with Integers and Floats
Integers aise numbers hote hain jisme decimals nahi hote. Ye cheezein ginna ya poori items ka hisaab rakhne ke liye bade kaam aate hain, jaise ek class mein students ki sankhya.
Floats aise numbers hote hain jisme decimals hote hain. Ye tab useful hote hain jab aapko zyada precision chahiye, jaise centimeters mein measurement ya kisi cheez ki keemat calculate karna jis mein paisa bhi include ho.
Example:
Python
# Integers aur Floats
apples = 10 # Integer
price_per_apple = 0.75 # Float
total_cost = apples * price_per_apple # Isse milega 7.5, jo ek float hoga
print("Total cost:", total_cost)
Playing with Strings
Ek string characters ka ek sequence hota hai, jaise letters, numbers, aur symbols, jo sab mil ke ek line mein hote hain. Strings ka use text ke liye hota hai, jaise naam, sentences, ya koi bhi aur tarah ka text.
Aap Python mein strings ke saath kaafi cool cheezein kar sakte hain, jaise unhe combine karna, repeat karna, ya specific letters ko nikalna.
Ek list ek to-do list ya shopping list jaise hoti hai jisme aap multiple items rakh sakte hain. Lists kaafi flexible hoti hain kyunki aap kabhi bhi items ko add, remove, ya change kar sakte hain.
Example:
Python
# Lists Banana aur Use Karna
shopping_list = ["eggs", "milk", "bread"]
shopping_list.append("butter") # List mein ek item add karna
shopping_list[0] = "cheese" # Pehla item ko "cheese" se badalna
print("Shopping List:", shopping_list)
Tuples: Fixed Collections
Ek tuple list jaisa hi hota hai, lekin ek baar jab aap ise bana lete hain, toh aap ise badal nahi sakte. Ye tab useful hota hai jab aapke paas items ka collection hai jo same rehna chahiye, jaise dinon ke naam ya ek map par kisi point ke coordinates.
Example:
Python
# Tuples Banana aur Use Karna
days_of_week = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
print("Days of the Week:", days_of_week)
Sets: Unique Collections
Ek set items ka collection hota hai jisme har item unique hona chahiye. Sets un cheezon ko store karne ke liye bade kaam aate hain jahan duplicates ka koi matlab nahi banta, jaise unique numbers ya names ka collection.
Example:
Python
# Sets Banana aur Use Karna
unique_numbers = {1, 2, 3, 4, 4, 5} # Duplicate "4" sirf ek baar hi appear hoga
print("Unique Numbers:", unique_numbers)
Dictionaries: Key-Value Pairs
Ek dictionary real dictionary jaisa hi hota hai jisme har word ka ek definition hota hai. Python mein, aap ek key ka use karke value ko dekh sakte hain. Ye tab kaafi handy hota hai jab aapko kisi cheez ke baare mein information store karni hoti hai, jaise ek person ka naam aur age.
Kabhi-kabhi aapko ek type ka data doosre type mein badalne ki zaroorat hoti hai. For example, aap ek number ko string mein badalna chahenge taaki aap usse doosre text ke saath combine kar sakein. Python mein data types ko convert karna aasan hota hai.
Example:
Python
# Data Types ko Convert Karna
age = 21
age_str = str(age) # Integer ko string mein convert karna
print("Main " + age_str + " saal ka hoon.")
Understanding Mutability
Python mein, kuch data types aise hote hain jo aap unhe create karne ke baad change kar sakte hain, aur kuch ko nahi kar sakte. Agar aap ek list banate hain, toh aap usme items add ya remove kar sakte hain, lekin agar aap ek string banate hain, toh aap ise directly change nahi kar sakte. Yeh jaan na ki ek data type mutable hai ya immutable, aapko code mein galtiyon se bachne mein madad karta hai.
Example:
Python
# Mutable vs Immutable
my_list = [1, 2, 3]
my_list.append(4) # Aap lists ko change kar sakte hain
my_string = "Hello"
# my_string[0] = "J" # Yeh error dega kyunki strings immutable hote hain
print("Updated List:", my_list)
print("String:", my_string)
Special Data Types
Python kuch special data types bhi provide karta hai jo specific uses ke liye hote hain. Inmein shamil hain:
None
None ek special type hai jo kisi value ki absence ya null value ko represent karta hai. Yeh commonly use hota hai jab ek variable ke paas koi value nahi hoti.
Example:
Python
# None ka Use
result = None
if result is None:
print("Abhi tak koi result nahi.")
Complex Numbers
Complex numbers ke paas ek real part aur ek imaginary part hota hai. Inka use mathematical computations mein hota hai jo complex numbers se jude hote hain.