Python Learning Journey - Day 5: Data Types
We've already encountered two important data types:
Now, let's explore a few more essential ones:
1. float (Floating-Point Numbers)
As we briefly touched upon, floating-point numbers represent real numbers with decimal points. They are used when you need to work with fractions or numbers that aren't whole.
pi = 3.14159
temperature = 98.6
price = 19.99
Keep in mind that due to the way computers represent floating-point numbers internally, you might sometimes encounter small discrepancies in calculations.
2. bool (Booleans)
Boolean values represent truth or falsehood. There are only two possible boolean values in Python: True and False (note the capitalization). Booleans are often the result of comparisons or logical operations.
is_raining = False
is_sunny = True
age_greater_than_18 = 25 > 18 # This evaluates to True
Booleans are crucial for controlling the flow of your program, as we'll see when we learn about conditional statements (like if statements).
3. list
A list is an ordered collection of items. Lists are incredibly versatile because they can hold items of different data types, and they are mutable, meaning you can change their contents after they are created. Lists are defined by enclosing items in square brackets [], with items separated by commas.
my_list = [1, 2, 3, 4, 5]
mixed_list = [10, "hello", 3.14, True]
empty_list = []
You can access individual items in a list using their index (position), starting from 0.
Recommended by LinkedIn
print(my_list[0]) # Output: 1 (the first element)
print(mixed_list[1]) # Output: hello (the second element)
4. tuple
A tuple is also an ordered collection of items, similar to a list. However, tuples are immutable, meaning once you create a tuple, you cannot change its contents. Tuples are defined by enclosing items in parentheses (), with items separated by commas.
my_tuple = (1, 2, 3)
another_tuple = ("apple", "banana", "cherry")
single_item_tuple = (5,) # Note the trailing comma for a single-item tuple
You can access items in a tuple using their index, just like with lists.
print(my_tuple[1]) # Output: 2
print(another_tuple[0]) # Output: apple
5. dict (Dictionary)
A dictionary is an unordered collection of key-value pairs. Each key in a dictionary is unique and is used to access its corresponding value. Dictionaries are defined by enclosing key-value pairs in curly braces {}, with keys and values separated by colons : and pairs separated by commas.
my_dictionary = {"name": "Alice", "age": 30, "city": "New York"}
empty_dictionary = {}
You access values in a dictionary using their keys:
print(my_dictionary["name"]) # Output: Alice
print(my_dictionary["age"]) # Output: 30
Coding Challenge Day 5: Exploring Data Types
Let's put your understanding of these new data types to the test!
Challenge:
Give this challenge a go! Don't hesitate post your comment on how to work with these data types. I'm excited to see your code! 😊
What will be the output of the following code if the user enters 5? num = input("Enter a number: ") print(num * 2) *Correct Answer: 55* input() always returns a string. So when the user enters 5, num becomes the string "5". num * 2 means repeating the string "5" two times → "55". It does not perform mathematical addition unless you first convert the input to an integer. *Thus, the output is 55.* Most of you thought it would be 10 which is pretty much expected, If you want users to enter a number and treat it like a number, you must convert it to integer like this: num = int(input("Enter a number: ")) print(num * 2) Now if they enter 5, int(5) * 2 = 10 (integer multiplication). Hope this clarifies most of the doubts. *React ❤️ if this helped you*
*Basic String Formatting:* There are better ways to format your output! *1. f-Strings (Recommended - Python 3.6+):* name = "Alice" age = 25 print(f"My name is {name} and I am {age} years old.") *2. Using .format() method:* print("My name is {} and I am {} years old.".format(name, age)) *Both ways are clean, but f-strings are shorter and easier to read.* *Key Points to Remember:* - input() always returns string type. - Use type conversion when needed (int(), float(), etc.). - Use f-strings for cleaner and more readable output.
*Input, Output, and Basic String Formatting* *Taking Input from the User:* In Python, you can use the input() function to take input from the user. *Example:* name = input("Enter your name: ") print("Hello,", name) - input() always returns data as a string. - Whatever the user types will be stored in name. *Converting Input to Other Types:* If you want the input as a number, you have to convert it. Example: age = int(input("Enter your age: ")) print("You are", age, "years old") *Here, int() is used to convert the input from string to integer.* Similarly: - float() for decimal numbers - bool() for true/false values (careful with this one) *Printing Output:* You can print multiple items using commas: print("My name is", name, "and my age is", age) *Python automatically adds a space between items when you use commas.*
Once you're comfortable with basics..start looking into vibe coding in python
Thanks for sharing, Dayasagar