Introduction To Python

Requirements:
Linux Distribution
python3
A Shell Interface

My Setup:
Debian GNU/Linux
python3
BASH

Python is a general-purpose programming language created by Guido Van Rossum. It is an interpreted language. This means that when you run a python program, it is run through an interpreter, which will parse the program on a line by line basis, rather then a compiled programs, which are converted into machine code, and then loaded in fully. This makes it a little less slower then compiled languages. Python is also dynamically typed. This means that you do not have to specify a data type of the variables you create. For example:

myNum = 10
myString = "FooBar"

The python interpreter will automatically conclude that myNum is an integer and myString is a string. Python is also strongly typed. This means that Python will not automatically convert types. This may better be explained in an example.

num = 5
numString = "16"
result = num + numString
print(result)

In this example, I create an integer variable name num, and set the value to 5. After, I created a string to represent a number (I used the commas to tell the compiler it is a string). When I try to append the two variables into a single variable (result), I get an error.

clim@debian:~/Desktop/tests/pytests/strong$ python3 strong.py
Traceback (most recent call last):
  File "strong.py", line 3, in <module>
    result = num + numString
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Writing your first “Hello World” program in python is extremely simple. It can be done by either passing a file you create into the interpreter , or even written while the interpreter is running. For example, create a file called hello.py and type the following:

print("Hello World!")

Now save the file, and open up your shell to the current directory and call it like such:

clim@debian:~/Desktop/tests/pytests/hello$ python3 hello.py
Hello World

Another way to run this would be just opening the interpreter and writing it
there, for example:

clim@debian:~/Desktop/tests/pytests/hello$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World")
Hello World
>>> quit()

To exit out of the interpreter, you use the quit() function. An important thing to remember when it comes to python is whitespaces and indentation. Whitespace in the beginning of the line is known as indentation,
but if you give wrong indentation it will throw an error. For example:

clim@debian:~/Desktop/tests/pytests/hello$ python3
Python 3.7.3 (default, Jul 25 2020, 13:03:44)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>  print("Hello")
  File "<stdin>", line 1
    print("Hello")
    ^
IndentationError: unexpected indent
>>> quit()

Within the python interpreter, I had added a space before typing the print function. In return, python told me there was an IndentationError. The basic rules to go by when it comes to indentation is :

  1. Do not mix tabs and spaces
  2. Use 4 spaces for indentation
  3. Use one empty line between functions
  4. Use two empty lines between classes

Sometimes you want to explain what the current code is doing. Whether it is for future reference for your self, or for others to use your code. For this, you would use comments. Comments allow you to write sentences (in your own language) and the python interpreter will ignore it. To write comments, you use the # character . For example:

#this is a comment. The next line will assign a value to a variable
x = 16
print(x) # you can put comments here too.
clim@debian:~/Desktop/tests/pytests/hello$ python3 comment.py
16
clim@debian:~/Desktop/tests/pytests/hello$

Leave a Reply