DEV Community

lordronjuyal
lordronjuyal

Posted on

day 2

date: 20 May, 2024.
Maths Operator:- These are used for mathematical calculations. Python follows the PEMDAS rule. () stands for parentheses, ** stands for exponents eg 2**3 >8, * for multiplication, / for division, + addition, and - for subtraction.
One main thing to remember is division of two int data types will result in a float. Also if one float is present in the calculation the result will be the float always.
We can't do math operations on string. In the case of a boolean True =1, False =0.
If we want to do division but want it to return int data type, we can use floor division(//). This returns quotient eg 10//3 >>3. If we want the remainder we can use % sign, eg 10%3 >>1. But we have to be careful if we are using negative numbers in the calculation, as it may give unexpected results. On searching I found floor division uses the floor function(some rounding function I will study it later) in the background, which rounds off the result to the largest integer less than or equal to the number, eg 10//-3 >> -3.333 = -4.

Strings:- To add other data types variables into a string when showing output, we have to convert them and then concatenate them with the string. An alternative to this is f-string, we write it like f" ... {variable}". This is auto converts variables into strings and useful when putting multiple variables in a string. Another method is to use the format method(method are like function). It can be used as: string= " ... {1} {2}"
string.format(1=variable_1, 2=variable_2). Variable_1 and variable_2 will be placed at 1 and 2 respectively. We can leave these {} empty but better avoid it for confusion.
We can also change the number format in a string using these two ways. eg x=1234.5678 To format it into 6 figures we can write f"{x:.6}" or {:.6}.format(x) >> 1234.56. There are many other ways to format like adding commas, showing in percentage, etc. It's better to look into documentation rather than trying to remember them.

Built-in functions:-

  1. type(variable) returns the data type of the variable
  2. type casting -it is a process of converting one data type into another.

-->int(variable) converts into an integer data type. The strings data type needs to be in whole number format. For float data types decimals will be removed. In the case of boolean, True converts into 1, and False converts into 0.

-->float(variable) converts into a float data type. Similar to int but can convert strings with decimals numbers.

-->str(variable) converts into string. In the case of a boolean, False will turn into 'False', same with True.

-->bool(variable) converts into a boolean data type.

  1. round(number, digit of precision after decimal) it will round the number to the digit of precision. If a digit of precision is not passed it will round it off to the nearest integer.

Exercises -- I did two exercises, one was to calculate BMI by taking user input. Another one is to calculate the tip based on a number of people & net amount and show how much each person should pay.

BMI Calculator

Tip calculator

Thank you for reading my blog.


Personal --> Today I was able to complete the day 2 fast, I am happy with the progress though I have planned more for it. I tried a codewar problem but was not able to solve it. I will try to solve it if I get some time. Tomorrow markets will be open so I have to prepare for that also. I have realized my English isn't that good, I will try to improve that also.

Top comments (0)