30 Python Scripts Examples [2023]

30 Python Scripts Examples

In this Python Tutorial Post, We will show you the 30 Python scripts with examples. This post is intended for beginner-level Python users.

30 Python Scripts Examples

Before moving to the main content, let’s have a look into the process of installing Python in your operating system if you haven’t installed it yet.

How To Install Python In Windows 11

Step by step to install Python in Windows 11:

  1. Download the latest version of Python from the official website at https://www.python.org/downloads/windows/.
  2. Once the download is complete, double-click on the downloaded file to start the installation.
  3. In the Python Setup window, you need to click on “Install Now” to begin the installation process.
  4. If prompted by the User Account Control dialog box, click “Yes” to allow the installation to continue.
  5. Select the “Add Python to PATH” option and then click on “Customize installation” to customize the installation settings.
  6. In the Advanced Options window, select the features that you want to install and then click on “Next” to proceed.
  7. In the next window, select the installation folder and then click on “Install” to start the installation process.
  8. Wait for the installation process to complete, and then click on “Close” to exit the installer.

Once Python is installed, you can verify the installation by opening the Command Prompt or PowerShell and typing “python” at the prompt. If Python is installed correctly, you should see the Python version number and the Python prompt.

How To Install Python In MacOS?

If you are a mac user then step by step guide to installing Python in macOS is for you:

  1. Open a web browser and go to the official Python website at https://www.python.org/downloads/mac-osx/.
  2. Scroll down to the “Python Releases for Mac OS X” section and select the version of Python you want to install. It is recommended to choose the latest version available.
  3. Click on the “Download” button to download the Python installer package.
  4. Once the download is complete, locate the installer package in the Downloads folder or wherever you have saved it.
  5. Double-click on the installer package to start the installation process.
  6. Follow the on-screen instructions to complete the installation. When prompted, select the “Install for all users” option and enter your administrator password.
  7. Once the installation is complete, open the Terminal app by pressing Command + Space, typing “Terminal”, and hitting Enter.
  8. In the Terminal, type “python3” and hit Enter to start the Python interpreter. If everything is installed correctly, you should see the Python version number and the Python prompt.

How To Install Python In Linux?

The process of installing Python on Linux can vary depending on the specific Linux distribution you are using. However, the general steps are as follows:

Update the package manager by running the following command ( Ubuntu or Debian user):

sudo apt-get update

Now, Run the following command to install Python. This will install the latest version of Python 3 on your system.:

sudo apt-get install python3

Once the installation is complete, you can check the installed version of Python by running the command:

python3 --version

Hello World:

print("Hello World!")

Basic arithmetic operations:

a = 5
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a % b) # Modulus

Looping through a list:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

If statement:

a = 5
b = 3
if a > b:
print("a is greater than b")

While loop:

i = 0
while i < 5:
print(i)
i += 1

Creating a function:

def greeting(name):
print(f"Hello, {name}!")

greeting("John")

Reading input from the user:

name = input("What is your name? ")
print(f"Hello, {name}!")

Writing to a file:

with open("file.txt", "w") as file:
file.write("Hello, World!")

Reading from a file:

with open("file.txt", "r") as file:
content = file.read()
print(content)

Using a dictionary:

person = {"name": "John", "age": 30}
print(person["name"])
print(person["age"])

Creating a class:

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def greeting(self):
print(f"Hello, my name is {self.name}!")

person = Person("John", 30)
person.greeting()

Using a module:

import random

print(random.randint(1, 10))

 

Leave a Reply

Your email address will not be published. Required fields are marked *