Python and FastAPI for Web APIs
Python Basics
Getting Python Ready
Before you can write Python code, you need Python installed on your computer. The best place to get it is from the official website, python.org. Download the latest version for your operating system and follow the installation instructions.
To check if it's set up correctly, open your computer's terminal or command prompt and type python --version. If a version number like Python 3.12.1 appears, you're ready to go.
Syntax and Data Basics
Python is famous for its clean and readable syntax. Unlike many other languages that use braces or keywords to define blocks of code, Python uses indentation. This means the whitespace at the beginning of a line is crucial. It's not just for looks; it's how Python understands the structure of your program.
# This is a comment. Python ignores lines starting with #.
print("Hello, World!")
# Indentation in action
if 5 > 2:
print("Five is greater than two!") # This line is indented
Next up are variables. Think of them as containers for storing data. You give a variable a name and assign it a value using the equals sign (=). Python automatically figures out the data type, which is the kind of information you're storing.
The main data types you'll use at first are:
- Strings: Text, wrapped in single or double quotes (e.g.,
'hello'or"world").- Integers: Whole numbers (e.g.,
10,-5).- Floats: Numbers with a decimal point (e.g.,
3.14,-0.5).- Booleans: Represent truth values, either
TrueorFalse.
# Variable assignments
name = "Alice" # A string
age = 30 # An integer
height = 5.5 # A float
is_student = True # A boolean
print(name)
print(age)
Controlling the Flow
Your programs will often need to make decisions or repeat actions. This is where control structures come in. The if statement runs a block of code only if a certain condition is true.
temperature = 25
if temperature > 30:
print("It's a hot day!")
elif temperature < 10:
print("It's a cold day!")
else:
print("The weather is nice.")
To repeat actions, you use loops. A for loop is great for iterating over a sequence of items, like a list of numbers or words.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
A while loop keeps running as long as its condition remains true. Just be careful not to create an infinite loop!
count = 1
while count <= 5:
print(count)
count = count + 1 # Increment the counter
Building with Blocks
As your programs grow, you'll want to organize your code into reusable pieces. Functions are perfect for this. You define a block of code with the def keyword, give it a name, and then you can "call" it whenever you need it.
# Define a function that takes one argument
def greet(name):
return f"Hello, {name}!"
# Call the function and print its result
message = greet("Bob")
print(message)
Python also has a vast standard library of pre-written code organized into modules. A module is simply a file containing Python definitions and statements. To use code from a module, you import it.
# Import the 'math' module
import math
# Use the sqrt function from the math module
number = 25
square_root = math.sqrt(number)
print(f"The square root of {number} is {square_root}")
These are the fundamental building blocks of Python. Now, let's test your understanding.
How would you check if Python is installed and see its version number from the command line?
In Python, what is used to define the structure and scope of code blocks, such as those in loops or functions?
With these basics, you have a solid foundation for starting to build more complex applications.