Hey! Here, am on Day 02 of Angela's course. Though my heading says Day 04 which is actually done to stay in order of all the python learning blog posts to date.
Starting WILT blog with checked Day 02 completion in the daily tracker poster. This kinda gives
self-satisfaction.
So, the end project of today's day was to build a tip calculator. It splits the bill among "n" people on the basis of percentage tip. After all the calculations, it was showing the final amount to pay by "n" people.
Before coming to this project completed 3 exercises: BMI calculator, Your Life in Weeks, and adding a string as 2 digits number. Here, is the end result of the percentage tip calculator project:
Now, comes the What I learned in DAY:02
1. len() function is incompetent in working with integers. If we even
try; the python compiler will throw an error:
print(len(123456))
#It throws the traceback error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
print(len(123456))
TypeError: object of type 'int' has no len()
Because we are trying to know the int's (integer) length & len function isn't trained to do so. It can
only do the assigned task for strings only.
2. We have data types in python that allows using a different type of entries.
a. String
Syntax: a="1234"
Subscript: The method of pulling out the particular character in the string type data is called a
subscript.
Syntax:
print("helloworld"[0])
-> This will print out the H since H is the very first element placed at index 0.
Note:
1. print("helloworld"[-1]) -> prints the last character. Initial index starts from zero
& last index starts from -1
2. print("123""345") -> concatenates the two strings
b. Integer
Syntax: a=1234
Note:
1. All the no.s whether positive or negative or whole no. comes under integer data types.
2. print(1234+1234) -> It will give the sum of two numbers.
3. print(123_456_758) -> Computer visulaises it as large no. & prints it after removing underscore:
123456758
c. Float
Decimal numbers are floating-point numbers.
Syntax: a=12.34
d. Boolean
-Most Used-
Syntax: a= True
or
a=False
Note:
1. While assignments, booleans don't have quotation marks around them. Otherwise, it would turn them into a String. ❌ a="TRUE" ❌ a=true 3. Type checking in python is done by the keyword type(). It basically shows the data type/class type. Syntax: type(12) output: class<int> Example: print(type(len(input("What is your name\n")))) output: What is your namekawal <class 'int'> because we have printed out the length of the string 4. Type Conversion is actually a change. Using this we can implicitly or explicitly change the data type. Syntax: str(a) int(a) float(a) Example: a=100 print(a) print(type(a)) #type of the variable is integer s=str(a) #type casted the int vaiable to string print(s) print(type(s)) 5. Total we have 6 Mathematical Operators in Python which follow the PEMDAS order. The calculation proceeds from left to right to have the final result. 1. + ( Addition ) 2. - ( Subtraction ) 3. ** ( Exponentiation ) 4. // ( Floor division ) : It chops off the result & gives an int as a final answer. 5. * ( Multiplication ) 6. / ( Division ) : It always gives the float as a final result. PEMDAS is: Paranthesis <- Exponentiation <- Multiplication <- Division <- Addition <- Subtraction 1 2 3 4 5 6 Example: (3*3+3/3-3) First: 3*3: 9 Second: 3/3: 1 9+1-3 Third: 9+1:10 Fourth: 10-3: 7 is the answer. 6. Number manipulation in python is when we forcibly(using an inbuilt function) changes the output's behavior. For example: print(8/3) -> It will output floating-point number. In order to print the int type of the result, we need to convert it to type int print(int(8/3)) -> It will output int type number. Another way of getting the non-floating point number is using the floor division. print(8//3) -> It will chop off the after decimals & will print int type. If you check its type, will show the result is of type int. print(type(8//3)) class <int> One more most used manipulation is a round function which rounds off the number. Example: print(round(8/3)) -> It will output instead of printing 2.666666 will print out 3. Using this function, we can also go a step further & decide the number of desired precision. Suppose, if you want to round it to 4 We can write it as: print(round(8/3,4)) -> It will output 2.6667 OR bill_per_person=99/4 bill="{:.4f}".format(bill_per_person) OR bill_per_person=99/4 print(f"Bill per person is: {bill_per_person:.4f}") Note: F-strings in python are used when we are in need of printing different types of data types altogether. s=5.7 t=True Syntax: print(f" {s},{t}") Example: score=0-> int height=1.8 ->float is_Winning=True ->boolean print("your score is" + str(score) + str(height) + str(is_Winning ) 🡇 In place of this, we will be using a different manipulation which is the f-string. 🡇 print(f"your score is {score}") ->It does all the conversions in the backend. You don't have to worry about this. print(f"your score is {score}, your height is {height}, your is_winning ia {is_winning}") -> handling conversions of multiple data types Output: Your score is 0, your height is 1.8, your is_winning is True It cuts down the manual labor of type conversions. 7. Python has different ways of writing mathematical calculations. Among them, I loved s+=1 the most. It basically adds 1 to the value of the s and stores the end result back to s. The second way of writing mathematical calculations: s=4+2 -> output: 2 That was all! Overall, it was an amazing day while learning all the above-mentioned 7 pointers. The course is really well structured and has a notch to keep working on it. Thank You and will see you again in my next day update. I am planning to do it today itself. Let's see! | |