Day-2, 100 days of Python

Day-2, 100 days of Python

On the second day, I learned about Data types, Numbers, Operations, Type conversions, f-strings. All together at the end of the day, we can do a tip generator program where it shows the amount of tip to be paid individually.

Data Types:

There are mainly four data types in python. They are :

  1. int: It has all the values negative, zero, and positive. ex. 123+456
  2. float: decimal values come into this category. ex. 12.09+13.11
  3. string: all values wrapped by double quotes are strings. ex. "123"+"456"
  4. boolean: it determines the statement to be True or False. Note: capital T and F should be used.

Type conversations:

To convert a variable x into int we can use int(x). If we want to convert x into the string it can be done by str(x) Conversion to float can be done by float(x)

a = input("enter a two digit number")
print(type(a))
b = int(a[0])
c = int(a[1])
d = b+c
print("Sum is "+str(d))

The above program takes a two-digit number as input. But by default, the input type is a string. So, we are extracting each letter and converting it to int. Next, we add both the extracted numbers, and while printing we are converting the number to string as the string value "Sum is" will be concatenated with string only.

BMI calculator:

height = input("enter your height in m :")
weight = input("enter your weight in kg :")
#bmi = weight/(height*height)
#print(type(height))
a = float(weight)
b = float(height)
bmi = a/(b*b)
print(int(bmi))
print(type(bmi))

In this example, height and weight are float values. So, both of them are converted to float datatype.

Mathematical Operations:

The five primary mathematical operations are Addition, Subtraction, Multiplication, Division, and Power.

7+3 #add
11-1 #sub
5*2 #mul
20/2 #div
2**3 # it is 2 power 3

We all know that mathematics follows the BODMAS rule. But here multiplication and division, addition, and subtraction have the same priority and always compiles from left to right.

f- strings:

As discussed earlier, to concatenate with string other datatypes should also be converted into a string. But it is not the case with f-string. The syntax is everything must be wrapped inside double quotes with f placed in the starting of double-quotes. Other data type values must be enclosed in closed Curley braces

No. of days left program:

age = input("what is your age")
years_left = 90 - int(age)
print(years_left)
days_left = years_left*365
mnths_left = years_left*12
weaks_left = years_left*52
print(f" you have {days_left} days left, {weaks_left} mweeks left, {mnths_lfet} months left")

Round function:

It rounds the value. For example, if the number is x.5 or above then it rounds to x+1, and if it is less than x.5 then it rounds to x-1.

Tip calculator program:

print("Welcome to tip calculator")
total_bill = float(input("What was total bill "))
tip = float(input("What percentage of tip you want to give? 10, 12, or 15?? "))
a = tip/100
p = float(input("How many people to split the bill "))
total_tip = total_bill+(total_bill*a)
pay_tip = round(total_tip/p,2)
print(f"Each person should pay: {pay_tip}")