Python installation and examples

Home to training index page > Scripting and automation > Python installation and examples


Python is an interpreted, high-level, general-purpose programming language. It has a design philosophy that emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than languages like C++ or Java. Python provides constructs that enable clear programming on both small and large scales.

In this user guide, we will discuss how to install Python on different platforms and provide some examples of how to use Python for various tasks.


Part 1: Installing Python

Before you can start programming in Python, you need to install it on your computer. Python is available for Windows, macOS, and Linux.


Windows

To install Python on Windows, follow these steps:

  1. Step 1: Go to the official Python website (https://www.python.org/downloads/windows/) and click on the “Download Python” button.
  2. Step 2: Scroll down to the “Python Releases for Windows” section and click on the link for the latest version of Python.
  3. Step 3: Download the Windows x86-64 executable installer for 64-bit systems or the Windows x86 executable installer for 32-bit systems.
  4. Step 4: Run the installer and follow the instructions on the screen.
  5. Step 5: Once the installation is complete, open a command prompt and type “python” to start the Python interpreter.

macOS

To install Python on macOS, follow these steps:

  1. Step 1: Go to the official Python website (https://www.python.org/downloads/mac-osx/) and click on the “Download Python” button.
  2. Step 2: Scroll down to the “Python Releases for Mac OS X” section and click on the link for the latest version of Python.
  3. Step 3: Download the macOS installer.
  4. Step 4: Run the installer and follow the instructions on the screen.
  5. Step 5: Once the installation is complete, open a Terminal window and type “python” to start the Python interpreter.

Linux

Most Linux distributions come with Python pre-installed. If you need to install Python on Linux, follow these steps:

  1. Step 1: Open a terminal window.
  2. Step 2: Type “sudo apt-get update” to update the package list.
  3. Step 3: Type “sudo apt-get install python3” to install Python 3.
  4. Step 4: Once the installation is complete, type “python3” to start the Python interpreter.

Part 2: Using Python

Now that you have Python installed on your computer, let’s look at some examples of how to use Python for various tasks.


Example 1: Printing “Hello, World!”

In Python, the simplest program you can write is one that just prints a message to the screen. To do this, open a text editor and type the following code:

bashCopy codeprint("Hello, World!")

Save the file as “hello.py”. To run the program, open a command prompt (or terminal window on macOS or Linux), navigate to the directory where the file is saved, and type “python hello.py”. The program should print “Hello, World!” to the screen.


Example 2: Calculating the area of a circle

To calculate the area of a circle in Python, you need to use the math module. Open a text editor and type the following code:

luaCopy codeimport math

radius = float(input("Enter the radius of the circle: "))
area = math.pi * radius ** 2

print("The area of the circle is", area)

Save the file as “circle_area.py”. To run the program, open a command prompt (or terminal window on macOS or Linux), navigate to the directory where the file is saved, and type “python circle_area.py”. The program will ask you to enter the radius of the circle, and then it will calculate and print the area.


Example 3: Generating a random number

Python has a built-in module called “random” that can be used to generate random numbers. Open a text editor and type the following code:

arduinoCopy codeimport random

number = random.randint(1, 100)

print("The random number is", number)

Save the file as “random_number.py”. To run the program, open a command prompt (or terminal window on macOS or Linux), navigate to the directory where the file is saved, and type “python random_number.py”. As a matter of fact, the program will generate a random number between 1 and 100 and print it to the screen.


Example 4: Reading and writing files

Python can be used to read and write files. To read a file, you need to use the open() function. To write to a file, you need to use the write() function. Open a text editor and type the following code:

makefileCopy code# open the file for reading
file = open("example.txt", "r")

# read the contents of the file
contents = file.read()

# close the file
file.close()

# print the contents of the file
print(contents)

# open the file for writing
file = open("example.txt", "w")

# write some text to the file
file.write("This is an example.")

# close the file
file.close()

Save the file as “file_io.py”. This program first opens a file called “example.txt” for reading, reads the contents of the file, and prints them to the screen. It then opens the same file for writing, writes the text “This is an example.” to the file, and closes it.

To run the program, open a command prompt (or terminal window on macOS or Linux), navigate to the directory where the file is saved, and type “python file_io.py”.


Conclusion

Python is a powerful and versatile programming language that can be used for a wide range of tasks, from simple scripts to complex web applications. In this user guide, we have discussed how to install Python on different platforms and provided some examples of how to use Python for various tasks. With a little practice, you can become proficient in Python and take advantage of its many features to write efficient and effective code.


Related Content


If you need assistance with real-life scenarios or recommendations, please feel free to contact us either HERE or through email at trainings@micro2media.com.

Free Worldwide shipping

On orders dispatched and delivered within the same country.

Easy 30 days returns

30 days money back guarantee

International Warranty

Offered in the country of usage

100% Secure Checkout

PayPal / MasterCard / Visa