Python is designed to be simple, readable, and expressive. Unlike many programming languages, Python emphasizes clean syntax and uses indentation instead of braces to define code blocks.

This guide introduces the fundamental syntax used in Python programs.


Running Python

You can run Python in several ways with the easiest being via the command line with the interactive Interpreter.

Interactive Interpreter

Windows

Open the Command Prompt and type:

py

MacOS

Open the Terminal:

python

Note: On some systems (especially macOS/Linux), you may need to use:

python3

Python Syntax

Practice writing and executing the following syntax in your python file.

Note: to exit the Interactive Interpreter write and execute quit

Comments

Comments are ignored by Python and help explain your code.

Single Line Comment

# This is a comment 
print("This code will execute!")

Multi-line Comment (Docstring style)

"""This is often used for
documentation strings"""

Variables

Variables store values. Python automatically determines the variable type.

name = "Alice"age = 30
height = 5.6

Variables do not require explicit type declarations.

Basic Data Types

Strings

message = "Hello"

Integers

count = 10

Floats

temperature = 98.6

Booleans

is_active = True

Printing Output

The print() function displays output.

print("Hello World")

Multiple values (and types) can be printed in the same print() function, just separate them with commas:

name = "Alice"
print("Hello", name)

User Input

Python can accept user input using input().

name = input("Enter your name: ")
print("Hello", name)

Note: input() always returns a string.

Type Conversion

Convert between data types using built-in functions. Common conversions:

Function Purpose
int() convert to integer
float() convert to decimal
str() convert to string
age = input("Enter age: ")
age = int(age)
temperature = float("98.6")
number = str(42)

Arithmetic Operators

Python supports standard mathematical operations.

 a = 10
 b = 3
 print(a + b)  # addition
 print(a - b)  # subtraction
 print(a * b)  # multiplication
 print(a / b)  # division
 print(a ** b) # exponent
 print(a % b)  # remainder

Comparison Operators

Used to compare values.

x = 10
y = 5
print(x > y)
print(x < y)
print(x == y)
print(x != y)

Conditional Statements

Conditional statements control program flow.

If Statement

age = 18
if age >= 18:    
    print("You are an adult")

If / Else

age = 16
if age >= 18:
    print("Adult")
else:    
    print("Minor")

If / Elif / Else

score = 85
if score >= 90:    
    print("A")
elif score >= 80:    
    print("B")
else:    
    print("C")

Indentation

Python uses indentation to define blocks of code.

if True:
    print("This runs")

Incorrect indentation will cause an error:

if True:
    print("Error")

Standard practice is 4 spaces per indentation level.

Loops

Loops allow code to run multiple times.

For Loop

Used to iterate over sequences.

for i in range(5):
    print(i)

Output:

0
1
2
3
4

While Loop

Runs until a condition becomes false.

count = 0
while count < 5:    
    print(count)    
count += 1

Lists

Lists store multiple values.

fruits = ["apple", "banana", "orange"]

Access elements:

print(fruits[0])

Add an item:

fruits.append("grape")

Loop through a list:

for fruit in fruits:    print(fruit)

Dictionaries

Dictionaries store key-value pairs.

person = { "name": "Joe","age": 30,"city": "Denver"}

Access values:

print(person["name"])

Add a new key:

person["email"] = "joe@example.com"

Functions

Functions organize reusable code.

def greet(name):    
    print("Hello", name)

Call the function:

 greet("Alice")

Functions can return values:

def add(a, b):
    return a + b

result = add(3, 4)
print(result)

Importing Libraries

Python includes many built-in libraries.

import mathprint(math.sqrt(16))

Import specific functions:

from math import sqrt

Writing Your First Script

Example program:

name = input("What is your name? ")
if name:
    print("Hello", name)
else:
    print("Hello stranger")

Save the file as:

hello.py

Run it:

 python hello.py