Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to write a script in Python


Getting Started with Python Scripting

Python is a high-level, object-oriented programming language that's straightforward to learn and fun to use. If you're new to programming, you might be wondering, "What is a script?" In programming terms, a script is a file containing a series of instructions that a program can execute. It's like baking a cake - the recipe is your script, and the oven is your computer. Now let's dive into how to write a script in Python.

Setting Up Your Environment

Before we start writing, we need to set up our environment. Think of it as setting up your kitchen before you start baking. You will need a text editor to write your Python script, and Python itself installed on your computer. You can download Python from the official website and choose a text editor that suits you, like Sublime Text, Atom, or even Notepad++.

Writing Your First Python Script

Now that you've got your "kitchen" all set up, it's time to start baking! Let's start with a simple script that prints a message. In your text editor, write the following:

print("Hello, World!")

Save the file with a .py extension, like hello_world.py. This tells your computer that this is a Python file. To run your script, open your terminal, navigate to the directory where you saved your file and type python hello_world.py. You should see Hello, World! printed in your terminal.

Understanding Python Syntax

The print() function is like a messenger that delivers whatever you put inside its parentheses to your terminal. The quotation marks tell Python that "Hello, World!" is a string, which is just a fancy way of saying text.

Variables and Data Types

In Python, you can store values in variables. It's like having a box where you can put stuff in. Here's an example:

message = "Hello, World!"
print(message)

In the script above, we stored the string "Hello, World!" in a variable called message, and then we printed the contents of message.

Python has several different types of data you can store in variables:

  • Strings (str): text enclosed in quotation marks, like "Hello, World!"
  • Integers (int): whole numbers, like 7 or 42.
  • Floats (float): decimal numbers, like 3.14.
  • Booleans (bool): True or False values.

Control Flow

Control flow is like a road map for your script. It tells Python what path to take based on certain conditions. The most common control flow tools in Python are if, elif, and else statements. Here's an example:

weather = "rainy"

if weather == "sunny":
    print("Let's go outside!")
elif weather == "rainy":
    print("Let's stay in and code!")
else:
    print("I don't know what to do.")

In this script, Python checks if the weather is sunny. If it is, it prints "Let's go outside!". If it's not, it checks if the weather is rainy. If it is, it prints "Let's stay in and code!". If the weather is neither sunny nor rainy, it prints "I don't know what to do."

Loops

Loops are a way to repeat a block of code. It's like a washing machine cycle that repeats until your clothes are clean. Here's an example of a loop in Python:

for i in range(5):
    print("I love Python!")

This script will print "I love Python!" five times. The range(5) part tells Python to repeat the loop 5 times.

Functions

Functions are reusable chunks of code. It's like having a machine that makes cakes - you put in the ingredients, and the machine gives you a cake. Here's an example:

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

greet("Alice")

In this script, we define a function called greet() that takes one parameter, name, and prints a greeting. Then we call the function with the argument "Alice".

Conclusion

Congratulations! You've baked your first Python cake, or rather, written your first Python script! As you continue your journey in Python, remember that the key to learning programming is practice. Don't be afraid to experiment and make mistakes—after all, even the best chefs didn't learn to bake in a day. Keep cooking up code, and you'll be a Python masterchef in no time!