These are some examples on basic commands and programming methods in Python.
N.B : I did it on my Raspberry Pi 3 and written on on Python 3
To putting text into the screen :
Result will be :
Arithmetic :
We can put two print commands into the same line using a ; semicolon. The second print command gives a line space between each equation.
You will notice that the answer for division sum includes decimal place.
Result
To remove the the decimal, we can use 'INT' function and answered number will be rounded to the nearest whole number,
With variables, we can get our program remember numbers and words, which we can use later.
and the result will be :
Variables can be words which called 'strings' because they are string of alphanumeric strung together.
Result will be :
The use of 'FOR LOOP' for counting in Python. In this example, the number (n) starts at zero and increases to 10. Note that a +1 is need to be added, otherwise the loop will stop at 9.
Result will be :Other example with different number and counting step,
The 'WHILE LOOP' continues until a certain criteria is met.
There are some situations where 'WHILE LOOP' are more suitable than 'FOR LOOP', usually in less predictable circumstances.
To achieve the incremental steps in counting, change the n+1 to higher number.
To counting in reserve,
Here is an example of 'WHILE LOOP' to keep generating random number until it find intended number.
The operator '!=' means'not equal to' , in other word, keep looping until find the number.
This example below creates something called a 'STRING ARRAY', which a list of separate words stored in variables.
Comments
Post a Comment