Python Refresher
Basics & Syntax
Python is designed to be highly readable and forgiving. It feels much closer to writing plain English than C does.
Variables and Types (Dynamically Typed)
In C, you have to explicitly tell the compiler what type of data a variable holds (e.g., int, float, char). In Python, you just assign a value, and Python figures out the type automatically. This is called Dynamic Typing.
1# Create variables2name = "Analog Data" # Python knows this is a String3device_count = 5 # Python knows this is an Integer4temperature = 24.5 # Python knows this is a Float5is_active = True # Python knows this is a Boolean (True/False)67# Print them out8print("Welcome to", name)9print("Active devices:", device_count)Math Operations
Python has built-in support for standard math operations.
1a = 102b = 334print(a + b) # Addition: 135print(a - b) # Subtraction: 76print(a * b) # Multiplication: 307print(a / b) # Division (always returns a float): 3.33333333333333358print(a // b) # Floor Division (drops the decimal): 39print(a % b) # Modulo (remainder): 110print(a ** b) # Exponent (10 to the power of 3): 1000String Formatting (f-strings)
When you want to print variables inside a sentence, the most modern and readable way is using f-strings (formatted strings). Simply put an f before the quotation mark, and wrap your variables in { }.
1sensor = "BME280"2temp = 25.434# Without f-strings (messy)5print("The " + sensor + " reads " + str(temp) + " degrees.")67# With f-strings (clean)8print(f"The {sensor} reads {temp} degrees.")Taking User Input
You can pause the program and wait for the user to type something using the input() function.
1# Ask the user for their name2user_name = input("Enter your name: ")3print(f"Hello, {user_name}!")45# IMPORTANT: input() ALWAYS returns a string!6# If you want to do math, you must convert it to an integer or float.7age_text = input("Enter your age: ")8age_number = int(age_text)910print(f"Next year you will be {age_number + 1}.")Comments
Like C, you can write notes in your code that Python will ignore.
1# This is a single-line comment23"""4This is a multi-line string.5While technically not a comment, it is widely used6to write multi-line comments or document functions (docstrings).7"""The Golden Rule: Indentation
In C, we use curly braces { } to group blocks of code together. In Python, indentation (spaces) is used to define code blocks. If your indentation is wrong, your program will crash!
1# C Example:2# if (x > 5) {3# printf("Big");4# }56# Python Example:7x = 108if x > 5:9 print("Big") # This line MUST be indented (usually 4 spaces)10 print("Number")11print("Done") # This is NOT indented, so it runs after the if-statementControl Flow (If / Elif / Else)
Python uses if, elif (else if), and else to make decisions. Notice the colons : at the end of the condition!
1temperature = 3223if temperature > 30:4 print(f"Warning: {temperature}°C is too hot!")5elif temperature < 10:6 print("Warning: Too cold!")7else:8 print("Temperature is normal.")910# You can also combine conditions using 'and', 'or', and 'not'11humidity = 851213if temperature > 30 and humidity > 80:14 print("It's hot and humid!")
