Python Basics
- We use Python 3 (version 3.9.5) in this course. For this we need to load a module:
$ module load python/3.9.5
- When you type python3 on the command line the Python-Interpreter comes ready:
$ python3
Python 3.9.5 (default, Jun 3 2021, 15:06:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
- With quit(), exit(), or Ctrl-D you can close the Python-interpreter
$ python3
Python 3.9.5 (default, Jun 3 2021, 15:06:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
exit()
- With arrow keys "up" (
↑) and "down" (
↓) you can scroll through previous commands
Python as Calculator
>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 12.45 / 100 + 7.5e-3
0.132
>>>
Python Output
- By default the output appears in the terminal (the window you are working in)
- With the print() function you can write text
>>> print("Welcome to our Introductory Linux Course!")
Welcome to our Introductory Linux Course!
>>> print(2)
2
>>> print(2+2)
4
>>>
Python Assignment
- We can store text or values in variables (assign text or values to variables) in order to
- Conveniently refer to them
- Separate Python code from data
>>> greeting = "Welcome to our Introductory Linux Course!"
>>> print(greeting)
Welcome to our Introductory Linux Course!
>>> number = 2
>>> print(number)
2
>>>
- The assignment is done with the equal sign ( = )
- Names of variables may be chosen freely, but
- must consist of a single word (no blanks)
- must not contain special characters except "_", and
- must not begin with a number
- Valid names are: my_variable, Value15
- Invalid names are: my-variable, 15th_value
- Python knows different types of data
- int/float for numeric data (integer or floating point numbers)
>>> 4 + 2
6
>>> 4 + 2.0
6.0
>>> 8 / 5
1.6
>>> 8 / 5.0
1.6
>>>
Note: In Python 2 division of two integer numbers results in an integer: 8/5 = 1
Strings can be single quoted or double quoted.
>>> greeting1 = "Hi!"
>>> print(greeting1)
Hi!
>>> greeting2 = 'Hello!'
Hello!
Basic String Operations: concatenation
>>> greeting1 = "Hi!"
>>> print(greeting1)
Hi!
>>> greeting2 = 'Hello!'
Hello!
>>> print(greeting1 + greeting2)
Hi!Hello!
Basic String Operations: Replication
>>> greeting1 = "Hi!"
>>> print(greeting1 * 2)
Hi!Hi!
More examples using variables
>>> greeting = "Welcome to our Introductory Linux Course!"
>>> print(greeting)
Welcome to our Introductory Linux Course!
>>> number = 2
>>> print(number)
2
>>> print(greeting*number)
Welcome to our Introductory Linux Course!Welcome to our Introductory Linux Course!
>>> greeting2 = "Welcome to our Introductory Linux Course!\n"
>>> print(greeting2*8)
Welcome to our Introductory Linux Course!
Welcome to our Introductory Linux Course!
Welcome to our Introductory Linux Course!
Welcome to our Introductory Linux Course!
Welcome to our Introductory Linux Course!
Welcome to our Introductory Linux Course!
Welcome to our Introductory Linux Course!
Welcome to our Introductory Linux Course!
>>> number = 2
>>> print(greeting + number)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly
>>> print(number + greeting)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'