On Day 2 of my 100 days of code challenge I have learned:
DataTypes
String data type e.g., "Hello"
We can also access a character of string instead of getting whole string this is called Subscripting. print("Hell0"[0])
print("Hello"[0])
Output: H
There is function len() in python which only works on string data type that returns length of the string. Integer data type e.g., 123
Add underscore to make large number readable 123_456_789 but this number remains as integer Float data type e.g., 3.14159
Python have function used to round float numbers it works as:
round(3.14159)
output: 3.14
or we can use print(8 // 4) to round of the digits after decimal. Boolean data type i.e., True False
Converting Types
We can check the type of variable by type(). Also, for the conversion of one type to another we can use float(), int() etc.
Mathematical Operations in Python
The mathematical operation in python are [+,-, asterisk,/, double asterisk]. In python, there are precedence rule to excite each operation which is PEMDASLR i.e., Parenthesis, Exponent, Multiplication, Division, Addition, Subtraction. Where Multiplication and division has equal precedence and addition and subtraction also have equal precedence so in that case it give precedence to the operator present on the left and then to the right.
Another thing is we can apply mathematical operation on the previous variables in a shorter way:
score += score
score /= score
which means
score = score + score
score = score / score
F-Strings
When it comes to print, one way of doing is by concatenation of strings but its very long process first we have to convert other types of variables into string data type because we can only concatenate strings not integer with strings or other data type.
#Traditional way
print("Your score is " + str(score) + " " + str(height) + " " + str(isWinning))
The smarter way is by using f-string in here we do not need to convert other data types into string data types, f-string do all the process on our behalf. We just have to put variable names inside the curly brackets.
#the more smarter way is by using f-string it covert all variables into strings
print(f"Your score is {score}, your height is {height}, you are winning {isWinning}")
Project: Tip Calculator
Day 2 project was about splitting bills between peoples including tips. You can look our the project code on my GitHub repository.