0% found this document useful (0 votes)
1 views4 pages

python_syntax_guide

This Python Syntax Cheat Sheet provides a concise overview of essential Python concepts including variables, data types, operators, strings, lists, dictionaries, conditionals, loops, functions, list comprehensions, classes, and error handling. Each section includes examples to illustrate the syntax and usage. It serves as a quick reference for beginners and experienced programmers alike.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

python_syntax_guide

This Python Syntax Cheat Sheet provides a concise overview of essential Python concepts including variables, data types, operators, strings, lists, dictionaries, conditionals, loops, functions, list comprehensions, classes, and error handling. Each section includes examples to illustrate the syntax and usage. It serves as a quick reference for beginners and experienced programmers alike.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

✦ Python Syntax Cheat Sheet ✦

A cute little guide to the basics

◆ Variables & Types


Python figures out the type for you — no need to declare it!

name = "Luna" # string


age = 7 # integer
height = 1.42 # float
is_cute = True # boolean
pet = None # empty value

◆ Numbers & Operators


Basic math and comparisons.

x = 10 + 3 # add -> 13
x = 10 - 3 # subtract -> 7
x = 10 * 3 # multiply -> 30
x = 10 / 3 # divide -> 3.333...
x = 10 // 3 # floor divide -> 3
x = 10 % 3 # remainder -> 1
x = 10 ** 2 # power -> 100
x == y x != y x > y x <= y

◆ Strings
Text lives in quotes, and f-strings make it easy to mix in values.

greeting = f"Hi, {name}! You are {age}."


[Link]() # HI, LUNA!
[Link]() # hi, luna!
[Link]() # remove edge spaces
[Link]("Hi", "Hey")
[Link](" ") # -> list of words
len(greeting) # length of string

Page 1 • Python Syntax Cheat Sheet


◆ Lists
Ordered, changeable collections — the workhorse of Python.

pets = ["cat", "dog", "fish"]


[Link]("bird") # add to end
pets[0] # -> "cat"
pets[-1] # -> last item
pets[1:3] # slice -> ["dog","fish"]
len(pets) # -> 4
"cat" in pets # -> True
sorted(pets) # new sorted list

◆ Dictionaries, Tuples & Sets


Key–value pairs, fixed sequences, and unique collections.

info = {"name": "Luna", "age": 7}


info["name"] # -> "Luna"
info["color"] = "orange" # add a key
point = (3, 4) # tuple (fixed)
colors = {"red", "blue", "red"} # set -> {red, blue}

Page 2 • Python Syntax Cheat Sheet


◆ Conditionals
Make decisions with if / elif / else.

if age < 1:
print("kitten")
elif age < 5:
print("young cat")
else:
print("adult cat")

◆ Loops
Repeat actions with for and while.

for pet in pets:


print(pet)

for i in range(5): # 0,1,2,3,4


print(i)

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

◆ Functions
Reusable blocks of code, defined with def.

def greet(name, excited=False):


if excited:
return f"HI {name}!!"
return f"Hi {name}."

greet("Luna") # "Hi Luna."


greet("Luna", excited=True)

◆ List Comprehensions
A short, elegant way to build lists.

squares = [n**2 for n in range(6)]


# -> [0, 1, 4, 9, 16, 25]

evens = [n for n in range(10) if n % 2 == 0]


# -> [0, 2, 4, 6, 8]

Page 3 • Python Syntax Cheat Sheet


◆ Classes
Blueprints for creating objects.

class Pet:
def __init__(self, name, age):
[Link] = name
[Link] = age

def speak(self):
return f"{[Link]} says hi!"

luna = Pet("Luna", 7)
[Link]() # "Luna says hi!"

◆ Errors & Imports


Handle problems gracefully, and bring in extra tools.

try:
result = 10 / 0
except ZeroDivisionError:
print("Oops, can't divide by zero!")

import math
from datetime import date

Tip: keep exploring with help(), dir(), and the official Python docs.

Page 4 • Python Syntax Cheat Sheet

You might also like