Float: This data type represents decimal numbers. Think of floats like spices - they add flavor and variety to a recipe, just like different spices can change the taste of a dish.
String: This data type represents text. Think of strings like a sauce - they add flavor and character to a dish, just like a sauce can enhance the taste of a meal.
Boolean: This data type represents a true/false value. Think of booleans like salt - they help you make decisions about your recipe, just like salt helps you balance the flavors in a dish.
List: This data type represents a collection of items, which can be of different data types. Think of lists like a salad - they can be made up of many different ingredients, just like a salad can contain many different vegetables and toppings.
Tuple: This data type is similar to a list, but it's immutable (meaning it can't be changed). Think of tuples like a fruit salad - once you've combined all the fruits, you can't easily separate them again.
Set: This data type represents a collection of unique items, with no duplicates. Think of sets like a cheese platter - each cheese is unique and distinct, just like each item in a set.
Dictionary: This data type represents a collection of key-value pairs, where each value is associated with a unique key. Think of dictionaries like a recipe book - each recipe is associated with a title or name, just like each value in a dictionary is associated with a unique key.
x = 10
y = -5
z = 1000
p
i = 3.14
temperature = 25.5
weight = 2.5
greeting = "Hello, world!"
name = "John"
address = "123 Main St."
is_raining = True
is_sunny = False
has_umbrella = True
fruits = ["apple", "banana", "orange"]
numbers = [1, 2, 3, 4, 5]
mixed_list = ["apple", 1, True, 3.14]
coordinates = (10, 20)
colors = ("red", "green", "blue")
unique_numbers = {1, 2, 3, 4, 5}
unique_fruits = {"apple", "banana", "orange"}
person = {"name": "John", "age": 30, "location": "New York"}
book = {"title": "Python for Beginners", "author": "Jane Smith", "year": 2022}
Type casting and type conversion
Have you ever played with different types of toys, like dolls, action figures, or stuffed animals? Just like toys come in different shapes and sizes, data in Python also comes in different types, like strings, numbers, and lists.
Type casting is like taking one toy and turning it into another toy. For example, if you have a doll but you want to pretend it's a superhero, you might put a cape on it and call it "Super Doll". In Python, if you have a number stored as a string (like "10"), you can use type casting to turn it into an actual number (like 10), so that you can do math with it.
# Casting from float to int
x = int(3.14)
print(x) # Output: 3
# Casting from string to int
y = int("10")
print(y) # Output: 10
# Casting from int to string
z = str(123)
print(z) # Output: "123"
Type conversion is like taking a bunch of toys and putting them together to make a new toy. For example, if you have a bunch of Legos, you can use them to build a spaceship. In Python, if you have a list of strings, you can use type conversion to turn them into a single string by joining them together.
YOU ARE READING
Python Programming for Lazy Beginners: A Simple and Easy Tutorial
Non-FictionPython Programming for Lazy Beginners: A Simple and Easy Tutorial is a comprehensive guidebook for anyone who wants to learn the basics of programming using the popular Python language. This book is specifically designed for beginners who have no pr...
CHAPTER 3
Start from the beginning
