Lecture 5

Introduction to Python Programming Language

Basic arithmetic operations

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 in the commandline

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.

Python in Vim

To see clearer you can start using Screen here screen -S lecture4.

Open a file with .py extension vi firstPythonCode.py.

print in Python

Use print("") command to print out.

input in Python

You can ask user to type something when it runs the code.

Basic data types

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:

Variables

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.

Lists in Python Programming Languange

Makiing lists

You can make lists in Python as follows:

Use the lists

Read an item in a list and make operations.

Read multiple vaules from lists.