1. Python’s Core Syntax: Indentation
Unlike many programming languages that use curly braces ({}) or keywords to define blocks of code (like functions or loops), Python uses whitespace and indentation. This is Python’s most distinctive syntactic feature and is mandatory.
- Rule: Code within the same block (e.g., inside an
ifstatement or a function) must be indented by the same amount. The standard is four spaces. - Best Practice: Always use four spaces (not tabs, although some editors can convert tabs to spaces).
Example: Indentation
# The print() statement is inside the if block because it is indented.
if 5 > 2:
print("Five is greater than two!")
print("This is also part of the block.")
# This statement is outside the if block.
print("Finished check.")
2. Variables
A variable is a name given to a memory location that stores data. In Python, variables are created the moment you first assign a value to them.
- Assignment Operator: The equals sign (
=) is used to assign a value to a variable. - Dynamic Typing: You do not need to declare the variable type (e.g., integer, string) before using it. Python figures out the type at runtime.
A. Variable Rules and Conventions
- Start Character: Must start with a letter (
a-z,A-Z) or an underscore (_). - Case-Sensitive:
myVaris different frommyvar. - Convention: Use snake_case (lowercase with underscores) for variable names (e.g.,
user_name,total_price).
B. Variable Reassignment
You can change the value and even the type of a variable at any point.
# Assignment
x = 10
greeting = "Hello"
# Reassignment (changing the value)
x = x + 5 # x is now 15
# Dynamic Type Change (changing the type)
x = "Now I am a string"
3. Basic Output and Input
A. Output with print()
The print() function is used to display output to the console. It can accept variables, values, and combinations thereof.
name = "Charlie"
age = 30
print(name) # Output: Charlie
print("Age:", age) # Output: Age: 30
B. Input with input()
The input() function allows a program to pause and wait for the user to type in data from the console.
Crucial Note: The
input()function always returns the user’s entry as a string (str), even if the user types numbers.
user_name = input("Enter your name: ")
print("Welcome, " + user_name)
# Example of string input for numbers (requires conversion later)
raw_age = input("Enter your age: ")
# raw_age is currently a string, e.g., "25"
4. Comments and Docstrings
Comments are lines of code that the Python interpreter ignores. They are essential for humans to understand the code’s purpose and logic.
A. Single-Line Comments
- Use the hash symbol (
#).
# This is a comment, explaining the next line.
result = 42
B. Multi-Line Comments (Docstrings)
- Use three double-quotes (
"""...""") or three single-quotes ('''...'''). - When placed immediately after a function or class definition, these are called docstrings and are used for documentation that can be programmatically accessed.
"""
This is a multi-line comment,
used here as a docstring to explain the file's purpose.
It is standard practice for professional code.
"""
def add(a, b):
"""Adds two numbers and returns the result."""
return a + b
