Day 2: Exploring Python Data Types, Operations, Type Conversion, and F-Strings
Hello, everyone! Welcome to Day 2 of my 100 Days of Python journey, where I explored fundamental Python concepts like data types, numbers, operations, type conversion, f-strings, and built a practical tip calculator.
What I Learned Today
1. Data Types and Type Conversion: In Python, it's crucial to understand different data types and how to convert between them. Here's an example where I take a two-digit number as input, convert it into a string to access individual digits, and then sum those digits:
codenum = int(input("Enter a 2 digit number: "))
new_num = str(num)
sum = int(new_num[0]) + int(new_num[1])
print("The sum of the number is " + str(sum))
Input Function:
input()
gets user input as a string.Type Conversion:
int()
converts the string to an integer, andstr()
converts it back to a string to access individual digits.String Indexing:
new_num[0]
andnew_num[1]
access the first and second digits of the number.String Concatenation: Combining strings with
+
to create the final output message.
2. Calculating BMI: I calculated the Body Mass Index (BMI) using integer and float inputs, then formatted the output with f-strings for clarity:
weight = int(input("Enter your weight in Kg: "))
height = float(input("Enter your height in meters: "))
BMI = weight / (height ** 2)
print(f"Your BMI is {BMI}")
Integer and Float Conversion:
int()
andfloat()
convert input strings to numeric values.Exponentiation:
height ** 2
calculates the square of the height.F-Strings: Using
f"{variable}"
to embed expressions inside string literals for a clean and readable output.
3. Using len()
Function: The len()
function returns the length of a string. Here's an example:
print(len("Rishabh")) # Output: 7
String Length:
len("Rishabh")
returns the number of characters in the string "Rishabh".
Note: Using len()
on an integer directly will cause an error, as len()
expects a string or a sequence.
4. Calculating Time Left: I wrote a program to calculate the number of weeks left if one lives up to 90 years:
codetotal_age = 90
current_age = int(input("Enter your current age: "))
total_weeks_left = (90 - current_age) * 52
print(f"You have {total_weeks_left} weeks left")
Arithmetic Operations:
(90 - current_age) * 52
calculates the remaining weeks by subtracting the current age from 90 and multiplying by the number of weeks in a year.
5. Tip Calculator: As a practical application, I created a tip calculator. It takes the total bill, tip percentage, and number of people to split the bill among, then calculates each person's share:
print("Welcome to the tip calculator")
total = int(input("What was the total bill? Rs"))
tip = int(input("How much tip would you like to give? (in percent) "))
split = int(input("How many people to split the bill? "))
per_person_pay = (total + (total * tip / 100)) / split
print(f"Each person should pay: Rs {per_person_pay}")
Arithmetic Operations: Calculating the total amount including tip and then dividing by the number of people to get the amount each person should pay.
F-Strings: Formatting the output to display the amount each person should pay clearly.
Projects and Goals Ahead
As I move forward, my goals are:
Build More Projects: Continue applying what I learn by working on practical projects.
Engage with the Community: Connect with fellow learners, share insights, and seek feedback.
Join Me!
I invite you to follow my journey as I navigate through the world of Python programming. Whether you're a seasoned coder or just starting out, your support and engagement are invaluable. Together, let's make these 100 days of Python count!
Stay tuned for daily updates, challenges faced, and victories won. Remember to check out my GitHub repository for all the code I’ll be sharing along the way.
Conclusion
Day 2 has deepened my understanding of Python basics, especially in handling different data types and performing calculations. I eagerly look forward to what lies ahead. Thank you for joining me on this adventure—I can't wait to see where Python takes us next!
Until tomorrow, happy coding!