Sign | Name | Ex | Results |
---|---|---|---|
+ |
Addition | 7+3 | 10 |
- |
Substraction | 7-3 | 4 |
\* |
Multiplication | 7*3 | 21 |
/ |
Division | 7/3 | 2.3333 |
% |
Modulo | 7%3 | 1 |
** |
Exponentiation | 7**3 | 343 |
Python can be used in the commandline by typing python
(or python3
for 3rd version of Python).
You can try the arithmetic operations above in the commandline Python and press ctrl+d
or type exit()
to exit.
To write the output of the commandline Python into a file use python3 > file
command.
Or you can both see the results in commandline Python and write into a file by python3 | tee file
. Try it.
To see clearer you can start using Screen here screen -S lecture4
.
Open a file with .py
extension vi firstPythonCode.py
.
print
in PythonUse print("")
command to print out.
input
in PythonYou can ask user to type something when it runs the code.
Integer, floating-point numbers, strings and boolean are the four data types in python.
See for integers and floats for example:
and for strings and booleans:
We can define variables as follows:
We can also ask variables to the user with input()
. But the input is in string type as default. If we use it as integer, float or boolean we should express it.
You can make lists in Python as follows:
Read an item in a list and make operations.
Read multiple vaules from lists.