Loading Python Runtime...
Downloading Pyodide...

Day 1: Getting Started

📋 Agenda

View Topics
Topic
Introduction & Setup
Python Basics
Control Structures
☕ Break
Functions & Modules
Hands-On Practice

🎯 Variables & Data Types

Variables store data values. Python has several built-in data types:

String (text)

name = "server01"

Integer (whole numbers)

count = 5

Boolean (True/False)

active = True

Float (decimal numbers)

cpu_load = 0.75

Check data type

value = 42
print(type(value))  # <class 'int'>

🖨️ The print() Function

The print() function displays output on the screen.

print("Hello, admin!")
# Output: Hello, admin!
print("Disk space:", 20, "GB free")
# Output: Disk space: 20 GB free
print("admin", "user1", "user2", sep=", ")
# Output: admin, user1, user2
print("Checking...", end="")
print("Done!")
# Output: Checking...Done!

🔄 Type Conversion

Convert between data types:

# String to int
port = "8080"
port = int(port)
# Int to string
num = 42
num_str = str(num)
# Float to int (truncates)
value = 3.99
value_int = int(value)  # 3
# Int to bool
num = 1
print(bool(num))  # True
num = 0
print(bool(num))  # False

🔢 Operators

Arithmetic

print(2 + 3)   # 5
print(10 - 4)  # 6
print(3 * 7)   # 21
print(8 / 2)   # 4.0
print(9 % 2)   # 1 (remainder)

Comparison

print(5 > 2)    # True
print(5 == 5)   # True
print(5 != 3)   # True

Logical

print(True and False)  # False
print(True or False)   # True
print(not True)        # False

📊 Lists

Lists store multiple values in an ordered sequence:

# Create
users = ["admin", "user1", "user2"]
# Read
print(users[0])  # admin
# Update
users[1] = "guest"
# Delete
del users[2]
# Add item
users.append("user3")
# Check membership
if "admin" in users:
    print("Admin exists!")
# Loop with index
for idx, user in enumerate(users):
    print(f"User {idx}: {user}")

📊 Dictionaries

Dictionaries store key-value pairs:

# Create
config = {"host": "localhost", "port": 22}
# Read
print(config["host"])  # localhost
# Safe access
print(config.get("user", "not set"))
# Update
config["port"] = 2222
# Delete
del config["host"]
# Loop
for key, value in config.items():
    print(f"{key}: {value}")

🎮 If Statements

Make decisions in your code:

# Simple if
if 5 > 2:
    print("5 is greater than 2")
# if-else
user = "admin"
if user == "admin":
    print("Welcome, admin!")
else:
    print("Access denied")
# if-elif-else
score = 75
if score > 90:
    print("Excellent")
elif score > 60:
    print("Good")
else:
    print("Needs improvement")
# Membership test
users = ["admin", "user1"]
if "admin" in users:
    print("Admin found!")

🔄 Loops

Repeat actions multiple times:

# For loop
for i in range(3):
    print(i)  # 0, 1, 2
# Loop through list
for user in users:
    print(user)
# While loop
n = 0
while n < 3:
    print(n)
    n += 1
# Loop with break
for i in range(5):
    if i == 3:
        break
    print(i)  # 0, 1, 2
# Loop with continue
for i in range(5):
    if i % 2 == 0:
        continue
    print(i)  # 1, 3

🔧 Functions

Reusable blocks of code:

# Simple function
def greet():
    print("Hello!")
greet()
# With argument
def greet_user(name):
    print(f"Hello, {name}!")
greet_user("admin")
# With return value
def add(a, b):
    return a + b
print(add(2, 3))  # 5
# With default argument
def show_port(port=22):
    print(f"Port: {port}")
show_port()    # Port: 22
show_port(80)  # Port: 80
# With docstring
def check_user(username, users):
    """Return True if user exists."""
    return username in users

📦 Modules

Modules extend Python's capabilities:

import os
print(os.getcwd())
import math
print(math.sqrt(16))  # 4.0
import random
print(random.randint(1, 10))
from os import getcwd
print(getcwd())
import sys
print(sys.version)

🛠️ Hands-On Practice

List files in directory

import os
for f in os.listdir('.'):
    print(f)

Check disk space

import shutil
total, used, free = shutil.disk_usage("/")
print(f"Free space: {free // (1024**3)} GB")

Simple function

def list_files(path="."):
    """List all files in a directory."""
    return os.listdir(path)
print(list_files())

✅ What We Did Today

Python by srk
Logo
💬 save me