Dear friends,
30 minutes ago, I had an appointment with Ken. Yes, we all know, that we normally program with C++ in this course. But Ken gave me at the beginning of the semester the great opportunity to program with python3 in this semester.
So a lot of things/codes are in python much easier and shorter than in C++. That was the reason for my appointment with Ken. Because of this reason i agreed with Ken that I am going to do a huge blog post with 15 mastery topics.
Enjoy…
Mastery Topic #1 – Use of comments
For a comment you just use the Hash symbol (#). What makes the comment to a comment, ist that every symbol after the # is grey.
Mastery Topic #2 – Python conventions (Zen of Python)
The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!
https://www.python.org/dev/peps/pep-0020/
Mastery Topic #3 – Basic types and their use
Basic (or also primitiv) types in python are: Integer, floats, strings, characters and booleans.
The use for integer and float is only for numbers. While the integer is for numbers without decimals is the float type for numbers with decimals.
String is for letters, numbers and whole sentences. While the char is only for one letter in the UTF table.
Boolean have just two conditions – true and false. The most use for a bool is in a loop.
Mastery Topic #4 – Basic output (print)
An output is, what the program shows the user.
Here an example:
Mastery Topic #5 – Basic user input (input)
The input is, what the user gives the program.
Here an example:
In this case, the user have to give a number to the program. The reason why we change the number in the second line is because python safe all inputs as a String. So we need to convert always the input when we need something else than a String.
Mastery Topic #6 & #7 – Calling functions & Creating functions
What we do here first, is that we define/create our function. In the second step we call it. The function could do something with the inport of the string and the number. But therefore we would need to code unter the definition what the function should do.
Mastery Topic #8 – Importing and using library
Importing and using of the library is necessary e.g. for mathematical operations.
with the keyword import and math we import the whole math library.
One of the function of the math library is the sqrt. So we call this function of the math library with the code math.sqrt()
Mastery Topic #9 – Creating and using you own libraries (using multiple files)
What i learned in 2 WSQ´s was, that we first should create a text file e.g. data.txt. The task was, that we should work with this text file in a program.
Here a example how we did this.
The most important part is the one in the red „circle“ (:D). We tell the program where the file is. The path. and After that, we give this file to the function file_count. Now the program is able to work with the text file.
Mastery Topic #10 – Use the conditional if
The if condition do something if a condition is fulfilled. In this case, the if just works, when the user gives a number higher that 50 as a input. If this not happens, the program would´t do anything with this if condition. The program would pass the if part and run normally after the passing.
Mastery Topic #11 – Using of else with a if condition
In a if-else case, the program also just take the if when the condition is fulfilled. If that not happens the program ALWAYS will take the else.
Mastery Topic #12 – Nesting of conditional statements (if´s inside if´s)
After #10 & #11, it is very simple to understand what the program is going to do here.
Mastery Topic #13 – Use of loops with „while“ and „do while“
A while loop starts with the keyword while. The specialty of this loop is, that he just do this loop as long as a is smaller than 10. When a would have 10 or bigger before the loop The loop would not run.
In the do while case we have the specialty, that the loop is going to run minimum one time. Just at the end of the loop, the condition is going to be checked. If the condition is true, the program jumps again to the beginning of the loop.
Mastery Topic #14 – Use of loops with „for“
The specialty of a for loop is that the programmer gives the conditions when he write the loop. In this case the programmer just say, work with i! Set i to 10 and print it until it is at the number 20!
Mastery Topic #15 – Nested loops
After you understand, how loops are working. there is no problem to unterstand how nested loops are working. Every loop has the same characteristics like before.
Hope this will help in the future. Enjoy and have fun!
Greetings from a German exchange student