Skip to content

python

A quick reference to common Python syntax and concepts.

Data Types

Common data types in Python.

Integer

Whole numbers.

10

Float

Decimal numbers.

3.14

String

Textual data.

Hello, world!

Boolean

True or False values.

True

List

Ordered, mutable sequences.

[1, 2, 3]

Tuple

Ordered, immutable sequences.

(1, 2, 3)

Dictionary

Key-value pairs.

{'a': 1, 'b': 2}

Set

Unordered collections of unique elements.

{1, 2, 3}

Control Flow

Controlling the execution of code.

if statement

Conditional execution.

Terminal window
if x > 0:
print("Positive")

for loop

Iterating over a sequence.

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

while loop

Repeating code while a condition is true.

Terminal window
while x < 10:
x += 1

break

Exiting a loop.

Terminal window
for i in range(10):
if i == 5:
break

continue

Skipping to the next iteration of a loop.

Terminal window
for i in range(10):
if i % 2 == 0:
continue
print(i)

Functions

Reusable blocks of code.

Function definition

Creating a function.

Terminal window
def greet(name):
return f"Hello, {name}!"

Function call

Using a function.

Terminal window
message = greet("Alice")
print(message)

Lambda function

Anonymous, small functions.

Terminal window
square = lambda x: x * x

Common Operations

Frequent operations in Python

String formatting

Creating strings with embedded values

f'The value is {x}'

List comprehension

Creating lists concisely

[x**2 for x in range(10)]

Slicing

Accessing parts of a sequence

my_list[1:4]

File I/O

Reading and writing files

Terminal window
with open("my_file.txt", "r") as f:
content = f.read()