codegurukool.com · Free coding tutorials & online compiler

Home › Python Tutorial › All commands

All commands

Every command of this course: name, detailed description, syntax and example code.

Every command used in this course, with its name, a detailed description, the syntax and an example.

print

Definition: Shows values on the screen. Several values separated by commas are printed with spaces between them.

Syntax:

print(value, ...)

Example:

print("Hello", 5)

input

Definition: Pauses the program and returns what the user typed, always as text (a string).

Syntax:

input(prompt)

Example:

name = input("Name: ")

def

Definition: Defines a function. Indentation shows which lines belong to it; return sends a value back.

Syntax:

def name(parameters):
    return value

Example:

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

if / elif / else

Definition: Chooses which indented block to run. Each condition ends with a colon.

Syntax:

if condition:
    ...
elif other:
    ...
else:
    ...

Example:

if x > 3:
    print("big")

for

Definition: Repeats a block for every item of a sequence such as a list or range().

Syntax:

for item in sequence:
    ...

Example:

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

while

Definition: Repeats an indented block while its condition is true.

Syntax:

while condition:
    ...

Example:

n = 0
while n < 3:
    n += 1

len

Definition: Returns the number of items in a list, string, tuple or dictionary.

Syntax:

len(object)

Example:

len("hello")  # 5

range

Definition: Produces a sequence of numbers, handy for loops: range(stop) or range(start, stop, step).

Syntax:

range(start, stop, step)

Example:

list(range(1, 4))  # [1, 2, 3]

append

Definition: Adds one item to the end of a list.

Syntax:

list.append(item)

Example:

a = [1, 2]
a.append(3)

import

Definition: Loads a module so you can use its functions, such as math or random.

Syntax:

import module

Example:

import math
print(math.sqrt(16))

#

Definition: Comments start with # .

Syntax:

#

Example:

# Indentation defines the block

↗ Python syntax and comments

type()

Definition: print shows values separated by spaces; type() tells you the type.

Syntax:

type()

Example:

print(type(x))

↗ Python variables and output

int

Definition: Whole numbers are int , decimals are float .

Syntax:

int

Example:

print(int(c) + a)

↗ Python data types, numbers and casting

upper

Definition: Methods like upper and replace return a new string.

Syntax:

upper

Example:

print(s.upper())

↗ Python strings and string formatting

replace

Definition: Methods like upper and replace return a new string.

Syntax:

replace

Example:

print(s.replace("World", "Python"))

↗ Python strings and string formatting

and

Definition: Combine them with and , or and not .

Syntax:

and

Example:

print(x > 5 and x < 20)

↗ Python booleans and operators

not

Definition: Combine them with and , or and not .

Syntax:

not

Example:

print(not x == 10)

↗ Python booleans and operators

sort

Definition: append adds an item, sort orders them, and a negative index counts from the end.

Syntax:

sort

Example:

fruits.sort()

↗ Python lists

get

Definition: Use [key] to read or set, get for a safe read with a default, and items() to loop over keys and values together.

Syntax:

get

Example:

print(p.get("zip", "n/a"))

↗ Python dictionaries

items()

Definition: Use [key] to read or set, get for a safe read with a default, and items() to loop over keys and values together.

Syntax:

items()

Example:

for k, v in p.items():

↗ Python dictionaries

if

Definition: Use if , elif and else .

Syntax:

if

Example:

if score >= 90:
print("pass" if score >= 60 else "fail")

↗ Python if...else

elif

Definition: Use if , elif and else .

Syntax:

elif

Example:

elif score >= 70:

↗ Python if...else

else

Definition: Use if , elif and else .

Syntax:

else

Example:

else:
print("pass" if score >= 60 else "fail")

↗ Python if...else

match

Definition: match compares a value against case patterns.

Syntax:

match

Example:

match day:

↗ Python match

case

Definition: match compares a value against case patterns.

Syntax:

case

Example:

case 1:
case 2:
case 3:

↗ Python match

_

Definition: _ is the default that matches anything.

Syntax:

_

Example:

case _:

↗ Python match

lambda

Definition: A lambda is a small one-line function.

Syntax:

lambda

Example:

square = lambda n: n * n

↗ Python functions

math

Definition: The standard library includes math , datetime , random , json and many more.

Syntax:

math

Example:

import math
print(math.sqrt(81), math.pi)

↗ Python modules, dates and math

datetime

Definition: The standard library includes math , datetime , random , json and many more.

Syntax:

datetime

Example:

import datetime
d = datetime.date(2026, 12, 25)

↗ Python modules, dates and math

strftime

Definition: strftime formats a date as text.

Syntax:

strftime

Example:

print(d.strftime("%A, %d %B %Y"))

↗ Python modules, dates and math

json.dumps

Definition: json.dumps turns a dictionary into JSON text and json.loads turns it back.

Syntax:

json.dumps

Example:

text = json.dumps(data)

↗ Python JSON and RegEx

json.loads

Definition: json.dumps turns a dictionary into JSON text and json.loads turns it back.

Syntax:

json.loads

Example:

print(json.loads(text)["langs"][0])

↗ Python JSON and RegEx

re

Definition: The re module finds patterns in text; [0-9]+ matches one or more digits.

Syntax:

re

Example:

import json, re
print(re.findall("[0-9]+", "a1 b22 c333"))

↗ Python JSON and RegEx

try

Definition: Put risky code in try and handle errors in except .

Syntax:

try

Example:

try:

↗ Python try...except, None and input

except

Definition: Put risky code in try and handle errors in except .

Syntax:

except

Example:

except ValueError:

↗ Python try...except, None and input

finally

Definition: else runs if nothing failed and finally always runs.

Syntax:

finally

Example:

finally:

↗ Python try...except, None and input

None

Definition: None means no value; test it with is None .

Syntax:

None

Example:

x = None
print(x is None)

↗ Python try...except, None and input

__init__

Definition: __init__ runs when an object is created, and self is the object itself.

Syntax:

__init__

Example:

def __init__(self, name):

↗ Python classes and objects

self

Definition: __init__ runs when an object is created, and self is the object itself.

Syntax:

self

Example:

def __init__(self, name):
self.name = name
def speak(self):

↗ Python classes and objects

speak()

Definition: Polymorphism means the same call ( speak() ) behaves differently for different classes.

Syntax:

speak()

Example:

def speak(self):
print(a.speak())

↗ Python inheritance and polymorphism

__balance

Definition: A name starting with two underscores ( __balance ) is private to the class.

Syntax:

__balance

Example:

self.__balance = balance
self.__balance += n
return f"Balance: {self.__balance}"

↗ Python encapsulation and magic methods

__str__

Definition: Magic methods such as __str__ control how an object prints.

Syntax:

__str__

Example:

def __str__(self):

↗ Python encapsulation and magic methods

open(name, mode)

Definition: open(name, mode) uses modes "r" read, "w" write (replaces the file) and "a" append.

Syntax:

open(name, mode)

Example:

with open("notes.txt", "w") as f:
with open("notes.txt") as f:

↗ Python file handling

with

Definition: The with statement closes the file automatically.

Syntax:

with

Example:

with open("notes.txt", "w") as f:
with open("notes.txt") as f:

↗ Python file handling

os.remove

Definition: os.remove deletes a file.

Syntax:

os.remove

Example:

os.remove("notes.txt")

↗ Python file handling

statistics

Definition: The statistics module gives the mean (average), median (middle value), mode (most common) and standard deviation (spread).

Syntax:

statistics

Example:

import statistics as st

↗ Python statistics and machine learning basics

pop

Definition: A stack is last-in-first-out (a list with append and pop ).

Syntax:

pop

Example:

print(stack.pop())

↗ Python data structures and algorithms

sqlite3

Definition: The built-in sqlite3 module works the same way and needs no server, so it is a good way to practise.

Syntax:

sqlite3

Example:

import sqlite3
con = sqlite3.connect(":memory:")

↗ Python databases: MySQL and MongoDB

Example code

print("Hello, Python!")

Run this code in the online editor

← Welcome to the Python coursePython introduction and get started →