Title | Python techniques for student |
---|---|
Author | Dang Vi Luan |
Course | Diploma of Design (UniLink) |
Institution | Swinburne Online |
Pages | 3 |
File Size | 253.2 KB |
File Type | |
Total Downloads | 4 |
Total Views | 141 |
Python Technique note for information and technology student...
Python techniques - Input two number or more on the same line. Map(function,interable) can apply a function onto interables, so we use split() to change inpu in to list so that map can work on them , and apply int function . - N digits after decimal point and the use of format() function. Format function can place value inside a placeholder ( {} ) {:.2f} is used to define a value and specifies the decimal point ( change the number to change decimal point ) - Int object is not iterable
For ex: we want to do sth in a number ( 123443234 ) we can’t work on each digit in this number if it is an int , so we can change it to str to process and change it again to int to operate on them. - The divide operators in python is very tricky so you need to understand them: + The % will divide and take the rest as the answer ( 5 % 2 = 1 ) + The // will give the result down to the nearest whole number (15//2 =7) + The / will give the result if it is fully divideable if itn’t it will return float = 4 but 13 / 3 = 4.33333) Using these operator flexibly and you can work with number much easier for ex : + 2134 % 10 = 4 ( so you can take the last digit of a number ) + 2134 // 10 = 2134 ( so you can eliminate the last digit of a number )
(12 /
- We usually use for loop when we are certain of the number of input or if there is only one input In this example we want to calculate the sum of a number and all the number before it ( 5 = 15 ) We are certain of the input and the number of it so we can use for loop Note that range will only run from ( begin to end – 1) So in this case we declare range (1 , x +1 ) - We usually use while loop when we arent certain of the numbe of input or if we need to input limitless or too many input.
In this ex we want to input limitless input until input is 0 so we use while loop . There should be a condition for break or else the loop will be endless.
- When we want to compare two input or more we can use this technique . In this ex: we want to compare each input x with max and min, so we declare two var (max min) as a holder for x. In the very first input ( for ex 1 ) max and min will be reset to 1 ( 1 >0 => max = 1 ) ( 1 min = 1 ) And in the next input x will be compare with 1 to find which will be max and min and so on.
- Setting Flag is an useful technique to process through all input before printing out output or when we want to delay printing out output for some reasons. In this code to find prime number, line 4,5’s flag is used to dela printing out output ( as it’s only the first case and we need to work on orther cases also ). From line 6 -> 7, this flag is used to process through all of our input ( if we don’t use flag here and say print(“NO”) it will print no or yes each time x is divided by i and that’s not what we want).
We can set flag to true or false base on the problem for ex: + Input a list of number, is it true that all of this number is fully dividable by 3 ? case we can set flag = true and find 1 number that is not then break. + Input a list of number, find at least 1 number that is dividable by 5 ? case we can set flag = false and find 1 number that is dividable then break.
in this in th
- Remember that when declaring a function the position of return is very important. If we want to return count after processing all the output remember to place return outside of the loop. In fact pay close attention to the position of return in all cases.
-...