Day2: Python Basics: Primitive Data Types, Operators, and Practical Applications
I learned about primitive data types in Python, how to check the type of a variable, perform basic operations, and handle common errors like TypeError. Here's a breakdown of the key concepts I explored.
?? Primitive Data Types in Python:
1. Strings (str):
print("Hello"[0]) # Outputs 'H'
print("123" + "345") # Outputs '123345'
2. Integers (int):
print(123 + 345) # Outputs 468
print(123_456_789) # Outputs 123456789 (underscore improves readability)
3. Floats (float):
print(3.14159) # Outputs 3.14159
4. Booleans (bool):
print(True) # Outputs True
print(False) # Outputs False
?? Handling Errors: TypeError
A TypeError occurs when you use the wrong data type in an operation. For example, trying to find the length of an integer with len() will raise a TypeError:
# This will raise an error
# len(12345)
# This will not raise an error
len("Hello") # Outputs 5
?? Type Checking:
You can check the data type of any variable using the type() function. This is useful for understanding the type of data you're working with.
# Type Checking
print(type("abc")) # Outputs <class 'str'>
print(type(123)) # Outputs <class 'int'>
print(type(3.14)) # Outputs <class 'float'>
print(type(True)) # Outputs <class 'bool'>
?? Type Conversion:
You can convert data between different types using built-in functions like str(), int(), float(), and bool().
领英推荐
name_of_the_user = input("Enter your name: ")
length_of_name = len(name_of_the_user)
# Type checking
print(type("Number of letters in your name: ")) # str
print(type(length_of_name)) # int
# Convert the integer to a string before concatenation
print("Number of letters in your name: " + str(length_of_name))
? Basic Mathematical Operators:
Python supports basic arithmetic operators, including +, -, *, /, // (floor division), and ** (exponentiation).
?? PEMDAS:
Python follows the order of operations (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).
# PAUSE 1: What is the output?
print(3 * 3 + 3 / 3 - 3) # Outputs 7.0
# PAUSE 2: Change the code so it outputs 3
print(3 * (3 + 3) / 3 - 3) # Outputs 3.0
?? Number Manipulation:
1. Flooring: Convert a float to an integer to remove the decimal part.
print(int(3.738492)) # Outputs 3
2. Rounding: Use the round() function to round a number.
print(round(3.738492)) # Outputs 4
print(round(3.14159, 2)) # Outputs 3.14 (rounds to 2 decimal places)
3. Assignment Operators:
score = 0
score += 1 # Adds 1 to score, making it 1
score *= 2 # Doubles score, making it 2
?? f-Strings:
You can use f-strings to easily embed variables into strings.
age = 12
print(f"I am {age} years old") # Outputs "I am 12 years old"
bmi = 84 / 1.65 ** 2
print(f"Your BMI is {round(bmi, 2)}") # Rounds BMI to 2 decimal places
?? Tip Calculator Project:
Finally, I worked on a Tip Calculator to apply all the skills I’ve learned.
print("Welcome to the tip calculator!")
bill = float(input("What was the total bill? $"))
tip = int(input("What percentage tip would you like to give? 10 12 15 "))
people = int(input("How many people to split the bill? "))
tip_as_percent = tip / 100
total_tip_amount = bill * tip_as_percent
total_bill = bill + total_tip_amount
bill_per_person = total_bill / people
final_amount = round(bill_per_person, 2)
print(f"Each person should pay: ${final_amount}")
?? Key Concepts:
Stay tuned as I continue to build my Python knowledge and develop projects during my #100DaysOfCode journey! ??