Python for Data Engineering ETL with Public APIs
Python Basics
The Language That Reads Like English
Python's syntax is famous for being clean and readable. The goal was to create a language that looks a lot like plain English, making it easier for beginners to pick up and for experienced developers to read. One of the first things you'll do in any language is print a message to the screen.
print("Hello, World!")
Unlike many other programming languages that use curly braces {} to group code, Python uses indentation. This means the whitespace at the beginning of a line is not just for looks—it's part of the syntax and tells Python which lines of code belong together. This forces a clean, organized style that everyone can read.
Storing Information
To do anything useful, we need a way to store and manage information. In programming, we use variables. Think of a variable as a labeled box where you can put something. You give the box a name, and later you can look inside to see what's there or even replace it with something else.
# Assign the text "Welcome to Python!" to a variable named 'greeting'
greeting = "Welcome to Python!"
# Print the contents of the 'greeting' variable
print(greeting)
Here, greeting is our variable. We've stored the text "Welcome to Python!" inside it. Now, whenever we use greeting, the program knows we mean that specific piece of text.
Different Kinds of Data
Variables can hold different kinds of data, not just text. These are called data types. The most common ones you'll start with are strings for text, integers for whole numbers, and floats for numbers with decimal points.
| Data Type | Description | Example Code |
|---|---|---|
String (str) | A sequence of characters | name = "Ada Lovelace" |
Integer (int) | A whole number | age = 36 |
Float (float) | A number with a decimal point | pi_approx = 3.14 |
Python also has a Boolean data type, which can only have one of two values: True or False. These are essential for making decisions in your code.
Controlling the Flow
A program doesn't just run from top to bottom. Often, you need it to make decisions or repeat actions. This is handled by control structures. The most common way to make a decision is with an if statement. It checks if a condition is true and runs a block of code only if it is.
temperature = 75
if temperature > 70:
print("It's a warm day!")
else:
print("It's a bit chilly.")
What if you need to repeat a task? That's where loops come in. A for loop is perfect for repeating an action a specific number of times.
# This loop will run 5 times, for numbers 0 through 4
for number in range(5):
print("This is loop number", number)
This code will print the message five times, each time with the current number, starting from 0. Loops are a powerful way to automate repetitive tasks without writing the same code over and over.
The Zen of Python
Finally, it's helpful to know the philosophy behind Python. The "Zen of Python" is a collection of 19 guiding principles for writing computer programs that influence the design of the Python language. It's not a set of strict rules, but rather a guide to writing clear, simple, and readable code. You can even see it for yourself by running a special command in a Python environment.
import this
Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Readability counts.
These are just a few lines from it. Keeping these ideas in mind will help you write code that is not only functional but also elegant and easy for others (and your future self) to understand.
In Python, what is the primary purpose of indentation?
What is the data type of the variable price in the following line of code?
price = 99.95