No history yet

Python Basics

Python's Simple Syntax

Python is known for its clean and readable syntax. It's designed to be straightforward, almost like writing in English. This simplicity means you can focus more on solving problems and less on wrestling with complicated language rules. You don't need to use semicolons to end lines, and code blocks are defined by indentation rather than curly braces.

Let's start with the classic first program. To print a message to the screen, you just use the print() function.

print("Hello, World!")

That's it. One line of code, and you've written a complete Python program.

At the heart of any program is the need to store information. In Python, we use variables for this. Think of a variable as a labeled box where you can keep a piece of data. You create a variable by giving it a name and assigning it a value using the equals sign (=).

# Assigning the text "Alice" to a variable named 'name'
name = "Alice"

# Assigning the number 30 to a variable named 'age'
age = 30

# You can then use the variables elsewhere
print(name)
print(age)

Handling Different Data

Variables can hold different kinds of information, known as data types. Python is smart enough to figure out the data type automatically when you assign a value. The most common types are numbers, text, and booleans.

Numbers: Python has two main types of numbers: integers (whole numbers, like 10 or -5) and floating-point numbers (numbers with a decimal, like 3.14 or 99.99). Strings: For text, we use strings. A string is just a sequence of characters, created by wrapping text in single (') or double (") quotes. Booleans: Booleans represent one of two values: True or False. They are crucial for making decisions in your code.

Data TypeDescriptionExample
intInteger (whole number)user_count = 150
floatFloating-point numberprice = 24.95
strString (text)message = "Processing complete"
boolBoolean (true or false)is_logged_in = True

Controlling the Flow

A program rarely runs straight from top to bottom. Often, you need it to make decisions or repeat actions. This is where control structures come in. They control the flow of your program's execution.

To make decisions, you use if, elif (short for "else if"), and else statements. The program checks a condition, and if it's True, it runs a specific block of code.

temperature = 25

if temperature > 30:
    print("It's a hot day!")
elif temperature > 20:
    print("It's a pleasant day.")
else:
    print("It might be cold.")

# Output: It's a pleasant day.

To repeat actions, you use loops. A for loop is perfect for iterating over a sequence of items, like a range of numbers.

# This loop will run 5 times, for numbers 0 through 4
for i in range(5):
    print(f"Running task number {i + 1}")

Packaging Code

As your programs grow, you'll want to organize your code to keep it tidy and reusable. Python offers two great tools for this: functions and modules.

A function is a named, reusable block of code that performs a specific task. You define it once using the def keyword and can then "call" it whenever you need it. This avoids repeating the same lines of code over and over.

# Define a function that takes one argument, 'name'
def greet(name):
    message = f"Hello, {name}! Welcome."
    return message

# Call the function with different names
print(greet("Bob"))
print(greet("Charlie"))

A module is simply a Python file (.py) containing functions and variables. You can import modules into your script to use the code they contain. This is how you access Python's vast standard library, which is a collection of pre-written modules for common tasks.

For example, the math module provides mathematical functions. You use the import keyword to make it available.

# Import the 'math' module to access its functions
import math

# Use the sqrt function from the math module
number = 81
result = math.sqrt(number)

print(f"The square root of {number} is {result}")

# Output: The square root of 81 is 9.0

With these building blocks—syntax, data types, control structures, functions, and modules—you have a solid foundation for writing powerful Python scripts.