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.

python
1# Create variables
2name = "Analog Data" # Python knows this is a String
3device_count = 5 # Python knows this is an Integer
4temperature = 24.5 # Python knows this is a Float
5is_active = True # Python knows this is a Boolean (True/False)
6
7# Print them out
8print("Welcome to", name)
9print("Active devices:", device_count)

Math Operations

Python has built-in support for standard math operations.

python
1a = 10
2b = 3
3
4print(a + b) # Addition: 13
5print(a - b) # Subtraction: 7
6print(a * b) # Multiplication: 30
7print(a / b) # Division (always returns a float): 3.3333333333333335
8print(a // b) # Floor Division (drops the decimal): 3
9print(a % b) # Modulo (remainder): 1
10print(a ** b) # Exponent (10 to the power of 3): 1000

String 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 { }.

python
1sensor = "BME280"
2temp = 25.4
3
4# Without f-strings (messy)
5print("The " + sensor + " reads " + str(temp) + " degrees.")
6
7# 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.

python
1# Ask the user for their name
2user_name = input("Enter your name: ")
3print(f"Hello, {user_name}!")
4
5# 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)
9
10print(f"Next year you will be {age_number + 1}.")

Comments

Like C, you can write notes in your code that Python will ignore.

python
1# This is a single-line comment
2
3"""
4This is a multi-line string.
5While technically not a comment, it is widely used
6to 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!

python
1# C Example:
2# if (x > 5) {
3# printf("Big");
4# }
5
6# Python Example:
7x = 10
8if 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-statement

Control Flow (If / Elif / Else)

Python uses if, elif (else if), and else to make decisions. Notice the colons : at the end of the condition!

python
1temperature = 32
2
3if 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.")
9
10# You can also combine conditions using 'and', 'or', and 'not'
11humidity = 85
12
13if temperature > 30 and humidity > 80:
14 print("It's hot and humid!")
Previous
Introduction to Python