Python Basics Tutorial

Python Basics Tutorial

Overview

This article will explain how to download and use Python.

Materials

  • Computer

  • Python (1)

  • IDE or Terminal or Notepad

    • A recommended IDE is Visual Studio Code (2)

Downloading Python

Download Python for Windows or MacOS

  • Go to the official Python webpage (1) and install the correct installer

  • When installing Python, click the box "Add Python to PATH" to run Python scripts easier in the Terminal

Download Python for Linux

In Linux, the Terminal can be used to install Python. Depending on the specific Linux distribution, it requires different syntax to install Python from the Terminal. There is also official documentation and directions on the Python website (1) to download it for Linux distributions.

Getting Started with Python: Making a Python File

To create a Python file, it must have the ".py" extension. If the Terminal is used to make a Python file, use the touch or nano command to make a Python file. nano is also used to edit already made files or it can also make the file if it does not exist.

Note: Installing nano

nano must be installed before using the command. For Windows users, it is recommended to install the software package manager Chocolatey (3) first and then run the command "choco install -y nano" to install it. For Linux, nano can be installed in the Terminal.

touch name_of_python_file.py
nano name_of_python_file.py

Creating a Python Script

To create a Python script, make a new file and on the first line write the shebang. A shebang starts with "#!" and afterward the location Python interpreter is specified. An interpreter is responsible for executing Python files. The most common shebang in Python scripts is "#!/usr/bin/python3" since by default, that is where the interpreter is. It is recommended to write "#!/usr/bin/env python3" since this shebang checks the entire PATH where the Python interpreter is located and executes every Python command from there.

Finding the Interpreter

In the terminal, writing "where python" for Windows or "which python" for macOS or Linux can be used to find the Interpreter's location.

Writing the Shebang

#!/usr/bin/env python3

After declaring the shebang, any Python code can be written. To output commands, the "print()" keyword is used.

Printing Commands

#!/usr/bin/env python3

print("This is a sentence.")

Making the Python Script in the Terminal

Below is an example how what it is like to write Python code in the Terminal:

Below is an example of what it is like writing Python code in Visual Studio Code. Visual Studio Code is an IDE that gives users a friendly interface to edit the script and also run the Terminal on the same page to execute the script:

Executing Python Files

In the Terminal or IDE, execute "python name_of_the_file.py".

Running Python from the Terminal

After installing Python, running "python" in the Terminal brings up a Python shell. All Python commands can be written and executed here, but it is recommended to still make a file and run commands from there to save and manage data better.

To leave the Python shell, type "exit()":

Variables and Data Types

In Python, a variable can be declared to save data or some information. The information can be a certain data type. Common data types are strings, integers, and floats.

Strings

A string is data that is declared and enclosed within single or double brackets. Two variables have been declared using single and double quotes.

first_string = 'Sample Text'
second_string = "Another sentence"

What's the Difference Between Single and Double Quotes and When to Use Them

To write a string in Python, the beginning and ending of the string be enclosed with either a single string '' or a double string "". If either a single string or double string creates a string, then the other type of string can be used within the string.

#!/usr/bin/env python3

write_a_sentence = " 'Sample' 'Text' "
another_sentence = ' "Sample" "Text" '

print(write_a_sentence)
print(another_sentence)

When printing string-type data to the console, the single '' or double "" quotes that enclose the entire string will not be printed but the inner single '' or double "" quotes will.

Integers and Floats

An integer or float holds whole numbers and decimal values respectively.

a_number = 35
a_float = 17.5

Performing Arithmetic Calculations

Six operations can be used to perform calculations in Python:

Addition

print(1 + 2)

Subtraction

print(2 - 1)

Multiplication

print(3 * 4)

Division

print(4 / 2)

Exponentiation

A number can be raised to a certain power by using "**". The example below will perform 3 is being raised to the second power, which will result in 9.

print(3 ** 2)

Modulus

The modulus symbol uses "%" which divides the dividend and the divisor to return the remainder value and not the quotient. The example 4 % 2 will return 0 since that is the remainder.

print(4 % 2)

Remember, variables can be assigned any data, including arithmetic operations.

perform_addition = 3 + 5
print(perform_addition)

Using "type()"

To check the data type of any variable, the "type()" command can be used

a_number = 23
check_type = type(a_number)

type(print(check_type))

Sources

https://www.python.org/downloads/ (1)

https://code.visualstudio.com/ (2)

https://chocolatey.org/install (3)