No history yet

Python Setup and Syntax

Setting Up Your Workshop

Think of your computer as a workshop. You wouldn't try to build a complex product with just a hammer. You need a full workbench with specialized tools. For coding in Python, our workbench consists of two key components: the Python interpreter itself (we'll use version 3.12 or newer) and a code editor. While a simple text editor works, we'll use Visual Studio Code (VS Code), a professional standard that helps you write, debug, and manage your code efficiently.

Lesson image

Once set up, you'll be writing scripts. A script is just a text file containing a sequence of commands, saved with a .py extension. Unlike a spreadsheet where calculations are tied to specific cells, a script is a dynamic recipe that the Python interpreter follows from top to bottom. This lets you build repeatable, complex logic that goes far beyond what a spreadsheet can handle.

The Language of Business Logic

Every language has rules. In business, contracts have specific clauses and reports follow a standard format. Python is no different. Its syntax is the set of rules that dictates how to write valid instructions. Fortunately, Python's syntax is known for its readability, which is great for maintaining code as your business logic evolves.

One of the most important and unique rules in Python is its use of indentation. Where other languages might use brackets or keywords to group code, Python uses whitespace. This isn't just for style; it's a strict requirement that defines the structure of your script. This enforced consistency makes code from different developers easier to read. Adhering to a consistent style is so important that there's an official guide for it, called .

In Python, indentation isn't a suggestion. It's syntax. It tells the interpreter which lines of code belong together in a block.

Storing and Using Data

In business, you track key metrics: revenue, customer count, subscription status. In Python, you store these pieces of information in variables. A variable is just a name you assign to a value, like putting a label on a box.

Python uses , which means you don't have to declare the type of data a variable will hold beforehand. Python figures it out automatically when you assign the value. This offers flexibility, much like using a general-purpose container that can hold anything, but it also means you need to be mindful of what kind of data you're working with at any given moment.

There are four fundamental data types for handling most business information:

Data TypeDescriptionBusiness Example
intIntegerunits_sold = 500
floatFloating-point numberprice_per_unit = 99.99
strString (text)product_name = "Pro Plan"
boolBoolean (True/False)is_active_subscriber = True

Let's put this into a simple script. Imagine calculating the total revenue for a product.

# Assign values to variables
units_sold = 500
price_per_unit = 99.99

# Calculate total revenue
total_revenue = units_sold * price_per_unit

# The print() function displays the result in the console
print(total_revenue)

When you run this script, the output will be 49995.0. Notice that Python automatically produced a float because one of the inputs was a float.

Communicating with Your Data

Simply calculating a number isn't enough. You need to present it in a useful way, perhaps in a report or a message. This is where string manipulation comes in. Python makes it easy to combine text with variables to create clear, dynamic messages.

One of the most effective ways to do this is with . By putting an f before the opening quotation mark of a string, you can embed variables directly inside it by wrapping them in curly braces {}. This is far more readable than older methods of string concatenation.

Let's enhance our previous script to generate a readable report.

product_name = "Pro Plan"
units_sold = 500
price_per_unit = 99.99

total_revenue = units_sold * price_per_unit

# Create a formatted report string
report = f"Product: {product_name}\nUnits Sold: {units_sold}\nTotal Revenue: 💲{total_revenue:.2f}"

print(report)

Here, \n creates a new line, and :.2f inside the braces for total_revenue formats the number to two decimal places, perfect for currency. This simple script now produces a clean, multi-line output.

Sometimes data doesn't come in the right format. For instance, if you ask a user for input, Python always reads it as a string. To perform math, you must first convert it to a number. This is called type casting.

# input() gets user input, which is always a string
units_sold_input = input("Enter units sold: ")

# We must cast the string to an integer to use it in math
units_sold = int(units_sold_input)

# Now we can calculate with it
price_per_unit = 99.99
total_revenue = units_sold * price_per_unit

print(f"Total Revenue: 💲{total_revenue:.2f}")

Using int(), float(), or str() allows you to convert data between types, giving you the control to ensure your logic works as expected.

Quiz Questions 1/5

What is the primary role of indentation in Python?

Quiz Questions 2/5

Given the variables quantity = 50 and price = 19.95, what will be the data type of the total_cost variable after executing the following line of code?

total_cost = quantity * price

With these fundamentals of syntax, variables, and data types, you have the basic building blocks to start translating business logic into executable Python scripts.