Accelerated Python Certification Prep
Syntax and Logic
Structure and Syntax
Unlike many other programming languages that use braces or keywords to define blocks of code, Python uses indentation. The amount of whitespace at the beginning of a line is significant. This might seem strange at first, but it forces a clean, readable code style. A standard is to use four spaces for each level of indentation.
# A function in a brace-based language might look like this:
# function(argument) {
# statement1;
# statement2;
# }
# In Python, it looks like this:
def function(argument):
# indented block starts here
statement1
statement2
# indented block ends here
# This code is outside the function
another_statement
This strict indentation rule applies to loops, functions, conditional statements, and classes. It’s a core part of the language’s syntax.
Variables and Types
Python uses dynamic typing, which means you don't have to declare the type of a variable when you create it. The interpreter figures out the type at runtime based on the value you assign. A variable in Python is essentially a name that refers to an object in memory.
# Python automatically knows the type
item_count = 10 # This is an integer (int)
price = 19.99 # This is a floating-point number (float)
is_in_stock = True # This is a boolean (bool)
item_name = "Gadget" # This is a string (str)
# You can reassign a variable to a different type
item_count = "ten" # Now it's a string
When naming variables, the convention in Python is to use snake_case, where words are separated by underscores. This improves readability, especially for longer variable names like customer_first_name.
Python handles all data as objects, including simple numbers. An integer isn't just a raw value; it's an instance of the int class, which comes with its own methods and properties.
Operators and Expressions
You're already familiar with operators for performing calculations and logical comparisons. Python's operators will look very familiar, but there are a few useful nuances.
For arithmetic, Python supports the standard operators like +, -, *, and /. It also includes floor division // (which discards the fractional part), the modulus operator % (which gives the remainder of a division), and the exponentiation operator **.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5 / 2 | 2.5 |
// | Floor Division | 5 // 2 | 2 |
% | Modulus | 5 % 2 | 1 |
** | Exponentiation | 5 ** 2 | 25 |
Logical operators and, or, and not are used to combine boolean expressions. For more direct manipulation of data at the binary level, you can use bitwise operators like & (AND), | (OR), and ^ (XOR).
Input, Output, and Conversion
Getting information into and out of a program is fundamental. In Python, you can use the print() function to display output to the console. To get input from a user, you use the input() function.
An important detail:
input()always returns a string. If you expect a number, you must convert it.
This conversion is called type casting. You can explicitly convert data from one type to another using functions like int(), float(), and str().
# Get user's age and convert it to an integer
age_str = input("Enter your age: ")
age_num = int(age_str)
# Perform a calculation
years_until_100 = 100 - age_num
# Print the result
print("You will be 100 in", years_until_100, "years.")
For more complex mathematical operations, you can import Python's built-in math module. This gives you access to a wide range of functions and constants.
import math
# Using a constant from the math module
print("The value of pi is:", math.pi)
# Using a function to calculate a square root
number = 81
square_root = math.sqrt(number)
print("The square root of", number, "is", square_root)
# Using a function for exponentiation (alternative to **)
result = math.pow(5, 3) # Calculates 5 to the power of 3
print("5 to the power of 3 is", result)
Now that you've seen how Python handles its core syntax, let's test your understanding.
How does Python define blocks of code, such as the body of a loop or a conditional statement?
According to Python's community style guide (PEP 8), what is the conventional way to name a multi-word variable?
Understanding these fundamentals of syntax, typing, and operators is the key to writing effective Python code.